xsl:copy

  1. copy_1-input.xml

  2. <?xml version="1.0"?>
  3. <?myPI asdf234sd"?>
  4. <products>
  5.   <!-- Delta -->
  6.   <product id="p1" name="Delta" price="3250" stock="4"/>
  7.   <!-- Golf -->
  8.   <product id="p2" name="Golf" price="1000" stock="5"/>
  9.   <!-- Alpha -->
  10.   <product id="p3" name="Alpha" price="1200" stock="19"/>
  11. </products>
  1. copy_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  4.   <xsl:output indent="yes"/>
  5.   <xsl:template match="@*|node()">
  6.     <xsl:copy>
  7.       <xsl:apply-templates select="@*|node()"/>
  8.     </xsl:copy>
  9.   </xsl:template>
  10. </xsl:stylesheet>
  1. copy_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <?myPI asdf234sd"?>
  4. <products>
  5.   <!-- Delta -->
  6.   <product id="p1" name="Delta" price="3250" stock="4"/>
  7.   <!-- Golf -->
  8.   <product id="p2" name="Golf" price="1000" stock="5"/>
  9.   <!-- Alpha -->
  10.   <product id="p3" name="Alpha" price="1200" stock="19"/>
  11. </products>

Comment

The stylesheet contains the "identity" template, the most important of all templates. It walks through the input document node for node and recreate them using xsl:copy. Attributes are also nodes but "node()" in the above context means all nodes being children of nodes that is: element(), comment() and processing-instruction(). For that reason we need a union also including attributes with "@*".

The identity template is normally modified with additional templates of exceptions. Because they match more precisely they overrule the identity template for some specific nodes. See my tutorial: Identity Template: xsl:copy with recursion.

Updated 2009-03-19