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 June 9th, 2006, 02:10 PM
Registered User
 
Join Date: Jun 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Customized sorting

Hi, all

I want to implement sorting of the following piece of xml.
Sorting key should be, depending on previously defined variable:
- policy/@name or
- policy/description or
- policy/@created or even
- rule[@id = policy/rule/@id]/@name (I didn't tested this expression for correctness, it's just to explain the idea)

So I need an expression that selects correct expression depending on variable/param value.
I tried this:

<xsl:param name="sorted-by-column" >1</xsl:param>
<xsl:variable name="sorting-expression">
  <xsl:choose>
    <xsl:when test="$sorted-by-column = 1">
      <xsl:value-of select="@name"/>
    </xsl:when>
    <xsl:when test="$sorted-by-column = 2">
      <xsl:value-of select="./description"/>
    </xsl:when>
    ...
  </xsl:choose>
</xsl:variable>
...
<xsl:for-each select="policy">
  <xsl:sort select="$sorting-expression" order="{$sorting-order}"/>
</xsl:for-each>

...and this:
<xsl:sort select="{$sorting-expression}" order="{$sorting-order}"/>

It gives me errors from 'Invalid XPath expression' to 'Node cannot be an expression'.


XML:
<rule id="22" name="Name1"/>
<rule id="19" name="Name2"/>
...
<policy id="1" name="AA" created="1133388000" modified="1141920863">
  <description>BBB</description>
    <rule1 id="19"/>
    <rule2 id="22"/>
    <rule3 id="25"/>
</policy>
<policy id="2" name="BB" created="1133388000" modified="1141920863">
...
</policy>
 
Old June 9th, 2006, 02:53 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0 you can do

xsl:sort select="if ... then policy/@name
                 else if ... then policy/description
                 else ...

It's not so easy in 1.0. Some of the entries in Dave Pawson's FAQ relate to this problem:

http://www.dpawson.co.uk/xsl/sect2/N6461.html#d8741e455

If you've got a processor that offers xx:evaluate() (e.g. Saxon) then you can put the XPath expression into a string variable $sort-key and do xsl:sort select="saxon:evaluate($sort-key)".

Another technique is

<xsl:sort select="policy/@name[$condition=1]"/>
<xsl:sort select="policy/description[$condition=2]"/>
<xsl:sort select="policy/@created[$condition=3]"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 10th, 2006, 05:09 AM
Registered User
 
Join Date: Jun 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Michael! This is of greate help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent help - Customized Windows login Rehanrana C++ Programming 0 December 26th, 2007 08:15 AM
Opening a customized page Snuffles ASP.NET 2.0 Basics 8 April 1st, 2007 10:53 AM
Get your customized software aprababu C# 0 November 15th, 2006 07:57 AM
customized skin parag.nikhal Classic ASP Basics 0 December 19th, 2005 10:19 AM
Urgent help about sending customized summary Elain Access 1 December 8th, 2005 02:37 PM





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