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 February 14th, 2006, 02:45 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL Sorting using variable

Hello, I have to post my question again because the first time I forgot (or I was to stupid) to explain someting.
Thank you very much to Joe and Michael Kay for answering my first question.

Assuming the following xml data:

<shop_articles>
  <article id="1" order="50" show="yes">
    <article_groups>
      <article_group>
        <name>Group1</name>
    <value>value of group1</value>
      </article_group>
      <article_group>
    <name>Quality</name>
    <value>good</value>
      </article_group>
      ...
    </article_groups>

    <article_price>400</article_price>
  </article>

  <article id...
  </article>
</shop_articles>

I am using a <xsl:for-each > instruction to loop through all articles. When I want to sort the articles by the price it works fine with <xsl:sort select="*[name()=$article_sorting]" /> after the <xsl:for-each> instruction when the variable $article_sorting contains the value 'article_price'.

But I don´t know how to set the variable $article_sorting when I want to sort the articles by the <value> of <article_group>´s with the <name>Quality</name>.

Any one have an idea?
Thanks you very much in advance.

Greetings, Wolfgang
 
Old February 14th, 2006, 03:18 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:sort select="*[name()=$article_sorting]" /> allow you to sort by the value any child element, but it doesn't allow you to supply a completely arbitrary sort key at run-time. If you want a completely general solution, consider <xsl:sort select="saxon:evaluate($x)"/> where $x can hold any XPath expression - the trouble is this isn't available in all products.

If you don't want a completely general solution but just one of a number of possible sort keys, you can try multiple sort keys:

<xsl:for-each...
<xsl:sort select="*[name()=$article_sorting]" />
<xsl:sort select="article_groups[$article_sorting='']/article_group[name="Quality"]/value"/>

If $article-sorting is "" the first sort key has no effect, if it is not "" then the second sort key has no effect.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 15th, 2006, 06:09 AM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael, thank you very much for your help, that was the solution for me. I know now a little bit more of XSL :)
Greetings Wolfgang
 
Old February 15th, 2006, 06:11 AM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That was what I was searching for.
Thank you very much Michael.
Greetings, Wolfgang
 
Old February 15th, 2006, 06:13 AM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael, thank you very much again.
That was what I was looking for.
Greetings, Wolfgang





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
xsl:variable holding name of an xsl:param perissos XSLT 0 December 5th, 2006 07:09 AM
The difference between xsl:variable and xsl:param NEO1976 XML 2 July 24th, 2006 06:05 AM
pass java variable to xsl variable kathy1016cats XSLT 1 June 14th, 2006 06:23 PM
Sorting with XSL mannabaron XSLT 2 February 14th, 2006 10:25 AM





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