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

RE: generic sort based on attribute names

Subject: RE: generic sort based on attribute names
From: "Chaitanya Desai" <cdesai@xxxxxxxxxxx>
Date: Tue, 25 Jan 2005 15:40:28 -0800
xsl sort based on variable
Hey Joris, 
Thanks for your help. But I am still not getting the desired output. 
This is what I get: 
Input:
<root>
	<e b="bb" y="yy">
		<node x="123" a="123"/>
		<node x="999" a="999" b="999"/>
	</e>
	<e z="zz" a="aa"/>
</root>

Output:
<root>
    <e b="bb" y="yy">
        <node a="123" x="123"/>  
        <node a="999" b="999" x="999"/>
	</e>
    <e a="aa" z="zz"/>
</root>
Basically it is only sorting attributes within an element, not elements within a set of siblings. 
Thanks.


-----Original Message-----
From: Joris Gillis [mailto:roac@xxxxxxxxxx] 
Sent: Tuesday, January 25, 2005 1:33 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re:  generic sort based on attribute names

Tempore 21:48:58, die 01/25/2005 AD, hinc in  
xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Chaitanya Desai  
<cdesai@xxxxxxxxxxx>:

> OK,
> This is the recursive stylesheet I have, to handle a generic XML.
> How does the thing you explained with the 'e' fit here.

This is an example of how it could be used;


<xsl:template match="*">
	<xsl:variable name="sortkey">
		<xsl:for-each select="*">
			<e>
				<xsl:for-each select="@*">
					<xsl:sort select="name(.)"/>
					<xsl:value-of select="name()"/>
				</xsl:for-each>
			</e>
		</xsl:for-each>
	</xsl:variable>
	<xsl:copy>
		<xsl:for-each select="@*">
			<xsl:sort select="name(.)"/>
			<xsl:copy-of select="."/>
		</xsl:for-each>	
		<xsl:apply-templates>
					<!-- the select for this sort needs to be the concatenated name-value  
pairs of
	all the attributes sorted in alphabetical order -->
			<xsl:sort  
select="xx:node-set($sortkey)//e[position()=count(current()/preceding-sibling::*)]"/>
		</xsl:apply-templates>
	</xsl:copy>
</xsl:template>



It will convert an input like this:
<root>
	<e b="bb" y="yy">
		<node x="123" a="123"/>
		<node x="999" a="999" b="999"/>
	</e>
	<e z="zz" a="aa"/>
</root>

to this:
<root>
	<e a="aa" z="zz"/>
	<e b="bb" y="yy">
		<node a="999" b="999" x="999"/>
		<node a="123" x="123"/>
	</e>
</root>


For each element in the source doument, it creates a table of the  
attributes of all child elements and uses this table (stored in the  
'sortkey' variable) to perform the sort.

e.g. when the 'root' element is matched, the 'sortkey' variable contains:
<e>by</e>
<e>az</e>

because these entries follow the document order, you can use them as  
sortkey for the child elements by matching the position:
xx:node-set($sortkey)//e[position()=count(current()/preceding-sibling::*)]

(please keep in mind that this is an XSLT1.0 solution. The XSLT2.0  
solutions of Michael Kay and Dimitre Novatchev are of course better if  
your XSLT processor can handle them)

regards,
-- 
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
"CB&CB9CB;CB.CB:CB?CB?CB= CB5CB9CB=CB1CB9 CB<CB1CB;CB;CB?CB= CB7 Cb CB9CB;CECB;CB1CB;CB?CB="  -  
CE!CB;CB5CECB2CB?Cb&CB;CB?Cb

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
--~--

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.