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 August 10th, 2008, 07:43 PM
Authorized User
 
Join Date: Oct 2006
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
Default Forcing blank values to bottom of a sort

I'm a relative newbie to XSLT and am struggling with this problem. I have a sorted result set that begins as follows:

    <xsl:for-each select="Player">
    <xsl:sort select="GrossScore" data-type="number"/>

When GrossScore is blank, I'd like those result rows to be at the bottom, but otherwise I'd like the numerical values of GrossScore to be in the normal sequence.

It would be great to insert some kind of if statement to test for a blank and replace it with an arbitrarily large number like 999 for the sort operation, but I can't see any syntax that would work. I've also thought about trying to sort on the length>0 of characters in GrossScore first then use a second sort on the numeric value, but again what syntax?

Any ideas on cracking this would be greatly appreciated.


 
Old August 11th, 2008, 02:38 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can supply more than one xsl:sort element, so simply make the first one based on if the GrossScore element is blank (if this is the wrong order then add the order="descending" attribute):

<xsl:for-each select="Player">
    <xsl:sort select="GrossScore=''"/>
    <xsl:sort select="GrossScore" data-type="number"/>

/- Sam Judson : Wrox Technical Editor -/
 
Old August 11th, 2008, 06:58 AM
Authorized User
 
Join Date: Oct 2006
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
Default

That worked perfectly. Thanks a lot.

I thought there had to be some sort of elegant solution like that but hadn't been able to figure out the actual syntax.

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

Use two sort keys: the first one a boolean string-length(.)!=0 (false sorts before true), the second the actual string.

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
for-each loop and blank values Navy1991_1 XSLT 3 June 6th, 2008 08:24 AM
Handle Null or Blank values using xslt in csv data mums XSLT 1 April 3rd, 2008 07:30 PM
Dynamic sort order or sort datatype kapy_kal XSLT 2 September 18th, 2007 02:10 PM
To sort a hashmap using its keys and/or values Mr. Ram Java Basics 1 June 1st, 2006 11:19 PM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM





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