sorting XML by multiple elements

Discussion in 'XML' started by R, May 30, 2006.

  1. R

    R Guest

    Hi All,

    I have problem with sorting nodes I want sort them by columns c1, c2
    also I have node <root><sort ActiveField="c0"/></root>

    ActiveField attribute is set by user.

    I'm sorting 'row' nodes, 'row' has c0, c1, c2, ..., cn child elements

    I wrote:
    <xsl:for-each select="row">
    <xsl:sort select="concat(/root/sort/@ActiveField, c1, c2)"/>
    // do somethning
    </xsl:for-each>

    but I doesn't work - is it allowed to use XPath inside concat function?

    how can it be achived to sort 'row' elements by multiple elements?

    thanks in advance for any help
    best regards
    R
     
    R, May 30, 2006
    #1
    1. Advertising

  2. R wrote:


    > <xsl:for-each select="row">
    > <xsl:sort select="concat(/root/sort/@ActiveField, c1, c2)"/>
    > // do somethning
    > </xsl:for-each>
    >
    > but I doesn't work - is it allowed to use XPath inside concat function?
    >
    > how can it be achived to sort 'row' elements by multiple elements?


    You can have multiple xsl:sort elements inside the xsl:for-each e.g.
    <xsl:for-each select="row">
    <xsl:sort select="c1" />
    <xsl:sort select="c2" />
    <!-- ... -->
    </xsl:for-each>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 30, 2006
    #2
    1. Advertising

  3. R

    R Guest

    thanks,

    If you could help me out with XPath expression in xsl:sort

    given
    <root><sort ActiveField="name"/></root>

    <xsl:for-each select="row">
    <xsl:sort select="/root/sort/@ActiveField" />
    <xsl:sort select="c1" />
    <xsl:sort select="c2" />
    <!-- ... -->
    </xsl:for-each>

    doesn't work - probably in place of /root/sort/@ActiveField string
    'name' is passed
    so every row is sorted by conts 'name' (not value of name element)
    and values of c1 and c2 elements

    when I hardcode the child element this way:

    <xsl:for-each select="row">
    <xsl:sort select="name" />
    <xsl:sort select="c1" />
    <xsl:sort select="c2" />
    <!-- ... -->
    </xsl:for-each>

    it works fine - how can the same result be achieved with
    /root/sort/@ActiveField attribute
    pointing to the name of the first sort element?

    thanks in advance for any hints
    best regards
    R
     
    R, May 30, 2006
    #3
  4. R

    R Guest

    sorry i've just found the solution:

    <xsl:sort select="*[name() = /root/sort/@ActiveField]"/>

    ;)

    best regards
    R
     
    R, May 30, 2006
    #4
  5. R wrote:


    > If you could help me out with XPath expression in xsl:sort
    >
    > given
    > <root><sort ActiveField="name"/></root>
    >
    > <xsl:for-each select="row">
    > <xsl:sort select="/root/sort/@ActiveField" />


    I think you want
    <xsl:sort select="*[local-name() = /root/sort/@ActiveField]" />





    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 30, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. P
    Replies:
    1
    Views:
    1,203
    Joe Kesselman
    Jul 7, 2006
  2. Ultrus
    Replies:
    4
    Views:
    2,851
    Stefan Behnel
    Aug 30, 2007
  3. Chris Seberino
    Replies:
    3
    Views:
    770
    Stefan Behnel
    Jun 12, 2009
  4. Paul
    Replies:
    12
    Views:
    189
    Robert Dober
    Aug 30, 2009
  5. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    513
    Michael Budash
    Oct 11, 2003
Loading...

Share This Page