Re: [xsl] move tag up to level

Subject: Re: [xsl] move tag up to level
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 29 Sep 2006 12:28:40 +0100
Don't think in terms of "closing tags" or "moving tags" anywhere.
XSLT does not work with tags, and thinking in those terms just makes it
harder to use XSLT.

What you want to do is take the children of a and group them into groups
of adjacent nodes that are either b elements or not b elements.
b elements you just want to copy, and each group of not-b elements you
want to place as a child of an a element. 

So you use for-each-group gruping on a boolean test for a node being a b
element, like so:



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  
  <xsl:template match="*">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

 <xsl:template match="a">
   <xsl:for-each-group select="node()" group-adjacent="exists(self::b)">
     <xsl:choose>
       <xsl:when test="self::b">
	 <xsl:apply-templates select="current-group()"/>
       </xsl:when>
       <xsl:otherwise>
	 <a>   
	   <xsl:copy-of select="../@*"/>
	   <xsl:apply-templates select="current-group()"/>
	 </a>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:for-each-group>
</xsl:template>


</xsl:stylesheet>

Current Thread