XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Wei Wei TanSubject: Assign Ranking to Sorted Number
Author: Wei Wei Tan
Date: 14 Feb 2007 03:31 AM
Originally Posted: 14 Feb 2007 03:05 AM
Hi all,

Does anyone has experience on assigning ranking to sorted number using xslt? Any help/reference given is very much appreciated. Thanks.

XML File
================================
<countries>
<country><name>UK</name><number>55</number></country>
<country><name>US</name><number>40</number></country>
<country><name>Singapore</name><number>55</number></country>
<country><name>China</name><number>20</number></country>
<country><name>Taiwan</name><number>20</number></country>
<country><name>Canada</name><number>70</number></country>
<country><name>Japan</name><number>70</number></country>
</countries>

Intended OUTPUT - ascending sort
================================
Rank - Number - Country
1 - 20 - China
1 - 20 - Taiwan
2 - 40 - US
3 - 55 - Singapore
3 - 55 - UK
4 - 70 - Canada
4 - 70 - Japan

XSLT
================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" />

<xsl:key name="sort" match="country" use="number"/>
<xsl:variable name="list" select="countries/country[generate-id(.) = generate-id(key('sort', number))]"/>

<xsl:template match="/">
<xsl:call-template name="process-list">
<xsl:with-param name="rank" select="1" />
<xsl:with-param name="position" select="1" />
<xsl:with-param name="counter" select="0" />
</xsl:call-template>
</xsl:template>

<xsl:template name="process-list">
<xsl:param name="rank" />
<xsl:param name="position" />
<xsl:param name="counter" />
<xsl:choose>
<!-- process new number -->
<xsl:when
test="$counter = count($list[number =
$list[$rank]/number]) - 1">
<xsl:apply-templates select="$list[$rank]">
<xsl:with-param name="rank" select="$rank" />
</xsl:apply-templates>
<xsl:call-template name="process-list">
<xsl:with-param name="rank" select="$rank" />
<xsl:with-param name="position" select="$position + 1" />
<xsl:with-param name="counter" select="1" />
</xsl:call-template>
</xsl:when>
<!-- process the rest of the current number -->
<xsl:otherwise>
<xsl:apply-templates select="countries/country[number =
$list[$rank]/number][$counter + 1]">
<xsl:with-param name="rank" select="$rank" />
</xsl:apply-templates>
<xsl:choose>
<!-- goto next number -->
<xsl:when test="$counter = count(countries/country[number =
$list[$rank]/number])">
<!-- test if there are more numbers -->
<xsl:if test="$rank &lt; count($list)">
<xsl:call-template name="process-list">
<xsl:with-param name="rank" select="$rank + 1" />
<xsl:with-param name="position" select="$position" />
<xsl:with-param name="counter" select="0" />
</xsl:call-template>
</xsl:if>
</xsl:when>
<!-- goto next in this number -->
<xsl:otherwise>
<xsl:call-template name="process-list">
<xsl:with-param name="rank" select="$rank" />
<xsl:with-param name="position" select="$position + 1" />
<xsl:with-param name="counter" select="$counter + 1" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="country">
<xsl:param name="rank" />
<xsl:value-of select="$rank" />
<xsl:text> - </xsl:text>
<xsl:value-of select="number" />
<xsl:text> - </xsl:text>
<xsl:value-of select="name" />
<xsl:text>; </xsl:text>
</xsl:template>

</xsl:stylesheet>

OUTPUT from Above XSLT
Problem - I am not able to sort the number in ascending/descending before assigning the ranking.
================================
1 - 55 - UK;
1 - 55 - Singapore;
2 - 40 - US;
3 - 20 - China;
3 - 20 - Taiwan;
4 - 70 - Canada;
4 - 70 - Japan;

Thank you in advance,
Wei


Documenttest(17).xml
xml file

Documenttest(15).xsl
xsl file

Posttop
Tony LavinioSubject: Assign Ranking to Sorted Number
Author: Tony Lavinio
Date: 15 Feb 2007 08:23 AM
General XSLT questions that aren't Stylus Studio-specific are best
asked on the xsl-list run by Mulberry Technologies.

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.