10.1.5. A Simple XSL Style Sheet
XSL allows developers to describe transformations using XSL
Transformations (XSLT), which can convert XML documents into XSL
Formatting Objects, HTML, or other textual output.
As this book goes to print, the XSL Formatting Objects specification
is still changing; therefore, this book covers only the XSLT portion
of XSL. The examples that follow, however, are consistent with the
W3C specification.
Let's add a simple XSL style sheet to the example:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<font size="+1">
<xsl:apply-templates/>
</font>
</xsl:template>
</xsl:stylesheet>
The first thing you might notice when you look at an XSL style sheet
is that it is formatted in the same way as a regular XML document.
This is not a coincidence. By design, XSL stylesheets are themselves
XML documents, so they must adhere to the same rules as well-formed
XML documents.
Breaking down the pieces, you should first note that all XSL elements
must be contained in the appropriate
<xsl:stylesheet> outer element. This tells
the XSLT processor that it is describing style sheet information, not
XML content itself. After the opening
<xsl:stylesheet> tag, we see an XSLT
directive to optimize output for HTML. Following that are the rules
that will be applied to our XML document, given by the
<xsl:template> elements (in this case, there
is only one rule).
Each rule can be further broken down into two items: a
template pattern and a template
action. Consider the line:
<xsl:template match="/">
This line forms the template pattern of the style sheet rule. Here,
the target pattern is the root element, as designated by
match="/". The / is shorthand
to represent the XML document's root element.
The contents of the <xsl:template> element:
<font size="+1">
<xsl:apply-templates/>
</font>
specify the template action that should be performed on the target.
In this case, we see the empty element
<xsl:apply-templates/> located inside a
<font> element. When the XSLT processor
transforms the target element, every element inside the root element
is surrounded by the <font> tags, which will
likely cause the application formatting the output to increase the
font size.
Example 10-3. sample.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3c.org/1999/XSL/Transform"
xmlns:OReilly="http://www.oreilly.com">
<xsl:output method="html">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="OReilly:Books">
<font size="+3">
<xsl:text>Books: </xsl:text>
<br/>
<xsl:apply-templates/>
</font>
</xsl:template>
<xsl:template match="OReilly:Product">
<font size="+0">
<xsl:apply-templates/>
<br/>
</font>
</xsl:template>
<xsl:template match="OReilly:Price">
<font size="+1">
<xsl:text>Price: $</xsl:text>
<xsl:apply-templates/>
<xsl:text> + tax</xsl:text>
<br/>
</font>
</xsl:template>
</xsl:stylesheet>
In this example, we target the
<OReilly:Books> element, printing the word
Books: before it in a larger font size. In
addition, the <OReilly:Product> element
applies the default font size to each of its children, and the
<OReilly:Price> tag uses a slightly larger
font size to display its children, overriding the default size of its
parent, <OReilly:Books>. (Of course, neither
one has any children elements; they simply have text between their
tags in the XML document.) The text Price:
$ will precede each of
<OReilly:Price>'s children,
and the characters + tax will
come after it, formatted accordingly.
Here is the result after we pass sample.xsl
through an XSLT processor:
<html xmlns:OReilly="http://www.oreilly.com">
<body>
<font size="+3">
Books: <br>
<font size="+0">
Webmaster in a Nutshell<br>
</font>
<font size="+1">
Price $34.95 + tax
</font>
</font>
</body>
</html>