473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

xsl:sort based on values from another scope

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>

Jul 20 '05 #1
1 1897
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>

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Derek Tinney | last post by:
Hi, I'm having difficulty building an XLST file that allows me to sort a list of log records. I put together an XSL file that allows me to output a copy of the input file and then I attempted...
2
by: R | last post by:
Hello everybody. I was sorting all my XML data with for-each and sort. But there were few cases that I didn't want to sort my data. so I added nosort attribute - if given and set to '1' don't...
3
by: Tjerk Wolterink | last post by:
I posted my problem earlier, but i simplified the examples, and i know what the cause of the problem is, but i dont know the solution, my xml file: <?xml version="1.0" encoding="ISO-8859-1"?>...
6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
1
by: aerotops | last post by:
Hi, I am trying to sort something using XSLT. I am going to give examples. Original.xml <Root> <Car> <Name>Ford</Name> <DealerRating>3</DealerRating>
0
by: Nicolas | last post by:
Hi, I've been working on this problem for a couple of days and now i have no more ideas. first, this problem only happens with jdk 1.5, everything was fine with jdk1.4. I'm using a...
2
by: jobooker | last post by:
I'm having issues sorting. The short description is, how do I set the select attribute of xsl:sort to be the value of an xsl:variable? The longer description follows: What I want to do is to be...
3
by: davisford | last post by:
Hi, If I have something like this: <parents> <parent name="foo"> <children> <child>fred</child> <child>barney</child> <child>wilma</child>
1
by: =?Utf-8?B?SWxsdXN0cmlz?= | last post by:
I'm hoping someone can help me with what looks like a very simple problem. My output does not sort the languages alphabetically as I would expect. Can someone inspect my XSLT please? TIA!! ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.