Datasheet

Williams c01.tex V3 - 07/31/2009 2:53pm Page 7
Chapter 1: First Steps with XSLT
This means that processing will begin right at the start of the XML source tree, outside the
<reference>
element. Path expressions in this context will be relative to this location.
The contained
<xsl:apply-templates>
instruction selects the
<body>
element in the source document
(see Listing 1-1) for processing, showing the relative XPath expression
"reference/body"
in the
select
attribute. This means that the
<body>
element and everything inside it have been selected for processing.
This instruction simply defines a set of nodes to be processed using the template rules for each source
node to be matched.
You are not restricted to following the nested nodes as shown in this example. You might
want to select all the paragraphs in the source document for processing, in which case you
would use
<xsl:apply-templates select
=
"//p"/>
. There’s more on XPath expressions in
Chapter 2.
Literal Result Elements
You have two options when it comes to generating the element names that will be output. Usually,
the most straightforward is to create what is called a literal result element by typing the element name
with start and end tags straight into the stylesheet, and then populating the new elements with selected
content from the source XML.
Literal result elements are treated as data to be copied from the result tree directly to the output. These
elements can have any name, and the content may be XSLT instructions, nested literal result elements, or
text. If you set attributes on the literal result elements, they will also be copied to the output.
This gives you considerable freedom to construct output from any source in your target XML vocabulary.
For this XHTML page, you will start with something like the following skeleton:
<xsl:template match
=
"/">
<html>
<head>
<title>
<xsl:value-of select
=
"reference/body/title"/>
</title>
</head>
<body>
<p>The body goes here</p>
</body>
</html>
</xsl:template>
The preceding code will render the following output:
<html>
<head>
<meta http-equiv
=
"Content-Type" content
=
"text/html; charset
=
UTF-8">
<title>xsl:stylesheet</title>
</head>
<body>
<p>The body goes here.</p>
</body>
<html>
7