XSLT for Dummies
|
Chapter 7 - Adding Programming Logic Isnt Just for Propheads | |
XSLT For Dummies | |
by Richard Wagner | |
Hungry Minds 2002 |
The most basic of all control structures is the if statement. It performs one or more actions if certain conditions are met. XSLT uses xsl:if instruction for this purpose: <xsl:if test="expression"> do something </xsl:if> In effect, this instruction says: if the test expression is true, then process the lines inside the start and end xsl:if tags. For example, the following xsl:if instruction sends the literal text Extra large size is required to the result tree if the current node has a size attribute that equals XL : <xsl:if test="@size='XL'"> Extra large size is required. </xsl:if> Remember xsl:if can be used only inside an xsl:template rule. Otherwise, you get an error. Let me give you a fuller example to demonstrate xsl:if , starting with the students.xml file shown in Listing 7-1 as my source document.
Listing 7-1: students.xml
<?xml version="1.0"?> <!-- students.xml --> <school name="Elliot Academy"> <student id="601" name="Jordan"> <class name="Language Arts" days="5">Sentence diagramming</class> <class name="Reading" favorite="true" days="5">Lord Of The Rings</class> <class name="Writing" days="3">Colonial Times</class> <class name="Geography" days="2">African Sahel</class> <class name="Math" days="5" section="6.42">Decimals</class> <class name="Science" days="3" level="advanced">Volcanos</class> <class name="History" days="3">American Presidents</class> <class name="Art" days="1">Drawing</class> </student> <student id="401" name="Jared"> <class name="Language Arts" days="5" section="4.56">Punctuation</class> <class name="Reading" days="5">Voyage Of The Dawntreader</class> <class name="Writing" days="3">Haiku Poetry</class> <class name="Geography" favorite="true" days="2">African Sahel</class> <class name="Math" days="5" section="4.45">Fractions</class> <class name="Science" days="3" level="basic">Insects</class> <class name="History" days="3">American Presidents</class> <class name="Art" days="1">Paper Mache</class> </student> <student id="301" name="Justus"> <class name="Language Arts" days="5" section="3.80">Capitalization</class> <class name="Reading" days="5">Sherlock Holmes Solves Them All</class> <class name="Writing" days="3">Penmanship</class> <class name="Geography" days="2">African Sahel</class> <class name="Math" favorite="true" days="5" section="3.30">Division</class> <class name="Science" days="3" level="basic">Vertebrates</class> <class name="History" days="3">American Presidents</class> <class name="Art" days="1">Clay Sculptures</class> </student> </school>
Suppose I want to create a text-based report for each of the three students. If the desired output were the same for each of them, then I could use a normal template rule along with xsl:apply-templates or xsl:value-of . However, in this case, Id actually like to provide literal text that is customized for each student and make variations in my output based on each student. Given these requirements, xsl:if becomes a great tool at my disposal, because I can use it to test for a specific student, and if that expression has a true value, then I can tell the processor to write the customized output to the result tree. For example, for a student named Jordan, the following xsl:if instruction is used: <xsl:template match="student"> <xsl:if test="@name='Jordan'"> ******************************************* Jordan is a 6th grader with an id of <xsl:value-of select="@id"/> Emphasizing: Reading: <xsl:value-of select="class[@name='Reading']"/> Writing: <xsl:value-of select="class[@name='Writing']"/> ******************************************* </xsl:if> </xsl:template> As the template rule is processed and run on each student element, the XSLT processor faces a fork in the road for each node when it gets to the xsl:if statement. Either the conditions of test are met or they are not. If so, then the text inside is added to the result document. If not, then it is ignored. Heres what the complete stylesheet, which contains a test for each student, looks like:
<!-- students.xsl --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="student"> <xsl:if test="@name='Jordan'"> ******************************************* Jordan is a 6th grader with an id of <xsl:value-of select="@id"/> Emphasizing: Reading: <xsl:value-of select="class[@name='Reading']"/> Writing: <xsl:value-of select="class[@name='Writing']"/> ******************************************* </xsl:if> <xsl:if test="@name='Jared'"> ******************************************* Jared is a 4th grader with an id of <xsl:value-of select="@id"/> Emphasizing: Language: <xsl:value-of select="class[@name='Language Arts']"/> Writing: <xsl:value-of select="class[@name='Writing']"/> ******************************************* </xsl:if> <xsl:if test="@name='Justus'"> ******************************************* Justus is a 3rd grader with an id of <xsl:value-of select="@id"/> Emphasizing: Reading: <xsl:value-of select="class[@name='Reading']"/> Geography: <xsl:value-of select="class[@name='Geography']"/> ******************************************* </xsl:if> </xsl:template> </xsl:stylesheet> The text document generated looks like this: ******************************************* Jordan is a 6th grader with an id of 601 Emphasizing: Reading: Lord Of The Rings Writing: Colonial Times ******************************************* ******************************************* Jared is a 4th grader with an id of 401 Emphasizing: Language: Punctuation Writing: Haiku Poetry ******************************************* ******************************************* Justus is a 3rd grader with an id of 301 Emphasizing: Reading: Sherlock Holmes Solves Them All Geography: African Sahel ******************************************* Technical Stuff Unlike most other programming languages, XSLT does not have an else statement to go along with if . Use a series of xsl:if statements or else the xsl:choose instruction instead.
|