xsl-list
[Top] [All Lists]

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

2009-06-10 13:37:11
Perfect, I think that's as simple as it gets.  I was going down that road 
yesterday evening but got distracted..

Many thanks to all.  

/thread

-----Original Message-----
From: Emmanuel Bégué [mailto:eb(_at_)medusis(_dot_)fr] 
Sent: Wednesday, June 10, 2009 1:28 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Sort by one attribute & use Muenchian technique to group by 
another attribute?

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(_dot_)Honnen(_at_)gmx(_dot_)de]
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>




--~------------------------------------------------------------------
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>
--~--


--~------------------------------------------------------------------
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>
--~--

<Prev in Thread] Current Thread [Next in Thread>