[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: sort elements based on attribute value

Subject: RE: sort elements based on attribute value
From: "Mac Martine" <email@xxxxxxxxxxxxxxxx>
Date: Thu, 27 Mar 2003 14:57:09 -0800
sort elements
I recently had the same question. The thread is below.

========================================

Hi,

I haven't looked at the table formatting, but hopefully this will start
you on your way...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>

  <xsl:template match="/">
    <xsl:apply-templates select="//*[@task]" mode="sorted-tasks">
      <xsl:sort select="@task"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="*" mode="sorted-tasks">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>

Note that I use match="*" and a mode in the second template to avoid
having to specify the match pattern (//*[@task]) in two places which
would be a maintenance nightmare if the stylesheet were much bigger.

Rgds,

Dan.


-----Original Message-----
From: Mac Martine [mailto:email@xxxxxxxxxxxxxxxx]
Sent: 03 March 2003 19:33
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  :xsl matching/sorting recursively by attribute:



Hello-
 I am trying to match all elements with an attribute 'task' recursively
throughout the tree. I got that part working, but I have been unable to
get it 'sorted' how I want. I want all recursive elements with a 'task'
attribute to be sorted by the value of that attribute, and displayed.

So a simple xml file for this might look like:

<desktop id="desktop">
	<lineEdit task="3"/>
	<panel id="main">
		<panel id="toolbar">
			<button task="2">Back</button>
			<button task="1">Foward</button>
		</panel>
	</panel>
</desktop>

So, next is the xsl. This is as close I have been able to get.... Can
anyone help solve this one? Thanks a lot! -Mac

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
	<body>
		<table width="80%">
			<xsl:apply-templates />
		</table>
	</body>
</xsl:template>

<xsl:template match="*">

	<xsl:for-each select="*[@task>0]">
		<xsl:sort select="@task"/>
		<xsl:call-template name="tasks"/>
	</xsl:for-each>

	<xsl:apply-templates select='*'/>
</xsl:template>

<xsl:template name="tasks">

		<tr>
			<td width="5%">
				<xsl:value-of select="@task"/>
			</td>
		</tr>

</xsl:template>

</xsl:stylesheet>

-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Challa,
Harsha
Sent: Thursday, March 27, 2003 2:12 PM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject:  sort elements based on attribute value


Hi Gurus 


how can i sort a list of elements based on the an ascending value of an
attribute 

<elem value=3>

</elem>


<elem value=2>

</elem>


i need to sort the elements basd on attribute



gracias
 
















 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.