xsl:sort based on values from another scope

J

Jarle Presttun

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>
 
M

Marrow

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top