|
[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
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="' '"/> <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="' '"/> > > <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>
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








