RE: [xsl] Sorting the complete xml file

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

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

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

<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)
                                 string($a1) eq string($a2))"/>

Michael Kay

> -----Original Message-----
> From: Xuan Ngo [mailto:xuanngo2001@xxxxxxxxx] 
> Sent: 12 December 2005 16:15
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [xsl] 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:[xsl] + "subject line"
> Bye!
> Xuan Ngo
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 

Current Thread