RE: [xsl] Moving an element to a new location in the Result-tree

Subject: RE: [xsl] Moving an element to a new location in the Result-tree
From: david_n_bertoni@xxxxxxxxxx
Date: Wed, 7 Sep 2005 18:38:25 -0700
> I want to move the former "b1" elements down the tree and re-name them
> (all) along the way, yet maintain the actual data they contain ("XYZ1",
> etc.).  The main thing is to move them down there, not re-name them. 

Remember that you are not "moving" anything -- you're creating a whole new 
result tree.  That may seem to be a trivial detail, but it's really a 
fundamental concept you need to remember always.

If the whitespace-only text nodes are not significant, then the following 
stylesheet will do what I think you want:

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
  <xsl:copy>
     <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
 
<xsl:template match="b1">
  <new_b1>
     <xsl:apply-templates select="@* | node()"/>
  </new_b1>
</xsl:template>
 
<xsl:template match="document1">
  <document222>
    <xsl:apply-templates select="*[not(self::b1)]"/>
    <xsl:apply-templates select="b1"/>
  </document222>
</xsl:template>

</xsl:stylesheet>

If you really need to preserve whitespace-only text nodes, things get a 
bit trickier.

Dave

Current Thread