Making Choices with xsl:if and xsl:choose

Making Choices with <xsl:if> and <xsl:choose>

The <xsl:if> element lets you make choices. To use this element, you assign its test attribute a value that evaluates to a Boolean value of true or false . If it evaluates to true , the enclosed XSLT elements are also evaluated, but they are not evaluated if test evaluates to false .

Let's take a look at an example. In this case, we'll list the three planets in ch05_01.xml , and use <xsl:if> to add two horizontal rule elements ( <HR ) both above and below this list to offset the list visually. You can see how this works, using the position function to determine the top and bottom of the list, in ch05_12.xsl in Listing 5.11.

Listing 5.11 Using <xsl:if> ( ch05_12.xsl )
 <?xml version="1.0" encoding="UTF-8"?>  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="planets">         <HTML>             <HEAD>                 <TITLE>                     The First Three Planets                 </TITLE>             </HEAD>             <BODY>                 <H1>                     The First Three Planets                 </H1>                 <xsl:apply-templates select="planet"/>             </BODY>         </HTML>     </xsl:template>     <xsl:template match="planet">  <xsl:if test="position() = 1"><HR/><HR/></xsl:if>   <P>   Number <xsl:value-of select="position()"/>. <xsl:value-of select="name"/>   </P>   <xsl:if test="position() = last()"><HR/><HR/></xsl:if>  </xsl:template> </xsl:stylesheet> 

Here's what you get when you use this new stylesheetnote the two <HR> elements before and after the list of planets:

 
 <HTML>     <HEAD>         <TITLE>             The First Three Planets         </TITLE>     </HEAD>     <BODY>         <H1>             The First Three Planets         </H1>  <HR>   <HR>   <P>Number 1. Mercury</P>   <P>Number 2. Venus</P>   <P>Number 3. Earth</P>   <HR>   <HR>  </BODY> </HTML> 

You can see what this looks like in Figure 5.2.

Figure 5.2. Making choices with < xsl:if> .

graphics/05fig02.jpg

Besides <xsl:if> , you can also use <xsl:choose> to make decisions. This element lets you compare a test value against several possibilities.

For example, say that we want to color -code the planet namesMercury in red, Venus in white, and Earth in green. You can handle these possibilities with <xsl:choose> . Inside this element, you use the <xsl:when> element, setting the test attribute in those elements to the Boolean expression you want to test. For example, Mercury is first in the list, so we can display it in red this way:

 
 <xsl:template match="planet">  <xsl:choose>   <xsl:when test="name = 'Mercury'">   <P>   <FONT COLOR="RED"><B>   <xsl:value-of select="name"/>   </B></FONT>   </P>   </xsl:when>  .     .     . 

We can handle the other two planets using other <xsl:when> elements in the same <xsl:choose> , as you see in ch05_13.xsl in Listing 5.12. Note also that at the end of the group of <xsl:when> element, there's an (optional) <xsl: otherwise > elementif none of the <xsl:when> elements match their test conditions, the <xsl:otherwise> element is chosen .

Listing 5.12 Using <xsl:choose> ( ch05_13.xsl )
 <?xml version="1.0" encoding="UTF-8"?>  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="planets">         <HTML>             <HEAD>                 <TITLE>                     Color Coding the Planets                 </TITLE>             </HEAD>             <BODY BGCOLOR="PINK">                 <H1>                     Color Coding the Planets                 </H1>                 <xsl:apply-templates select="planet"/>             </BODY>         </HTML>     </xsl:template>     <xsl:template match="planet">  <xsl:choose>   <xsl:when test="name = 'Mercury'">   <P>   <FONT COLOR="RED"><B>   <xsl:value-of select="name"/>   </B></FONT>   </P>   </xsl:when>   <xsl:when test="name = 'Venus'">   <P>   <FONT COLOR="WHITE"><B>   <xsl:value-of select="name"/>   </B></FONT>   </P>   </xsl:when>   <xsl:when test="name = 'Earth'">   <P>   <FONT COLOR="GREEN"><B>   <xsl:value-of select="name"/>   </B></FONT>   </P>   </xsl:when>   <xsl:otherwise>   <P>   <xsl:value-of select="."/>   </P>   </xsl:otherwise>   </xsl:choose>  </xsl:template> </xsl:stylesheet> 

Here's what you get when you use this stylesheet on our planetary XML document, ch05_01.xml (note that we're setting the background color to pink to make sure that even the white text we'll be displaying stands out):

 
 <HTML>     <HEAD>         <TITLE>             Color Coding the Planets         </TITLE>     </HEAD>     <BODY BGCOLOR="PINK">         <H1>             Color Coding the Planets         </H1>         <P>  <FONT COLOR="RED"><B>Mercury</B></FONT>  </P>         <P>  <FONT COLOR="WHITE"><B>Venus</B></FONT>  </P>         <P>  <FONT COLOR="GREEN"><B>Earth</B></FONT>  </P>     </BODY> </HTML> 

You can see what this result document looks like in Figure 5.3 in glorious black and white (to see the actual colors for yourself, use ch05_13.xsl on ch05_01.xml and open the result in a browser).

Figure 5.3. Making selections with < xsl:choose> .

graphics/05fig03.jpg

Another important XSLT topic concerns the default template rules that XSLT uses, and that's coming up next .



XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net