Subject: XSL Sorting using variable
Posted By: mannabaron Post Date: 2/14/2006 1:45:23 PM
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
Reply By: mhkay Reply Date: 2/14/2006 2:18:10 PM
<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
Reply By: mannabaron Reply Date: 2/15/2006 5:09:57 AM
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
Reply By: mannabaron Reply Date: 2/15/2006 5:11:43 AM
That was what I was searching for.
Thank you very much Michael.
Greetings, Wolfgang
Reply By: mannabaron Reply Date: 2/15/2006 5:13:20 AM
Hi Michael, thank you very much again.
That was what I was looking for.
Greetings, Wolfgang

Go to topic 40039

Return to index page 370
Return to index page 369
Return to index page 368
Return to index page 367
Return to index page 366
Return to index page 365
Return to index page 364
Return to index page 363
Return to index page 362
Return to index page 361