XSL - conditional sort

Discussion in 'XML' started by Andy Chambers, Jan 12, 2009.

  1. Hi,

    I'd like to sort a nodelist using a sort key or just leave the
    nodelist in document order if any of the nodes lack the specified sort
    key. Is it possible to do this using just the select attribute on an
    xsl:sort?

    For example given the following input....

    Example A:
    <nodes>
    <node rank="3">c</node>
    <node rank="">a</node>
    <node rank="2">b</node>
    </nodes>

    Example B:
    <nodes>
    <node rank="3">c</node>
    <node rank="1">a</node>
    <node rank="2">b</node>
    </nodes>

    For the input in Example A, I'd want to process the nodes in document
    order. For the input in Example B, I'd want to process the nodes
    according to their @rank attribute. What's the best way to acheive
    this? Can it (or should it) be done without resorting to a xsl:choose
    that checks whether all the @rank attributes have been set before
    sorting?

    Many Thanks,
    Andy
    Andy Chambers, Jan 12, 2009
    #1
    1. Advertising

  2. Andy Chambers wrote:

    > I'd like to sort a nodelist using a sort key or just leave the
    > nodelist in document order if any of the nodes lack the specified sort
    > key. Is it possible to do this using just the select attribute on an
    > xsl:sort?


    I think you need an xsl:choose e.g.
    <xsl:choose>
    <xsl:when test="/nodes/node/@rank[not(normalize-space())]">
    <xsl:apply-templates select="/nodes/node"/>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:apply-templates select="/nodes/node">
    <xsl:sort select="@rank" data-type="number"/>
    </xsl:apply-templates>
    </xsl:eek:therwise>
    </xsl:choose>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 12, 2009
    #2
    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. Alec S.
    Replies:
    10
    Views:
    10,116
    Alec S.
    Apr 16, 2005
  2. nobody
    Replies:
    0
    Views:
    534
    nobody
    Jun 1, 2004
  3. Replies:
    1
    Views:
    3,595
    A. Bolmarcich
    May 27, 2005
  4. Replies:
    2
    Views:
    9,259
    Martin Honnen
    Sep 5, 2006
  5. Navin
    Replies:
    1
    Views:
    684
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page