Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 11th, 2010, 03:00 PM
Authorized User
 
Join Date: Jul 2009
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Default Mess with xsl:sort

Hi,

Anyone plz helpw me with this wierd logic.
I have 3 keys as xsl:sort in my code .

key 1: abc
key 2 :def
key 3 :Date order=ascending

Seems like the third Date key sorts only if both abc and def are equal.It doesnt sort if def varies .I need the elements to be sorted finally on date in ascending order

Below is my input xml:

<Response>
<Map>
<Date>2010-12-09</Date>
<abc>14261</abc>
<def>00378400305</def>
</Map>
<Map>
<Date>2010-11-30</Date>
<abc>16817</abc>
<def>00008083322</def>
</Map>
<Map>
<Date>2010-11-06</Date>
<abc>14852</abc>
<def>68180021503</def>
</Map>
<Map>
<Date>2010-10-19</Date>
<abc>16817</abc>
<def>00093738598</def>
</Map>
<Map>
<Date>2010-09-28</Date>
<abc>20662</abc>
<def>68382002210</def>
</Map>
<Map>
<Date>2010-06-25</Date>
<abc>14851</abc>
<def>00781580610</def>
</Map>
<Map>
<Date>2010-06-25</Date>
<abc>20662</abc>
<def>68382002211</def>
</Map>
</
Response>

My code:

<xsl:template match="/">
<xsl:variable name="sortAll">
<xsl:for-each select="Response/Map">
<xsl:sort select="abc" data-type="number"/>
<xsl:sort select="def" data-type="number"/>
<xsl:sort select="translate(Date,'-','')" data-type="number" order="ascending"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<Response>
<xsl:copy-of select="$sortAll"/>
</Response>
</xsl:template>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<Response xmlns:fo="http://www.w3.org/1999/XSL/Format">
<Map>
<Date>2010-12-09</Date>
<abc>14261</abc>
<def>00378400305</def>
</Map>
<Map>
<Date>2010-06-25</Date>
<abc>14851</abc>
<def>00781580610</def>
</Map>
<Map>
<Date>2010-11-06</Date>
<abc>14852</abc>
<def>68180021503</def>
</Map>
<Map>
<Date>2010-11-30</Date>
<abc>16817</abc>
<def>00008083322</def>
</Map>
<Map>
<Date>2010-10-19</Date>
<abc>16817</abc>
<def>00093738598</def>
</Map>
<Map>
<Date>2010-09-28</Date>
<abc>20662</abc>
<def>68382002210</def>
</Map>
<Map>
<Date>2010-06-25</Date>
<abc>20662</abc>
<def>68382002211</def>
</Map>
</
Response>

Desired Output:
<?xml version="1.0" encoding="UTF-8"?>
<Response xmlns:fo="http://www.w3.org/1999/XSL/Format">
<Map>
<Date>2010-12-09</Date>
<abc>14261</abc>
<def>00378400305</def>
</Map>
<Map>
<Date>2010-06-25</Date>
<abc>14851</abc>
<def>00781580610</def>
</Map>
<Map>
<Date>2010-11-06</Date>
<abc>14852</abc>
<def>68180021503</def>
</Map>
<Map>
<Date>2010-10-19</Date>
<abc>16817</abc>
<def>00093738598</def>
</Map>
<Map>
<Date>2010-11-30</Date>
<abc>16817</abc>
<def>00008083322</def>
</Map>
<Map>
<Date>2010-06-25</Date>
<abc>20662</abc>
<def>68382002211</def>
</Map>
<Map>
<Date>2010-09-28</Date>
<abc>20662</abc>
<def>68382002210</def>
</Map>
</
Response>

I know if i dont sort with def i might get my desired output.But i dont want it to be removed.Please help thanks

 
Old December 11th, 2010, 07:36 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As you have observed, if you have three sort keys, then the second will be considered only when two items have duplicate values for the first key, and the third will be considered only when two items have duplicate values for the first two keys.

It looks to me as if you want to reorder the keys so Date is given more significance, but I can't work out from your example exactly what your requirements are.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort with XSL crazeydazey XSLT 10 November 17th, 2009 07:04 AM
xsl:sort Question kwilliams XSLT 6 July 18th, 2005 08:39 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
XSLT-- xsl:sort help... debo XSLT 2 December 3rd, 2004 04:25 PM
xsl:sort and IE 5.0 Issue babloo81 XSLT 1 March 17th, 2004 06:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.