xsl-list
[Top] [All Lists]

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

2008-12-10 13:31:26
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/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>