Datasheet
Williams c01.tex V3 - 07/31/2009 2:53pm Page 9
Chapter 1: First Steps with XSLT
<xsl:template match
=
"p">
<p><xsl:apply-templates/></p>
</xsl:template>
In the next template you match the XML source <
attr>
(attribute) and <
element>
names using the
XPath union operator
"|"
, and output a containing
<code>
literal result element. The union operator
performs a logical
OR
, matching either of the source element names:
<xsl:template match
=
"attr | element">
<code>
<xsl:value-of select
=
"."/>
</code>
</xsl:template>
The output for an element name will look like this:
<p>An element occurring as a child of the
<code>xsl:stylesheet</code> element is called a
declaration. These top-level elements are all optional, and may
occur zero or more times.
</p>
Copying Content
When content in both the source and the output should be identical, you can simply copy the source
nodes to the result. With the <
xsl:copy>
instruction, you copy the source
<code>
element name and its
content to the output:
<xsl:template match
=
"code">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Here is the schema deļ¬nition:
<xs:element name
=
"copy" substitutionGroup
=
"xsl:instruction">
<xs:complexType>
<xs:complexContent mixed
=
"true">
<xs:extension base
=
"xsl:sequence-constructor">
<xs:attribute name
=
"copy-namespaces" type
=
"xsl:yes-or-no" default
=
"yes"/>
<xs:attribute name
=
"inherit-namespaces" type
=
"xsl:yes-or-no" default
=
"yes"/>
<xs:attribute name
=
"use-attribute-sets" type
=
"xsl:QNames" default
=
""/>
<xs:attribute name
=
"type" type
=
"xsl:QName"/>
<xs:attribute name
=
"validation" type
=
"xsl:validation-type"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
There are two copy instructions in XSLT. The
<xsl:copy>
instruction is a shallow copy,andcopies
only the context node, but nothing under it. You specify the output in the sequence constructor inside
<xsl:copy>.
This instruction is most useful when copying element nodes.
9