Datasheet
Williams c01.tex V3 - 07/31/2009 2:53pm Page 6
Chapter 1: First Steps with XSLT
version
=
"1.0">
<xsl:output
method
=
"xml"
encoding
=
"UTF-8"
doctype-system
=
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
doctype-public
=
"-//W3C//DTD XHTML 1.0 Transitional//EN"/>
...
</xsl:stylesheet>
An XSLT 2.0 stylesheetstylesheet may contain multiple <
xsl:output>
declarations and may include or
import stylesheet modules that also contain <
xsl:output>
declarations. This enables you to use one or
more stylesheets to output results using different methods. So you might, for instance, output both a CSV
file and a web page in one pass.
If you use multiple declarations in this way, the
name
attribute must be specified on each
<
xsl:output>
element to identify it. These names should match a set of
format
attribute values
on
<xsl:result-document>
instructions, which I discuss in Chapter 7, in the stylesheet. The following
snippet briefly illustrates how this might work:
<xsl:output name
=
"web" method
=
"xhtml" encoding
=
"UTF-8"/>
<xsl:output name
=
"csv" method
=
"text"/>
<xsl:template match
=
"/">
<xsl:result-document format
=
"web">
...
</xsl:result-document>
<xsl:result-document format
=
"csv">
...
</xsl:result-document>
</xsl:template>
The form of
name
attributes on XSLT elements is defined as a lexical QName or namespace qualified
name. It applies, for example, to templates, attribute sets, variables, parameters, and so on. Typically
this is a simple name, but it may also be qualified with a namespace prefix such as
<xsl:function
name
=
"xm:getentry-by-id">
. If two qualified names are compared, the namespace URI that is
declared with the prefix and the local name is used.
Main Template
The <
xsl:template>
element, which is covered in more detail in Chapter 3, is a basic building block of a
stylesheet. This element is used to declare templates that match elements in the XML source, and to gen-
erate nodes in a result tree. Usually a stylesheet has a main template with the
match
attribute, set to
"/"
:
<xsl:template match
=
"/">
...
<xsl:apply-templates select
=
"reference/body"/>
...
</xsl:template>
This value is an XPath expression that means ‘‘match the root of the source tree.’’ Note that this is not
the same thing as the root element of the source document. The root of the source tree is outside of
everything, including the containing top-level element.
6