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

RE: Sorting the complete xml file

Subject: RE: Sorting the complete xml file
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 12 Dec 2005 16:59:10 -0000
sort xml nodes by attribute
I'm sure I've already answered this question.

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*">
    <xsl:apply-templates select="*">
      <xsl:sort select="name()"/>
      <xsl:sort select="."/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

will sort the element nodes at every level, first by name and then by
content. But it won't normalize the order of attributes and it won't handle
mixed content.

You are using sorting as a means to an end, and it might be better to
concentrate on your real requirement, which is comparison. I'm using XSLT
2.0 here because it makes life so much easier. XSLT 2.0 gives you a
deep-equal() function that does much of the job for you, but it's also quite
possible to code it yourself (if you want slightly different logic, for
example). For example, you can compare two sets of attribute nodes like
this:

<xsl:function name="compare-attributes" as="xs:boolean">
  <xsl:param name="element1" as="element()"/>
  <xsl:param name="element2" as="element()"/>
  <xsl:variable name="A1" select="$element1/@*"/>
  <xsl:variable name="A2" select="$element1/@*"/>
  <xsl:sequence select="count($a1) eq count($a2)
                        and every $a1 in $A1 satisfies
                              some $a2 in $A2 satisfies
                                (node-name($a1) eq node-name($a2)
                                   and
                                 string($a1) eq string($a2))"/>
</xsl:function> 

Michael Kay
http://www.saxonica.com/



> -----Original Message-----
> From: Xuan Ngo [mailto:xuanngo2001@xxxxxxxxx] 
> Sent: 12 December 2005 16:15
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE:  Sorting the complete xml file
> 
> Thanx for your comprehensive explanation about the serializer.
> 
> >I'm afraid I don't really understand what you mean by "the 
> left side of a tag".
> 
> Example:
> <node attributes="AttributeValues">values</node>
> <nodeX attributesX="AttributeValues">valuesX</nodeX>
> 
> 1-Sort all nodes name on the SAME LEVEL AND then
> 2-Sort attributes names AND then
> 3-Sort value of node AND then repeat the from step 1 to 3 to 
> lower level nodes.
> 
> Well, I will assume that getting the differences between 2 
> xml file is a hard problem and can't be
> resolved with XSL.
> 
> On the side note, I think that the mailing list cut my email. 
> In the previous email, I also ask
> how to correctly reply to the mailing list.
> Anyway, here is the question again:
> How to reply to the mailing list so that it is nested to the 
> correct person? I am using YahooMail
> to reply. I only have control over the "TO" & "Subject" fields.
> Currently, what I am doing is:
> TO = xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject = RE: + "subject line"
> 
> 
> Bye!
> Xuan Ngo
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 

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.