Datasheet

Williams c01.tex V3 - 07/31/2009 2:53pm Page 8
Chapter 1: First Steps with XSLT
Selecting Source Content
To output source content, you select from the element to be transformed, using the <
xsl:value-of>
instruction. The
select
attribute defines the XPath expression to use. The next code snippet shows
how:
<h1 class
=
"section"><xsl:value-of select
=
"title"/></h1>
As you learned in the introduction to this book, the
<xsl:value-of>
element is a sequence constructor,
which is a series of XSLT instructions. This is the schema declaration:
<xs:element name
=
"value-of" substitutionGroup
=
"xsl:instruction">
<xs:complexType>
<xs:complexContent mixed
=
"true">
<xs:extension base
=
"xsl:sequence-constructor">
<xs:attribute name
=
"select" type
=
"xsl:expression"/>
<xs:attribute name
=
"separator" type
=
"xsl:avt"/>
<xs:attribute name
=
"disable-output-escaping" type
=
"xsl:yes-or-no" default
=
"no"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
Processing Specific Source Elements
The processor’s built-in template rules have a lower priority than other templates, so by adding rules for
individual elements, you can override the defaults.
Now you can add specific templates for the structural elements in the XML source:
<title>
, <
purpose>
,
<
usage>
,and<
p>
.The
match
attribute identifies the element, and the output is specified with literal
result elements. The
select
attribute value
"."
for the
<title>
element is an XPath expression that
refers to the current node being processed. Because both the
<purpose>
and the
<usage>
elements can
contain paragraphs, we apply processing to all the
<p>
content and its inline markup.
These templates are located at the top level like the main template you have just written, but their order
is not significant. The XSLT processor treats the source elements in document order and will look in the
templates for matches as it goes. I generally put them in rough document order in simple stylesheets:
<xsl:template match
=
"title">
<h1>
<xsl:value-of select
=
"."/>
</h1>
</xsl:template>
<xsl:template match
=
"purpose">
<h2>Purpose</h2>
<xsl:apply-templates select
=
"p"/>
</xsl:template>
<xsl:template match
=
"usage">
<h2>Usage</h2>
<xsl:apply-templates select
=
"p"/>
</xsl:template
8