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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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.

   
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.