http://www.w3.org/TR/xslt#copying
7.5 Copying
<!-- Category: instruction -->
<xsl:copy
use-attribute-sets =qnames>
<!-- Content:template-->
</xsl:copy>
Thexsl:copy
element provides an easy way of copying the current node. Instantiating thexsl:copy
element creates a copy of the current node. The namespace nodes of the current node are automatically copied as well, but the attributes and children of the node are not automatically copied. The content of thexsl:copy
element is a template for the attributes and children of the created node; the content is instantiated only for nodes of types that can have attributes or children (i.e. root nodes and element nodes).
Thexsl:copy
element may have ause-attribute-sets
attribute (see[7.1.4 Named Attribute Sets]). This is used only when copying element nodes.
The root node is treated specially because the root node of the result tree is created implicitly. When the current node is the root node,xsl:copy
will not create a root node, but will just use the content template.
For example, the identity transformation can be written usingxsl:copy
as follows:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/> <!-- 这里是把apply-templates的内容嵌入中当前拷贝的结点之内 -->
</xsl:copy>
</xsl:template>
When the current node is an attribute, then if it would be an error to usexsl:attribute
to create an attribute with the same name as the current node, then it is also an error to usexsl:copy
(see[7.1.3 Creating Attributes withxsl:attribute
]).
The following example shows howxml:lang
attributes can be easily copied through from source to result. If a stylesheet defines the following named template:
<xsl:template name="apply-templates-copy-lang"> <xsl:for-each select="@xml:lang"> <xsl:copy/> </xsl:for-each> <xsl:apply-templates/> </xsl:template>
then it can simply do
<xsl:call-template name="apply-templates-copy-lang"/>
instead of
<xsl:apply-templates/>
when it wants to copy thexml:lang
attribute.