Attribute Value Templates XPath Datatypes

page 51

3.5 The XPath View of an XML Document

Before we leave the subject of XPath, well look at a stylesheet that generates a pictorial view of a document. The stylesheet has to distinguish between all of the different XPath node types, including any rarely used namespace nodes.

3.5.1 Output View

Figure 3-1 shows the output of our stylesheet. In this graphical view of the document, the nested HTML tables illustrate which nodes are contained inside of others, as well as the sequence in which these nodes occur in the original document. In the section of the document visible in Figure 3-1 , the root of the document contains, in order, two processing instructions and two comments, followed by the sonnet element. The sonnet element, in turn, contains two attributes and an auth:author element. The auth:author element contains a namespace node and an element. Be aware that this stylesheet has its limitations; if you throw a very large XML document at it, it will generate an HTML file with many levels of nested tables—probably more levels than your browser can handle. Figure 3-1. XPath tree 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; }