title: How to copy all child nodes as-is to resulting tree?
body: <p>UPDATE (spoiler): This question is answered (see David Carlisle answere below) and it looks like a bug in XSLT implementation included in some versions in JRE (like it works in jdk but don't work on jre 1.6.0_20-b02 and not working at all at 1.6.0_31-b05). I logged a bug for that on Oracle site.</p>
  
  <p>I almost there and in terms of functionality it works now. But I'm not happy with some parts of it, some of them (I believe) could be shorter. This is the question... For details see below</p>
  
  <p><strong>This is an example of input xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;t1&gt;
      &lt;t2 a1="v1"&gt;
          &lt;ot1 a2="v2" /&gt;
          &lt;ot2 a3="v3"&gt;
              &lt;t3 a5="v4"&gt;
                  &lt;ot1 a2="v5" /&gt;
              &lt;/t3&gt;
          &lt;/ot2&gt;
      &lt;/t2&gt;
  &lt;/t1&gt;
  </code></pre>
  
  <p><strong>This is an example of expected result xml (see xslt file below, to get clue on whats-what)</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;t1&gt;
      &lt;t2 a1="v1"&gt;
          &lt;gt2 a="ot2" b="gtv1"&gt;
              &lt;gt1 a="ot1"&gt;v2&lt;/gt1&gt;
          &lt;/gt2&gt;
          &lt;gt2 a="ot2" b="v3"&gt;
              &lt;t3 a5="v4"&gt;
                  &lt;gt2 a="ot2" b="gtv1"&gt;
                      &lt;gt1 a="ot1"&gt;v5&lt;/gt1&gt;
                  &lt;/gt2&gt;
              &lt;/t3&gt;
          &lt;/gt2&gt;
      &lt;/t2&gt;
  &lt;/t1&gt;
  </code></pre>
  
  <p><strong>This is xslt I finally end up with (but not too happy).</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:exslt="http://exslt.org/common" version="1.0"
      exclude-result-prefixes="exslt"&gt;
      &lt;xsl:output method="xml" /&gt;
  
      &lt;xsl:template match="@*|node()"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*|node()" /&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="ot1|ot2"&gt;
          &lt;xsl:variable name="thisResult"&gt;
              &lt;xsl:apply-templates select="." mode="impl" /&gt;
          &lt;/xsl:variable&gt;
          &lt;xsl:apply-templates select="exslt:node-set($thisResult)" /&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="ot1" mode="impl"&gt;
          &lt;ot2 a3="gtv1"&gt;
              &lt;gt1 a="ot1"&gt;
                  &lt;xsl:value-of select="@a2" /&gt;
              &lt;/gt1&gt;
          &lt;/ot2&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="ot2" mode="impl"&gt;
          &lt;gt2 a="ot2" b="{@a3}"&gt;
              &lt;xsl:for-each select="child::*"&gt;
                  &lt;xsl:element name="{name()}"&gt;
                      &lt;xsl:copy-of select="@*|node()" /&gt;
                  &lt;/xsl:element&gt;
              &lt;/xsl:for-each&gt;
          &lt;/gt2&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>Question is: How to make this shorter?</strong></p>
  
  <pre><code>&lt;xsl:for-each select="child::*"&gt;
      &lt;xsl:element name="{name()}"&gt;
          &lt;xsl:copy-of select="@*|node()" /&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p><strong>I tried following things, but in this case part of resulting xml will be lost (specifically attributes for the gt2 element will be lost)</strong></p>
  
  <pre><code>&lt;xsl:copy-of select="node()"/&gt;
  </code></pre>
  
  <p>also tried</p>
  
  <pre><code>&lt;xsl:copy-of select="*"/&gt;
  </code></pre>
  
  <p>with no success</p>