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

August 10th, 2012, 03:44 AM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Dynamic sort key components on xsl:perform-sort
Hi,
I am trying to dynamically sort my data. I know that I can use something like:
Code:
<xsl:sort select="*[local-name() = 'whatever']"/>
to sort against a column I want. Now I have a sequence like this:
Code:
<xsl:variable name="sort-key-components" select="('field1', 'field2')"/>
and I need to generate:
Code:
<xsl:perform-sort select="myData">
<xsl:sort select="field1"/>
<xsl:sort select="field2"/>
</xsl:perform-sort>
But I don't see any way to generate the sort elemnts to do what I want :/ Any hints for me?
Thx,
Florian
|
|

August 10th, 2012, 04:36 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Well if you know the number of items in the sequence then you can code e.g.
Code:
<xsl:perform-sort select="myData">
<xsl:sort select="*[local-name() eq $sort-key-components[1]]"/>
<xsl:sort select="*[local-name() eq $sort-key-components[2]]"/>
</xsl:perform-sort>
I am not sure off the top of my head what happens if you just add as many sort elements as the maximum number of expected items in the sequence is e.g.
Code:
<xsl:perform-sort select="myData">
<xsl:sort select="*[local-name() eq $sort-key-components[1]]"/>
<xsl:sort select="*[local-name() eq $sort-key-components[2]]"/>
<xsl:sort select="*[local-name() eq $sort-key-components[3]]"/>
<xsl:sort select="*[local-name() eq $sort-key-components[4]]"/>
<xsl:sort select="*[local-name() eq $sort-key-components[5]]"/>
</xsl:perform-sort>
if you expect up to five such items in the sequence.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

August 10th, 2012, 04:39 AM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
That's my problem, I don't really know the length of the sequence. I could obviously add ten sort elemnts and hope that I never reach that much (which is realistic), but then I'd have like five sort elements which would sort by the empty sequence (). Will Saxon perform a useless sort then or leave the list as is for that key component?
|
|

August 10th, 2012, 11:32 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You've clearly never watched expert punched card operators at work
If you don't know the number of sort keys, then you can do multiple sorts. Sort first on the last sort key, then on the last but one, and so on. It's known in the trade as a digit-by-digit sort although of course the sort keys don't need to be single digits. Specify stable="yes" on the xsl:sort element in case this isn't the default for your implementation.
To do this you will need a template that performs sorting on the nth sort key where n is a parameter; the template should call itself to do the next sort except when n=1, at which point it terminates.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 10th, 2012, 12:32 PM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mhkay
You've clearly never watched expert punched card operators at work 
|
Small surprise there, I am decades to young for that stuff!
Quote:
|
If you don't know the number of sort keys, then you can do multiple sorts. Sort first on the last sort key, then on the last but one, and so on. It's known in the trade as a digit-by-digit sort although of course the sort keys don't need to be single digits. Specify stable="yes" on the xsl:sort element in case this isn't the default for your implementation.
|
Now this is embarrasing, I did think of doing that, but realized that starting with the first sort-key won't give me what I want, never thought about starting at the end, yikes
Quote:
|
To do this you will need a template that performs sorting on the nth sort key where n is a parameter; the template should call itself to do the next sort except when n=1, at which point it terminates.
|
Can I do this via apply-templates or only with call-template/function. I am having problems with how I should write the match condition for apply-templates so I can select the whole sequence instead of each row alone.
Thx for the pointers!
Florian
|
|

August 10th, 2012, 03:26 PM
|
|
Authorized User
|
|
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Just as reference, this is what I have now (working version using xsl:function). Maybe someone else who needs this finds it useful (that is while I try to get it working using apply-templates/call-templates ;)):
xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<sort-keys>
<key>field1</key>
<key>field2</key>
</sort-keys>
<rows>
<row>
<field1>z</field1>
<field2>a</field2>
</row>
<row>
<field1>z</field1>
<field2>b</field2>
</row>
<row>
<field1>a</field1>
<field2>a</field2>
</row>
</rows>
</data>
xsl:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:test="http://example.com/Test">
<xsl:output method="html" indent="yes" version="5.0"/>
<xsl:template match="data">
<html>
<body>
<table border="1">
<thead>
<tr>
<xsl:for-each select="sort-keys/key">
<th><xsl:value-of select="."/></th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="test:sort(rows/row, sort-keys/key/text())">
<tr>
<xsl:for-each select="*">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:function name="test:sort" as="element()*">
<xsl:param name="input"/>
<xsl:param name="keys"/>
<xsl:choose>
<xsl:when test="count($keys)">
<xsl:perform-sort select="test:sort($input, $keys[position() != 1])">
<xsl:sort select="*[local-name() = $keys[1]]" stable="yes"/>
</xsl:perform-sort>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="$input"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>
What I am not sure about is the xsl:sequence, is there a better option?
|
|
 |