[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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: Wed, 31 Jan 2007 15:46:03 GMT

>       <!-- how to match g, h and i as well within the same
>       "for-each-group"? -->

Two ways.

I used a grouping key of exists(self::b|self::c) so it only has two
states to group or not to group so like that you can only group one
thing at a time, but that's OK as you can have more than one pass.

   <xsl:template match="flatsequence">
  <hierarchy>
    <xsl:variable name="p1">
    <xsl:for-each-group
         select="*" group-adjacent="exists(self::b|self::c)"> ...
   </xsl:variable>
    <xsl:for-each-group
         select="$p1/*"
  group-adjacent="exists(self::h|self::h|self::i)"> ... 
    </xsl:for-each-group>
     </hierarchy>
   </xsl:template>

Or you do it in one pass with a grouping key that has three states (and
an xsl:choose with three branches.


   <xsl:template match="flatsequence">
  <hierarchy>
 <xsl:for-each-group
         select="*" group-adjacent="if (exists(self::b|self::c)) then 1
                                    if (exists(self::h|self::i|self::i)) then 2
                                    else 0"> 
   <xsl:choose>
    <xsl:when test="current-grouping-key()=1">
      <group1><xsl:copy-of select="current-group()"/>...
    <xsl:when test="current-grouping-key()=2">
      <group2><xsl:copy-of select="current-group()"/>...
    <xsl:otherwise>
   <xsl:copy-of select="current-group()"/>..


David


Current Thread