[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Sort by one attribute & use Muenchian technique to

Subject: RE: Sort by one attribute & use Muenchian technique to group by another attribute?
From: "Newman, John W" <newmanjw@xxxxxxxx>
Date: Wed, 10 Jun 2009 13:36:40 -0400
RE:  Sort by one attribute & use Muenchian technique to
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 Bigui [mailto:eb@xxxxxxxxxx]
Sent: Wednesday, June 10, 2009 1:28 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE:  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.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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.