Re: [xsl] How to sort a $variable and store that into another $variable ?

Subject: Re: [xsl] How to sort a $variable and store that into another $variable ?
From: "Jean-Philippe Martin" <jeanph01@xxxxxxxxx>
Date: Wed, 10 Dec 2008 14:05:24 -0500
Ok.
I'll try to make this understandable.

My need is :
- extract a list of values
- make it unique
- make it sorted
- put it on many columns in a html table

My XML document is big. I create an unordered list of unique values
with this call  (the first // is important since the info can be
anywhere in the xml tree) :

<xsl:variable name="unsortedList"
select="//category[@defid=3183315]//*[@attid=49]/node()[not(.=following::*)]"
/>


I put this list in a variable because i want to make a multi-column
html table with the values. And the technique I used needs a variable

  <table style="width:100%; ">
    <xsl:variable name="unsortedList"
select="//category[@defid=3183315]//*[@attid=49]/node()[not(.=following::*)]"
/>
    <xsl:variable name="nbCol" select="ceiling(count($unsortedList) div 5)"/>

    <xsl:for-each select="($unsortedList)[position() &lt;= $nbCol]">
      <!--xsl:sort select="."/-->
      <xsl:variable name="here" select="position()"/>
      <tr>
        <xsl:call-template name="TDcompetence">
          <xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*0]"/>
        </xsl:call-template>

        <xsl:call-template name="TDcompetence">
          <xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*1]"/>
        </xsl:call-template>

        <xsl:call-template name="TDcompetence">
          <xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*2]"/>
        </xsl:call-template>

        <xsl:call-template name="TDcompetence">
          <xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*3]"/>
        </xsl:call-template>

        <xsl:call-template name="TDcompetence">
          <xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*4]"/>
        </xsl:call-template>
      </tr>
    </xsl:for-each>
  </table>


The sort is in comments because it do not work since I access the
value with a position into the $unsortedList variable directly and not
through the for-each which could sort.

Thank you.

On Wed, Dec 10, 2008 at 13:30, Martin Honnen <Martin.Honnen@xxxxxx> wrote:
> Jean-Philippe Martin wrote:
>
>> I'm trying to sort a <xsl:variable> containing 22 nodes and store this
>> sorted list into another xsl:variable.
>>
>> Is this possible ?
>
>
>> Is there a way to do this ?
>>
>> Ps: this is xsl 1.0.
>
> There is a way if you show us your XML input and how exactly you initialize
> your first variable.
>
> Assuming the XML input document is
>
> <root>
>  <foo>10</foo>
>  <foo>2</foo>
>  <foo>1</foo>
>  <foo>4</foo>
> </root>
>
> then this stylesheet
>
> <xsl:stylesheet
>  version="1.0"
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
>  <xsl:output indent="yes"/>
>
>  <xsl:template match="/">
>    <xsl:variable name="v1" select="root/foo"/>
>    <xsl:variable name="v2">
>      <xsl:for-each select="$v1">
>        <xsl:sort select="." data-type="number"/>
>        <xsl:copy-of select="."/>
>      </xsl:for-each>
>    </xsl:variable>
>    <result>
>      <xsl:copy-of select="$v2"/>
>    </result>
>  </xsl:template>
>
> </xsl:stylesheet>
>
> outputs
>
> <result>
>   <foo>1</foo>
>   <foo>2</foo>
>   <foo>4</foo>
>   <foo>10</foo>
> </result>
>
> Variable v1 has the foo elements from the XML input, variable v2 a result
> tree fragment of the sorted foo elements.
>
> --
>
>        Martin Honnen
>        http://JavaScript.FAQTs.com/

Current Thread