The xsl:copy-of Element

The <xsl:copy-of > Element

The <xsl:copy-of> element enables you to make a deep copy of nodes, which means that not just the node, but all attributes and descendents are copied as well. This element has one attribute:

  • select (mandatory). The node or node set you want copied.

This element is empty, and takes no content.

Heres an example that shows how this works; in this case, Ill replace the <xsl:for-each> element in Listing 3.10 with an <xsl:copy-of> element that specifically selects all attributes of the context element to copy:

Listing 3.11 Using <copy-of >
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:output method="xml"/>      <xsl:template match="*">          <xsl:copy>              <xsl:copy-of select="@*"/>              <xsl:apply-templates/>          </xsl:copy>      </xsl:template>  </xsl:stylesheet> 

This works as the previous example did, copying all elements and attributes. On the other hand, I dont need to modify the previous example in Listing 3.10 at all; I can simply use <xsl:copy-of> to copy the entire document by matching the root node and copying all descendents of that node like this:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:output method="xml"/>      <xsl:template match="/">          <xsl:copy-of select="*"/>      </xsl:template>  </xsl:stylesheet> 

You can also use <xsl:copy-of> to copy particular nodes and their descendents instead of matching the wildcard *. For example, the following rule copies all <MASS> elements and their descendents:

 <xsl:template match="MASS">          <xsl:copy-of select="."/>      </xsl:template> 

For that matter, I can replace a <MASS> element with a <DAY> element like this:

 <xsl:template match="MASS">      <xsl:copy-of select="DAY"/>  </xsl:template> 


Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net