The Stylesheet

Now we’ll 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, defining some CSS styles and creating a legend for our nested tree view. Having created the legend for our document, we select all the different types of nodes and represent them:

<xsl:for-each select="*|comment()|processing-instruction()|text()">

It’s very important to understand the difference between the XPath document root and the XML root element. In our XML sonnet, there are processing instructions and comments outside the root element. The document root contains those processing instructions and comments in addition to the <sonnet> element itself. In a location path, / represents the document root, while /sonnet represents the root element in our XML document. (The more general expression /* represents the root element in any document.)

[2.0] The situation is even more complicated in XPath 2.0, where you have to distinguish between a node type (document node, element node, etc.) and the role the node plays: nodes of any type can be at the root of a document. It’s only in well-formed XML documents that we can say that the root node is a document node and its only element child is the document element. All that being said, if you’re transforming XML documents, this situation will come up very rarely.

The select attribute in the ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.