The Stylesheet The XPath View of an XML Document

page 53 xsl:comment style head body h1XPath view of your documenth1 pThe structure of your document as defined by the XPath standard is outlined below.p table cellspacing=5 cellpadding=2 border=0 tr td colspan=7 bNode types:b td tr tr td bgcolor=99CCCCbrootbtd td bgcolor=CCCC99belementbtd td bgcolor=FFFF99battributebtd td bgcolor=FFCC99btextbtd td bgcolor=CCCCFFbcommentbtd td bgcolor=99FF99bprocessing instructionbtd td bgcolor=CC99CCbnamespacebtd tr table br table width=100 border=1 bgcolor=99CCCC cellspacing=2 tr bgcolor=99CCCC td colspan=2 broot:b td tr xsl:for-each select=namespace:: tr bgcolor=CC99CC td width=15 td td xsl:call-template name=namespace-node td tr xsl:for-each xsl:for-each select=|comment|processing-instruction|text tr bgcolor=99CCCC td width=15 td td xsl:apply-templates select=. td tr xsl:for-each table body html xsl:template xsl:template match=comment table width=100 cellspacing=2 tr td bgcolor=CCCCFF bcomment: b span class=literal xsl:value-of select=. span td tr table xsl:template xsl:template match=processing-instruction table border=0 width=100 cellspacing=2 tr td bgcolor=99FF99 bprocessing instruction: b span class=literal xsl:textlt;?xsl:text page 54 xsl:value-of select=name xsl:text?gt;xsl:text br xsl:value-of select=. span td tr table xsl:template xsl:template match=text xsl:if test=string-lengthnormalize-space. tr td bgcolor=CCCC99 width=15 td td bgcolor=FFCC99 width=100 btext: b span class=literal xsl:value-of select=. span td tr xsl:if xsl:template xsl:template name=namespace-node table border=0 width=100 cellspacing=2 tr td bgcolor=CC99CC bnamespace: b span class=literal xsl:value-of select=name span br span class=literal xsl:value-of select=. span td tr table xsl:template xsl:template match= table border=1 width=100 cellspacing=2 xsl:choose xsl:when test=count gt; 0 tr td bgcolor=CCCC99 colspan=2 belement: b span class=literal xsl:textlt;xsl:text xsl:value-of select=name xsl:textgt;xsl:text span table border=0 width=100 cellspacing=2 tr td bgcolor=CCCC99 width=15 td td bgcolor=FFFF99 width=20 battribute nameb td td bgcolor=FFFF99 bvalueb td tr xsl:for-each select= tr td bgcolor=CCCC99 width=15 td td bgcolor=FFFF99 width=20 span class=literal xsl:value-of select=name span td page 55 td bgcolor=FFFF99 span class=literal xsl:value-of select=. span td tr xsl:for-each table td tr xsl:when xsl:otherwise tr td bgcolor=CCCC99 colspan=2 belement: b span class=literal xsl:textlt;xsl:text xsl:value-of select=name xsl:textgt;xsl:text span td tr xsl:otherwise xsl:choose xsl:for-each select=namespace:: tr td bgcolor=CCCC99 width=15 td td bgcolor=CC99CC xsl:call-template name=namespace-node td tr xsl:for-each xsl:for-each select=|comment|processing-instruction|text tr bgcolor=CCCC99 td width=15 td td xsl:apply-templates select=. td tr xsl:for-each table xsl:template xsl:stylesheet Before we leave this example, there are a couple of other techniques worth mentioning here. First, notice that we used CSS to format some of the output. XSLT and CSS arent mutually exclusive; you can use XSLT to generate CSS as part of an HTML page, as we demonstrated here. Second, we used wildcard expressions like and to process all the elements and attributes in our document. Use of these expressions allows us to apply this stylesheet to any XML document, regardless of the tags it uses. Because we use these wildcard expressions, we have to use the name function to get the name of the element or attribute were currently working with. Third, notice that we used conditional logic and the expression count gt; to determine whether a given element has attributes. Well talk more about conditional logic in the next chapter.

3.6 Summary

Weve covered the basics of XPath. Hopefully, at this point youre comfortable with the idea of writing XPath expressions to describe parts of an XML document. As we go through the following chapters, youll see XPath expressions used in a variety of ways, all of which build on the basics weve discussed here. Youll probably spend most of your debugging time working on the XPath expressions in your stylesheets. Very few of the things well do in the rest of the book are possible without precise XPath expressions. page 56

Chapter 4. Branching and Control Elements

So far, weve done some straightforward transformations and weve been able to do some reasonably sophisticated things. To do truly useful work, though, well need to use logic in our stylesheets. In this chapter, well discuss the XSLT elements that allow you to do just that. Although youll see several XML elements that look like constructs from other programming languages, theyre not exactly the same. As we go along, well discuss what makes XSLT different and how to do common tasks with your stylesheets.

4.1 Goals of This Chapter

By the end of this chapter, you should: • Know the XSLT elements used for branching and control • Understand the differences between XSLTs branching elements and similar constructs in other programming languages • Know how to invoke XSLT templates by name and how to pass parameters to them, if you want • Know how to use XSLT variables • Understand how to use recursion to get around the limitations of XSLTs branching and control elements

4.2 Branching Elements of XSLT

Three XSLT elements are used for branching: xsl:if , xsl:choose , and xsl:for-each . The first two are much like the if and case statements you may be familiar with from other languages, while the for-each element is significantly different from the for or do-while structures in other languages. Well discuss all of them here.

4.2.1 The xsl:if Element

The xsl:if element looks like this: xsl:if test=countzone gt; 2 xsl:textApplicable zones: xsl:text xsl:apply-templates select=zone xsl:if The xsl:if element, surprisingly enough, implements an if statement. The element has only one attribute, test . If the value of test evaluates to the boolean value true , then all elements inside the xsl:if are processed. If test evaluates to false , then the contents of the xsl:if element are ignored. If you want to implement an if-then-else statement, check out the xsl:choose element described in the next section. Notice that we used gt; instead of in the attribute value. Youre always safe using gt; here, although some XSLT processors process the greater-than sign correctly if you use instead. If you need to use the less-than operator , youll have to use the lt; entity. The same holds true for the less-than-or-equal operator = and the greater-than-or-equal = operators. See Section B.4.2 for more information on this topic.