Output View The XPath View of an XML Document

page 52

3.5.2 The Stylesheet

Now well take a look at the stylesheet and how it works. The stylesheet creates a number of nested tables to illustrate the XPath view of the document. We begin by writing the basic HTML elements to the output stream and creating a legend for our nested tree view: xsl:template match= html head titleXPath view of your documenttitle style type=textcss xsl:comment span.literal { font-family: Courier, monospace; } 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 Having created the legend for our document, we select all the different types of nodes and represent them: xsl:for-each select=namespace:: ... xsl:for-each xsl:for-each select=|comment|processing-instruction|text ... xsl:for-each The only difficult thing here was remembering to get all of the namespace nodes. These nodes are rarely used with the exception of this example, Ive never needed them, and they can only be selected with the namespace:: axis. Also, we process the attribute nodes when we process their element node parents; thats why the select attribute just shown doesnt have in it. Heres the complete stylesheet: ?xml version=1.0? xsl:stylesheet version=1.0 xmlns:xsl=http:www.w3.org1999XSLTransform xsl:output method=html xsl:template match= html head titleXPath view of your documenttitle style type=textcss xsl:comment span.literal { font-family: Courier, monospace; } 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