Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 10th, 2012, 03:44 AM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old August 10th, 2012, 04:36 AM
Friend of Wrox
Points: 6,516, Level: 34
Points: 6,516, Level: 34 Points: 6,516, Level: 34 Points: 6,516, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,218
Thanks: 0
Thanked 237 Times in 236 Posts
Default

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
Reply With Quote
  #3 (permalink)  
Old August 10th, 2012, 04:39 AM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default

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?
Reply With Quote
  #4 (permalink)  
Old August 10th, 2012, 11:32 AM
mhkay's Avatar
Wrox Author
Points: 18,240, Level: 58
Points: 18,240, Level: 58 Points: 18,240, Level: 58 Points: 18,240, Level: 58
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,925
Thanks: 0
Thanked 281 Times in 276 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old August 10th, 2012, 12:32 PM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
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
Reply With Quote
  #6 (permalink)  
Old August 10th, 2012, 03:26 PM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default

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?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort with XSL crazeydazey XSLT 10 November 17th, 2009 06:04 AM
Dynamic sort order or sort datatype kapy_kal XSLT 2 September 18th, 2007 02:10 PM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 08:12 AM
XSLT Dynamic xsl:sort Method kwilliams XSLT 2 July 20th, 2005 03:19 PM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM



All times are GMT -4. The time now is 03:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.