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: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Wed, 10 Dec 2008 19:30:55 +0100
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