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

RE: [xsl] Sort by one attribute & use Muenchian technique to group by another attribute?


Subject: RE: [xsl] Sort by one attribute & use Muenchian technique to group by another attribute?
From: Emmanuel Bégué <eb@xxxxxxxxxx>
Date: Wed, 10 Jun 2009 19:27:58 +0200

Hello,

Martin's solution is perfect as a two-pass approach, which needs the
node-set extention in 1.0; here is another solution that is maybe a
little clunky, but does not use the node-set extension and is a
one-pass approach:

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

  <xsl:variable name="nl" select="'&#13;&#10;'"/>
  <xsl:key name="prev" match="red|blue" use="@order"/>

  <xsl:template match="/root">
    <xsl:apply-templates select="red|blue">
      <xsl:sort select="@order" data-type="number"/>
      </xsl:apply-templates>
    </xsl:template>

  <xsl:template match="red|blue">
    <xsl:variable name="prev" select="key('prev',@order - 1)/@key"/>
      <xsl:if test="not($prev) or @key!=$prev">
        <xsl:value-of select="concat(@key,':')"/>
        <xsl:value-of select="$nl"/>
        </xsl:if>
      <xsl:value-of select="@text"/>
      <xsl:value-of select="$nl"/>
    </xsl:template>

  </xsl:stylesheet>

This does not have muh to do with the Muenchian method; it just sorts
once (using xsl:sort) and then, for each element that is output, looks
if the @key is different from the 'preceding' element's @key.

But since the real 'preceding' element would be in document order, it
re-computes another 'preceding' element based on the @order attribute.

This only works if @order attributes are strictly incremented by
one (and are numbers).

Hope this helps,
Regards,
EB


PS: this .



> -----Original Message-----
> From: Martin Honnen [mailto:Martin.Honnen@xxxxxx]
> Sent: Wednesday, June 10, 2009 6:47 PM
>
> Here is an XSLT 1.0 stylesheet that use the exsl:node-set extension
> function. As far as I know Xalan supports that or at least has an
> extension function in its own namespace that has the same result:
>
> <xsl:stylesheet
>    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>    xmlns:exsl="http://exslt.org/common"
>    exclude-result-prefixes="exsl"
>    version="1.0">
>
>    <xsl:output method="text"/>
>
>    <xsl:param name="nl" select="'&#13;&#10;'"/>
>
>    <xsl:template match="root">
>      <xsl:variable name="sorted-elements">
>       <root>
>        <xsl:for-each select="red | blue">
>          <xsl:sort select="@order" data-type="number"/>
>          <xsl:copy-of select="."/>
>        </xsl:for-each>
>       </root>
>      </xsl:variable>
>      <xsl:apply-templates
> select="exsl:node-set($sorted-elements)/root/*[1]"/>
>    </xsl:template>
>
>    <xsl:template match="blue | red">
>      <xsl:if test="not(preceding-sibling::*[1]/@key = @key)">
>        <xsl:value-of select="concat(@key, $nl)"/>
>      </xsl:if>
>      <xsl:value-of select="concat(@text, $nl)"/>
>      <xsl:apply-templates select="following-sibling::*[1]"/>
>    </xsl:template>
>
> </xsl:stylesheet>


Current Thread
Keywords