xsl:sort based on values from another scope

Discussion in 'XML' started by Jarle Presttun, Jul 9, 2003.

  1. Hi,

    Sometimes I display values by combining information from different
    sections in the xml, like I do with gradeText in the student template
    bellow. Is it possible to sort on gradeText when I loop over students,
    as shown in the example? If not, I would have to modify some sql's.

    Thanks, Jarle.

    XML:
    <root>
    <grades>
    <grade id="0">
    <gradeText>A</gradeText>
    <gradeValue>6</gradeValue>
    </grade>
    <grade id="1">
    <gradeText>B</gradeText>
    <gradeValue>5</gradeValue>
    </grade>
    <grade id="2">
    <gradeText>C</gradeText>
    <gradeValue>4</gradeValue>
    </grade>
    </grades>
    <students>
    <student>
    <name>Peter</name>
    <gradeId>2</gradeId>
    </student>
    <student>
    <name>Laura</name>
    <gradeId>0</gradeId>
    </student>
    <student>
    <name>John</name>
    <gradeId>1</gradeId>
    </student>
    </students>
    </root>

    XSL:
    <xsl:template match="students">
    <table>
    <tr>
    <td>Name</td>
    <td>Grade</td>
    </tr>
    <xsl:apply-templates select="student">
    <!-- Would like to sort on gradeText here -->
    </xsl:apply-templates>
    </table>
    </xsl:template>

    <xsl:template match="student">
    <xsl:variable name="gradeId" select="gradeId"/>
    <tr>
    <td>
    <xsl:value-of select="name"/>
    </td>
    <td>
    <xsl:value-of select="/*/grades/grade[@id = $gradeId]/gradeText"/>
    </td>
    </tr>
    </xsl:template>
     
    Jarle Presttun, Jul 9, 2003
    #1
    1. Advertising

  2. Jarle Presttun

    Marrow Guest

    Hi Jarle,

    You could do...

    <xsl:sort select="/root/grades/grade[@id = current()/gradeId]/gradeText"/>

    Or you could set up a key, e.g.
    <xsl:key name="kGrade" match="grades/grade" use="@id"/>
    and then use the key in the sort, e.g.
    <xsl:sort select="key('kGrade',gradeId)/gradeText"/>

    Hope this helps
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator


    "Jarle Presttun" <jarle@NOSPAM_inspera.no> wrote in message
    news:1057746615.269512@gurney...
    > Hi,
    >
    > Sometimes I display values by combining information from different
    > sections in the xml, like I do with gradeText in the student template
    > bellow. Is it possible to sort on gradeText when I loop over students,
    > as shown in the example? If not, I would have to modify some sql's.
    >
    > Thanks, Jarle.
    >
    > XML:
    > <root>
    > <grades>
    > <grade id="0">
    > <gradeText>A</gradeText>
    > <gradeValue>6</gradeValue>
    > </grade>
    > <grade id="1">
    > <gradeText>B</gradeText>
    > <gradeValue>5</gradeValue>
    > </grade>
    > <grade id="2">
    > <gradeText>C</gradeText>
    > <gradeValue>4</gradeValue>
    > </grade>
    > </grades>
    > <students>
    > <student>
    > <name>Peter</name>
    > <gradeId>2</gradeId>
    > </student>
    > <student>
    > <name>Laura</name>
    > <gradeId>0</gradeId>
    > </student>
    > <student>
    > <name>John</name>
    > <gradeId>1</gradeId>
    > </student>
    > </students>
    > </root>
    >
    > XSL:
    > <xsl:template match="students">
    > <table>
    > <tr>
    > <td>Name</td>
    > <td>Grade</td>
    > </tr>
    > <xsl:apply-templates select="student">
    > <!-- Would like to sort on gradeText here -->
    > </xsl:apply-templates>
    > </table>
    > </xsl:template>
    >
    > <xsl:template match="student">
    > <xsl:variable name="gradeId" select="gradeId"/>
    > <tr>
    > <td>
    > <xsl:value-of select="name"/>
    > </td>
    > <td>
    > <xsl:value-of select="/*/grades/grade[@id =

    $gradeId]/gradeText"/>
    > </td>
    > </tr>
    > </xsl:template>
    >
     
    Marrow, Jul 9, 2003
    #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. nobody
    Replies:
    0
    Views:
    539
    nobody
    Jun 1, 2004
  2. R

    scope of xsl:sort

    R, Mar 24, 2005, in forum: XML
    Replies:
    2
    Views:
    661
    David Carlisle
    Mar 24, 2005
  3. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  4. Replies:
    2
    Views:
    9,279
    Martin Honnen
    Sep 5, 2006
  5. Navin
    Replies:
    1
    Views:
    704
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page