The use of xsl:copy (Was RE: Retraction: I've modified my parser to copy attributes. )

Subject: The use of xsl:copy (Was RE: Retraction: I've modified my parser to copy attributes. )
From: Khun Yee Fung <KFung@xxxxxxxxxx>
Date: Wed, 12 Apr 2000 12:39:15 -0400
The <xsl:copy> element is actually a rather nice element to use in the cases
where you want to handle only part of an XML tree. The <xsl:copy> element
gives you the ability to not know much of the current node and yet allow you
to copy it to the result tree.

Suppose you have an XML document with an element and its descendents that
you want to remove. Suppose the name of the element is called 'remove'. You
want to keep the rest of the XML tree in the result tree.

These are the templates I use to perform that:

<xsl:template match='/'>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match='remove'/>

<xsl:template match='@* | node()'>
  <xsl:copy>
    <xsl:apply-templates select='@* | node()'/>
  </xsl:copy>
</xsl:template>

(xt says node() is not implemented in step patterns so the above templates
are no good for xt. Saxon and Xalan have no problem with it.)

The <xsl:copy> element is also not redundant in the specific example you
showed here. The element copies the current node to the result tree without
knowing much about what the current node is (according to the value of the
match attribute, it can be any kind of node). If the current node is an
element, <xsl:copy> will construct an element in the result tree. If it is
an attribute, <xsl:copy> will construct an attribute and put it inside the
containing element. The <xsl:copy> element handles the other types of nodes
too.*

Regards,
Khun Yee

* My version of Xalan (1.0.3) strips comments from the input tree when the
above templates are used. Saxon does not. Both do not strip processing
instructions. I wonder which behaviour is proper.


-----Original Message-----
From: Dan Morrison [mailto:dman@xxxxxxxx]

I've been directed to the spec 7.5:
-----------------------------------
For example, the identity transformation can be written using xsl:copy
as follows:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
------------------------------------

... which implies my construct is fine ( s/@/@*/ ) so apologies all.
I'd referred to only xsl:copy-of & xsl:attribute.

Isn't 'xsl:copy' a bit redundant?


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread