Re: [xsl] Wrap changing element sequence into container: with 'for-each-group'?

Subject: Re: [xsl] Wrap changing element sequence into container: with 'for-each-group'?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 30 Jan 2007 17:37:12 GMT
> 2) I fail to see how I can repeatedly group some children, when in 
> between are some children that are not to be grouped. Do I need to apply 
> recursion when working my way through the children?

not in xslt2, no just use for-each-group

It's so much easier if you provide an example, failing that I'll provide
one:-)

If I understand you correctly you want to take

<x>
<abc>1</abc>
<foo>bbb</foo>
<abc>b</abc>
<xyz>b</xyz>
<xyz>c</xyz>
<foo>bbb</foo>
<zzz/>
<hhh/>
<xyz>d</xyz>
</x>


and group adjacent runs of abc and xyz to produce


$ saxon8 wrap.xml wrap.xsl 
<?xml version="1.0" encoding="UTF-8"?>
<x>
   <wrap>
      <abc>1</abc>
   </wrap>
   <foo>bbb</foo>
   <wrap>
      <abc>b</abc>
      <xyz>b</xyz>
      <xyz>c</xyz>
   </wrap>
   <foo>bbb</foo>
   <zzz/>
   <hhh/>
   <wrap>
      <xyz>d</xyz>
   </wrap>
</x>


(using copy-of rather than apply-templates for simplicity)


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  
<xsl:output indent="yes"/>

  <xsl:template match="x">
    <x>
      <xsl:for-each-group select="*" group-adjacent="exists(self::xyz|self::abc)">
	<xsl:choose>
	  <xsl:when test="current-grouping-key()">
	    <wrap>
	      <xsl:copy-of select="current-group()"/>
	    </wrap>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:copy-of select="current-group()"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:for-each-group>
    </x>
  </xsl:template>
</xsl:stylesheet>

David

Current Thread