Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 14th, 2005, 03:54 PM
Registered User
 
Join Date: Aug 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conditionally displaying html elements in xslt

Hi,
 I have to display radio buttons in my page usin xslt like this:
The problem is that I have to display the radio button only if the value in a particular node is NOT null/blank etc. How do I do it??? Please help.

The xml is as follows:
   <quizbody>
      <sectiontitle>College Mathematics</sectiontitle>

      <subsectiontitle>
      </subsectiontitle>

      <subsectiondesc>
      </subsectiondesc>

      <sectionpassage>
      </sectionpassage>

      <quizqna>
         <quizq>
            If n is an odd integer, which of the following must be odd?
         </quizq>

         <quiza>
            a)n + 1
         </quiza>

         <quizb>
            b) (n + 1)
            <sup>2</sup>

         </quizb>

         <quizc>
            c) n
            <sup>2</sup>

         </quizc>

         <quizd>
            d) n
            <sup>2</sup>

            + 1
         </quizd>

         <quize>e) This is the answer</quize>

         <quizanswer>e</quizanswer>

         <quizexplanation>ans is b</quizexplanation>
      </quizqna>
   </quizbody>
The xslt is as follows:

 <xsl:element name="input">
   <xsl:attribute name="type">radio</xsl:attribute>
   <xsl:attribute name="name">q<xsl:number count="quizbody"/>_<xsl:number count="quizqna"/></xsl:attribute>
   <xsl:attribute name="value">a</xsl:attribute>
   <xsl:attribute name="onclick">Engine(<xsl:number level="any"/>, this.value)</xsl:attribute>
  </xsl:element> <xsl:apply-templates select="quiza"/>
<br/>

  <xsl:element name="input">
   <xsl:attribute name="type">radio</xsl:attribute>
   <xsl:attribute name="name">q<xsl:number count="quizbody"/>_<xsl:number count="quizqna"/></xsl:attribute>
   <xsl:attribute name="value">b</xsl:attribute>
   <xsl:attribute name="onclick">Engine(<xsl:number level="any"/>, this.value)</xsl:attribute>
  </xsl:element> <xsl:apply-templates select="quizb"/>
<br/>

  <xsl:element name="input">
   <xsl:attribute name="type">radio</xsl:attribute>
   <xsl:attribute name="name">q<xsl:number count="quizbody"/>_<xsl:number count="quizqna"/></xsl:attribute>
   <xsl:attribute name="value">c</xsl:attribute>
   <xsl:attribute name="onclick">Engine(<xsl:number level="any"/>, this.value)</xsl:attribute>
  </xsl:element> <xsl:apply-templates select="quizc"/>
<br/>

  <xsl:element name="input">
   <xsl:attribute name="type">radio</xsl:attribute>
   <xsl:attribute name="name">q<xsl:number count="quizbody"/>_<xsl:number count="quizqna"/></xsl:attribute>
   <xsl:attribute name="value">d</xsl:attribute>
   <xsl:attribute name="onclick">Engine(<xsl:number level="any"/>, this.value)</xsl:attribute>
  </xsl:element> <xsl:apply-templates select="quizd"/>
<br/>
 
Old August 14th, 2005, 04:47 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I do wish that lecturers could find a more interesting exercise to give their students than these quizzes - they get rather boring after a while.

Note first that you can write this:

  <xsl:element name="input">
   <xsl:attribute name="type">radio</xsl:attribute>
   <xsl:attribute name="name">q<xsl:number count="quizbody"/>_<xsl:number count="quizqna"/></xsl:attribute>
   <xsl:attribute name="value">b</xsl:attribute>
   <xsl:attribute name="onclick">Engine(<xsl:number level="any"/>, this.value)</xsl:attribute>
  </xsl:element> <xsl:apply-templates select="quizb"/>

like this:

<input type="radio" value="b">
   <xsl:attribute name="name">q<xsl:number count="quizbody"/>_<xsl:number count="quizqna"/></xsl:attribute>
   <xsl:attribute name="onclick">Engine(<xsl:number level="any"/>, this.value)</xsl:attribute>
</input>

To make it conditional, just wrap it in xsl:if:

<xsl:if test="not(particular_node='')">
<input type="radio" value="b">
   <xsl:attribute name="name">q<xsl:number count="quizbody"/>_<xsl:number count="quizqna"/></xsl:attribute>
   <xsl:attribute name="onclick">Engine(<xsl:number level="any"/>, this.value)</xsl:attribute>
</input>
</xsl:if>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove orphaned html elements from html string pauliehaha C# 2008 aka C# 3.0 2 June 30th, 2008 09:40 AM
display out of order html elements? chobo XSLT 2 April 1st, 2008 02:28 AM
Using xml elements as html attributes chipmaster XSLT 4 June 26th, 2007 10:22 AM
adding elements in XSLT 1.0 spencer.clark XSLT 3 July 25th, 2005 09:41 AM
Grouping Elements using XSLT mathuranuj XSLT 2 June 21st, 2005 02:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.