page 26
2.3.4 Walking Through Our Example
Lets revisit our example and see how the XSLT processor transforms our document: 1. The XSLT stylesheet is parsed and converted into a tree structure.
2. The XML document is also parsed and converted into a tree structure. Dont worry too much about what that tree looks like or how it works; for now, just assume that
the XSLT processor knows everything thats in the XML document and the XSLT stylesheet. After the first two steps are done, when we describe various things using
XSLT and XPath, the processor knows what were talking about.
3. The XSLT processor is now at the root of the XML document. This is the original context.
4. There is an
xsl:template
that matches the document root:
xsl:template match= xsl:apply-templates select=greeting
xsl:template
A single forward slash is an XPath expression that means the root of the
document. 5. Now the process begins again inside the
xsl:template
. Our only instruction here is to apply whatever
xsl:template
s might apply to any
greeting
elements in the current context. The current context inside this template is defined by the
match
attribute of the
xsl:template
element. This means the XSLT processor is looking for any
greeting
elements at the document root. Because one
greeting
element is at the document root, the XSLT processor must deal with it. If more than one element matches in the current context, the XSLT processor
deals with each one in the order in which they appear in the document; this is known as document order. Looking at the
greeting
element, the
xsl:template
that applies to it is the second
xsl:template
in our stylesheet:
xsl:template match=greeting html
body h1
xsl:value-of select=. h1
body html
xsl:template
6. Now were in the
xsl:template
for the
greeting
element. The first three elements in this
xsl:template html
,
body
, and
h1
are HTML elements. Because theyre not defined with a namespace declaration, the XSLT processor passes those HTML
elements through to the output stream unaltered. The middle of our
xsl:template
is an
xsl:value-of
element. This element writes the value of something to the output stream. In this case, were using the XPath
expression
.
a single period to indicate the current node. The XSLT processor looks at the current node the
greeting
element were currently processing and outputs its text.
page 27
Because our stylesheet is an XML document were really harping on that, arent we?, we have to end the
h1
,
body
, and
html
elements here. At this point, were done with this template, so control returns to the template that invoked us.
7. Now were back in the template for the root element. Weve processed all the
greeting
elements, so were finished with this template. 8. No more elements are in the current context there is only one root element, so the
XSLT processor is done.
2.4 Stylesheet Structure
As the final part of our introduction to XSLT, well look at the contents of the stylesheet itself. Well explain all the things in our stylesheet and discuss other approaches we could
have taken.
2.4.1 The xsl:stylesheet Element
The
xsl:stylesheet
element is typically the root element of an XSLT stylesheet.
xsl:stylesheet xmlns:xsl=http:www.w3.org1999XSLTransform
version=1.0
First of all, the
xsl:stylesheet
element defines the version of XSLT were using, along with a definition of the
xsl
namespace. To be compliant with the XSLT specification, your stylesheet should always begin with this element, coded exactly as shown here. Some
stylesheet processors, notably Xalan, issue a warning message if your
xsl:stylesheet
element doesnt have these two attributes with these two values. For all examples in this book, well start the stylesheet with this exact element, defining other namespaces as needed.
2.4.2 The xsl:output Element
Next, we specify the output method. The XSLT specification defines three output methods:
xml
,
html
, and
text
. Were creating an HTML document, so HTML is the output method we want to use. In addition to these three methods, an XSLT processor is free to define its own
output methods, so check your XSLT processors documentation to see if you have any other options.
xsl:output method=html
A variety of attributes are used with the different output methods. For example, if youre using
method=xml
, you can use
doctype-public
and
doctype-system
to define the public and system identifiers to be used in the the document type declaration. If youre using
method=xml
or
method=html
, you can use the
indent
attribute to control whether or not the output document is indented. The discussion of the
xsl:output
element in Appendix A
has all the details.
2.4.3 Our First xsl:template
Our first template matches , the XPath expression for the documents root element.
xsl:template match= xsl:apply-templates select=greeting
xsl:template