XSLT transformations accept a document tree as input and produce a
tree as output. From the XSLT point of view, documents are trees
built of nodes, and there are seven types of nodes XSLT recognizes;
here are those nodes, and how XSLT processors treat them:
Node
Description
Document
root
Is the very start of the document
Attribute
‑Holds the value of an attribute after entity references have
been expanded and surrounding whitespace has been trimmed
Comment
‑Holds the text of a comment, not including <!-- and
-->
Element
‑Consists of all character data in the element, which includes
character data in any of the children of the element
Namespace
Holds the namespace's URI
Processing
instruction
‑Holds the text of the processing instruction, which does not
include <? and ?>
Text
Holds the text of the node
To indicate what node or nodes you want to work on, XSLT supports
various ways of matching or selecting nodes. For example, the
character / stands for the root node. To get us started, I'll create
a short example here that will replace the root node-and, therefore,
the whole document-with an HTML page.
As you might expect, XSLT style sheets must be well-formed XML
documents, so you start a style sheet with the XML declaration. Next,
you use a <stylesheet> element; XSLT style sheets use the
namespace xsl, which, now that XSLT has been standardized,
corresponds to http://www.w3.org/1999/ XSL/Transform. You must also
include the version attribute in the <stylesheet> element,
setting that attribute to the only current version, 1.0:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
.
.
.
That's how you start an XSLT style sheet (in fact, if you're using
a standalone program that requires you to give the name of the style
sheet you're using, you can usually omit the <xsl:stylesheet>
element). To work with specific nodes in an XML document, XSLT uses
templates. When you match or select nodes, a template tells the XSLT
processor how to transform the node for output. In this example, I
want to replace the root node with a whole new HTML document, so I
start by creating a template with the <xsl:template> element,
setting the match attribute to the node to match, "/":
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
.
.
.
</xsl:template>
</xsl:stylesheet>
When the root node is matched, the template is applied to that
node. In this case, I want to replace the root node with an HTML
document, so I just include that HTML document directly as the
content of the <xsl:template> element:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>
A trivial transformation
</TITLE>
</HEAD>
<BODY>
This transformation has replaced
the entire document.
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
And that's all it takes; by using the <xsl:template>
element, I've set up a rule in the style sheet. When the XSL
processor reads the document, the first node that it sees is the root
node. This rule matches that root node, so the XSL processor replaces
it with the HTML document, producing this result:
<HTML>
<HEAD>
<TITLE>
A trivial transformation
</TITLE>
</HEAD>
<BODY>
This transformation has
replaced
the entire
document.
</BODY>
</HTML>
That's our first, rudimentary transformation. All we've done is
replace the entire document with another one. But, of course, that's
just the beginning.