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.