xsl-list
[Top] [All Lists]

Re: [xsl] [XSLT2] Some common, generic grouping problems

2006-06-30 02:31:40
On 6/30/06, David Carlisle <davidc(_at_)nag(_dot_)co(_dot_)uk> wrote:

oops I got the first one wrong, still in some places in the world it's
not midnight,someone else can fix it before I wake up:-)

This one solves the first one:

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

<xsl:output indent="yes"/>

<xsl:template match="root">
<root>
<xsl:for-each-group select="*" group-starting-with="*[(_at_)color='dark-red']">
  <xsl:choose>
    <xsl:when test="self::*[(_at_)color='dark-red']">
                <xsl:for-each-group select="current-group()"
group-ending-with="*[(_at_)color='light-red']">
                        <xsl:choose>
                                <xsl:when 
test="current-group()[last()][self::*[(_at_)color='light-red']]">
                                        <red>
                                                <xsl:copy-of 
select="current-group()"/>
                                        </red>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:copy-of select="."/>
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:for-each-group>
    </xsl:when>
    <xsl:otherwise>
        <xsl:copy-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each-group>
</root>
</xsl:template>
</xsl:stylesheet>

A pair of nested for-each-groups, gives this result:

<root>
  <arbitrary/>
  <red>
     <elem color="dark-red"/>
     <elem color="red"/>
     <arbitrary color="none"/>
     <elem color="red"/>
     <elem color="light-red"/>
  </red>
  <arbitrary/>
</root>

cheers
andrew

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--