 |
| 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
|
|
|
|

December 31st, 2007, 01:34 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorting number, but omit null
I have an xml file that some of the nodes do not contain a child that I use to sort.
<priceModule>
<row>
<name>Item 1</name>
<priceCol>600</priceCol>
</row>
<row>
<name>Item 2</name>
</row>
</priceModule>
I need to sort by priceCol, but omit the rows that do not contain a priceCol from the sort.
Thanks,Victor
--
Victor Corey
__________________
--
Victor Corey
|
|

December 31st, 2007, 02:03 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Do you use XSLT 1.0 or 2.0?
And what exactly should happen with elements that do not have a priceCol, do you not want to process them at all or do you want to process them after the elements which have a priceCol?
|
|

December 31st, 2007, 02:15 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I am using xslt 2.0.
Right now if I sort by priceCol:
<xsl:sort order="ascending" select="priceCol" data-type="number" />
It sorts correctly except it puts the items without that node at the top. I need these at the bottom.
Thanks,
Victor
--
Victor Corey
|
|

December 31st, 2007, 02:20 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Add a second sort key:
<xsl:sort order="ascending" select="empty(priceCol)">
<xsl:sort order="ascending" select="number(priceCol)"/>
false sorts before true, so nodes with no priceCol will sort at the end.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

December 31st, 2007, 02:24 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I get this error:
'empty' is not a valid XSLT or XPath function. -->empty(priceCol)<--
--
Victor Corey
|
|

December 31st, 2007, 02:33 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
empty is defined in XPath 2.0, see http://www.w3.org/TR/xpath-functions/#func-empty
Therefore I am not sure why you get that error as you stated you use XSLT 2.0 (where all XPath 2.0 functions are available).
|
|

December 31st, 2007, 02:33 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You said you were using XSLT 2.0, it seems that you were mistaken. XSLT 1.0 doesn't allow sorting of booleans, but you can achieve the same effect using select="not(priceCol)" which generates the sort keys "false" and "true" as strings.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

December 31st, 2007, 02:39 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have this at the top of my xslt <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">, But I'm very new to it.
Would empty evaluate if the node does not even exist, not just empty.
Thanks,
Victor
--
Victor Corey
|
|

December 31st, 2007, 02:41 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, I must be using 1.0, becuase not(priceCol) worked. Thank you.
I just started using xslt and continue to learn how powerful it is. Thanks for you help.
Why am I using 1.0 when I put 2.0 in the stylesheet directions?
Thanks,
Victor
--
Victor Corey
|
|

December 31st, 2007, 02:45 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Whether XSLT 2.0 is supported depends on the XSLT processor you use, not on the version attribute.
Current XSLT 2.0 processors are Saxon, Gestalt, and Altova.
If you use an XSLT 1.0 processor to run an XSLT stylesheet with version="2.0" then the processor tries to run it in forwards compatible mode http://www.w3.org/TR/xslt#forwards.
|
|
 |