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

Re: [xsl] Alternate sort order of subelements based on previous parent element


Subject: Re: [xsl] Alternate sort order of subelements based on previous parent element
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Thu, 01 Sep 2005 09:13:54 +0200

Hi,
Tempore 03:00:25, die 09/01/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Philipp Kursawe <LukeSky77@xxxxxxx>:

I have run into a problem solving the following sophisticated (at least for
me) task:
There is a list of items each containing two nodes "row" and "col". First
the list has to be ordered ASCENDING by "rows". Next step is to sort the
list items of each "row" by its "col" node where the ordering direction
changes when the row is different to the previous.

I guess the easiest way is to group the 'item' elements by row and then alternate their sort order:


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

<xsl:output method="xml" indent="yes"/>

<xsl:key name="item" match="item" use="@row"/>

<xsl:template match="items">
<xsl:copy>
<xsl:for-each select="item[generate-id(key('item',@row))=generate-id()]">
	<xsl:sort select="@row" data-type="number"/>
	<xsl:variable name="order">
		<xsl:choose>
			<xsl:when test="position() mod 2=0">ascending</xsl:when>
			<xsl:otherwise>descending</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:comment><xsl:value-of select="$order"/></xsl:comment>
	<xsl:for-each select="key('item',@row)">
		<xsl:sort select="@col" order="{$order}" data-type="number"/>
		<xsl:copy-of select="."/>
	</xsl:for-each>
</xsl:for-each>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
B+There are only 10 types of people in this world. Those who understand binary, and those who don'tB;


Current Thread