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: "Newman, John W" <newmanjw@xxxxxxxx>
Date: Wed, 10 Jun 2009 13:31:48 -0400
Thank you James, that works great.  I see what you did there, very nice.  I
will adapt this to our actual xsl now and see how it goes.

Martin - Thanks for your approach as well, I am having a bit of trouble
getting the exsl ns to work through IE, but I'll keep at it.

Andrew - Welcome to my spam folder, KMA.



-----Original Message-----
From: James A. Robinson [mailto:jim.robinson@xxxxxxxxxxxx]
Sent: Wednesday, June 10, 2009 1:26 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Sort by one attribute & use Muenchian technique to group by
another attribute?


> Anyway, I could really use some help or guidance here.  I have two sets of
nodes that are intermixed - I need to output them ordered by a sort order
attribute, and group them by a key attribute such that the key only shows up
when it changes from the previously sorted item.  Here's a simplified
example:

My first though is that, if you have some control/knowledge of
what values are or are not legal in your grouping key (@key),
you could build your own grouping function:


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

  <xsl:output indent="no" method="text"/>

  <xsl:template match="/">

    <xsl:variable name="list">
      <xsl:for-each select="root/*">
        <xsl:sort select="@order" data-type="number" />
        <xsl:value-of select="concat(@order, ',', @key, '&#10;')" />
      </xsl:for-each>
    </xsl:variable>

    <xsl:call-template name="group">
      <xsl:with-param name="list" select="$list" />
      <xsl:with-param name="previous" select="''" />
    </xsl:call-template>

  </xsl:template>

  <xsl:key name="key-pair" match="*" use="concat(@order, ',', @key)"/>

  <xsl:template name="group">
    <xsl:param name="list" />
    <xsl:param name="previous" />

    <xsl:choose>
      <xsl:when test="$list = ''" />
      <xsl:otherwise>
        <xsl:variable name="key-pair" select="substring-before($list,
'&#10;')" />
        <xsl:variable name="current"  select="substring-after($key-pair, ',')"
/>
        <xsl:variable name="remaining" select="substring-after($list,
'&#10;')" />

        <xsl:choose>
          <xsl:when test="$previous = ''">
            <xsl:value-of select="concat($current,':&#10;')"/>
          </xsl:when>
          <xsl:when test="$previous != $current">
            <xsl:value-of select="concat($current,':&#10;')"/>
          </xsl:when>
        </xsl:choose>

        <xsl:value-of select="concat(key('key-pair', $key-pair)/@text,
'&#10;')"/>

        <xsl:call-template name="group">
          <xsl:with-param name="list" select="$remaining" />
          <xsl:with-param name="previous" select="$current" />
        </xsl:call-template>

      </xsl:otherwise>
    </xsl:choose>
   </xsl:template>

</xsl:stylesheet>


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       jim.robinson@xxxxxxxxxxxx
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)

Current Thread