Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xsl:sort based on values from another scope

Reply
Thread Tools

xsl:sort based on values from another scope

 
 
Jarle Presttun
Guest
Posts: n/a
 
      07-09-2003
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>

 
Reply With Quote
 
 
 
 
Marrow
Guest
Posts: n/a
 
      07-09-2003
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>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
CSPEC issue: lossing scope (or incorrect scope) in cspec subroutine. balldarrens@gmail.com Perl Misc 0 02-05-2009 08:42 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments