XSLT for Dummies

 
Chapter 7 - Adding Programming Logic Isnt Just for Propheads
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

The second type of control statement in XSLT is the xsl:choose instruction. While xsl:if is the equivalent of a true/false test, xsl:choose is akin to a multiple choice test. Using it, you can choose among two or more options or optionally default to an alternative if no other condition is met.

 Technical Stuff   xsl:choose is the equivalent of the case or switch statement found in many other programming languages.

xsl:choose has a xsl:when subelement that is used to test for each condition and an optional xsl: otherwise to specify a default response. The xsl:choose syntax looks like:

<xsl:choose> <xsl:when test="expression"> do something </xsl:when> <xsl:when test="expression2"> do something </xsl:when> <xsl:when test="expression3"> do something </xsl:when> <xsl:otherwise> do something </xsl:when> </xsl:choose>

When the XSLT processor encounters an xsl:choose element, the processor evaluates each xsl:when instruction in sequential order looking for a true result of the test expression. When the first true result is found, then that xsl:when s content is processed , and the processor bypasses the remaining xsl:when and xsl:otherwise elements of the xsl:choose instruction. If no xsl:when evaluates to true, then the xsl:otherwise element is used.

For example, suppose the following code snippet is run against an element that has a name attribute with a value of Larry (such as <emp name="Larry"> ):

<xsl:choose> <xsl:when test="@name='Moe'"> My name is Moe </xsl:when> <xsl:when test="@name='Curly'"> My name is Curly </xsl:when> <xsl:when test="@name='Larry'"> My name is Larry </xsl:when> <xsl:otherwise> I am not a stooge! </xsl:when> </xsl:choose>

The XSLT processor evaluates the first xsl:when statement, but continues after it returns a false value. The same thing happens with the second xsl:when . But when the processor gets to the third xsl:when instruction, it returns a true value, so the literal string My name is Larry is output to the result tree. The xsl:otherwise is ignored in this case, because a true test expression was already found.

 Remember   As with xsl:if , you can use xsl:choose only within a template rule.

To demonstrate xsl:choose , I transform the XML document in Listing 7-1 into a new XML fallclasses structure. The changes are as follows :

  • For class name="Reading" elements, new schedule="9am" and priority="A" attributes are added.

  • For class name="Writing" elements, new schedule="10am" and priority="B" attributes are added.

  • For class name="Math" elements, new schedule="11am" and priority="C" attributes are added.

  • For every other class element, new schedule="postlunch" and priority="D" attributes are added.

The stylesheet to perform this transformation is as follows:

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/> <!-- Replace school element with fallclasses --> <xsl:template match="/"> <xsl:text> </xsl:text><fallclasses teacher="ksw"> <xsl:apply-templates/> </fallclasses> </xsl:template> <!-- Transform class elements --> <xsl:template match="class"> <xsl:choose> <xsl:when test="@name='Reading'"> <class student="{../@name}" subject="{@name}" assignment="{.}" schedule="9am" priority="A"/> </xsl:when> <xsl:when test="@name='Writing'"> <class student="{../@name}" subject="{@name}" assignment="{.}" schedule="10am" priority="B"/> </xsl:when> <xsl:when test="@name='Math'"> <class student="{../@name}" subject="{@name}" assignment="{.}" schedule="11am" priority="C"/> </xsl:when> <xsl:otherwise> <class student="{../@name}" subject="{@name}" assignment="{.}" schedule="postlunch" priority="D"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>

In the first template rule, the school document element is replaced by adding a fallclasses element. In the second rule, the xsl:choose instruction is run against each class element. For all Reading class elements, the first xsl:when statement is run, while Writing class elements act on the second and Math elements on the third. For the remaining elements, the xsl:otherwise is triggered.

After the transformation, I have a shiny new XML document:

<?xml version="1.0" encoding="utf-8"?> <fallclasses teacher="ksw"> <class student="Jordan" subject="Language Arts" assignment="Sentence diagramming" schedule="postlunch" priority="D"/> <class student="Jordan" subject="Reading" assignment="Lord Of The Rings" schedule="9am" priority="A"/> <class student="Jordan" subject="Writing" assignment="Colonial Times" schedule="10am" priority="B"/> <class student="Jordan" subject="Geography" assignment="African Sahal" schedule="postlunch" priority="D"/> <class student="Jordan" subject="Math" assignment="Decimals" schedule="11am" priority="C"/> <class student="Jordan" subject="Science" assignment="Volcanos" schedule="postlunch" priority="D"/> <class student="Jordan" subject="History" assignment="American Presidents" schedule="postlunch" priority="D"/> <class student="Jordan" subject="Art" assignment="Drawing" schedule="postlunch" priority="D"/> <class student="Jared" subject="Language Arts" assignment="Punctuation" schedule="postlunch" priority="D"/> <class student="Jared" subject="Reading" assignment="Voyage Of The Dawntreader" schedule="9am" priority="A"/> <class student="Jared" subject="Writing" assignment="Haiku Poetry" schedule="10am" priority="B"/> <class student="Jared" subject="Geography" assignment="African Sahal" schedule="postlunch" priority="D"/> <class student="Jared" subject="Math" assignment="Fractions" schedule="11am" priority="C"/> <class student="Jared" subject="Science" assignment="Insects" schedule="postlunch" priority="D"/> <class student="Jared" subject="History" assignment="American Presidents" schedule="postlunch" priority="D"/> <class student="Jared" subject="Art" assignment="Paper Mache" schedule="postlunch" priority="D"/> <class student="Justus" subject="Language Arts" assignment="Capitalization" schedule="postlunch" priority="D"/> <class student="Justus" subject="Reading" assignment="Sherlock Holmes Solves Them All" schedule="9am" priority="A"/> <class student="Justus" subject="Writing" assignment="Penmanship" schedule="10am" priority="B"/> <class student="Justus" subject="Geography" assignment="African Sahel" schedule="postlunch" priority="D"/> <class student="Justus" subject="Math" assignment="Division" schedule="11am" priority="C"/> <class student="Justus" subject="Science" assignment="Vertebrates" schedule="postlunch" priority="D"/> <class student="Justus" subject="History" assignment="American Presidents" schedule="postlunch" priority="D"/> <class student="Justus" subject="Art" assignment="Clay Sculptures" schedule="postlunch" priority="D"/> </fallclasses>

  
 
 
2000-2002    Feedback

Категории