0

I have an XML like this

 <root>
    <aree fbc="123" ad="12" b="123"/>
    <beee c="12" a="144"/>
</root>

Need to sort names of attributes by alphabet order. Like this:

 <root>
    <aree ad="12" b="123" fbc="123"/>
    <beee  a="144" c="12"/>
</root>

I have an xQuery code, but it don't work right. Help with it please. I need to replace to real name of node

select @data.query('<root>{for $i in root/* return <aaa> {for $j in $i/@* order by local-name($j) return  $j} </aaa> }</root>')
Ivan
  • 85
  • 6
  • 1
    Really? Order of attributes is important? – Backs May 23 '17 at 00:59
  • @Backs: [**Indeed, XML attribute order is not important**](https://stackoverflow.com/a/44124620/290085). – kjhughes May 23 '17 at 02:02
  • @kjhughes i know :) – Backs May 23 '17 at 02:15
  • 1
    @Backs: I know you know. :) Now we have to convince Ivan. – kjhughes May 23 '17 at 02:25
  • 1
    It is possible to order the attributes alphabetically, but this will not be preserved, if you don't store the result as string (you shouldn't). Whatever you do here, there is no guarantee to get the attributes in the same order the next time. In most cases this is done to get XML documents compareable on string level or to create a hash to check for manipulations. Besides the attribute's order there's also no guarantee about insignificant whitespaces. [Read about the limits here](https://msdn.microsoft.com/en-us/library/ms187107(v=sql.90).aspx) – Shnugo May 23 '17 at 08:00

1 Answers1

1

The XML Recommendation says that the the order of XML attributes is insignificant:

Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

Therefore, XML tools generally do not care about attribute ordering, and unless you are concerned with XML normalization/canonicalization1, neither should you.

1For those rare circumstances, see the section on attribute processing in the XML Normalization Recommendation or the Canonical XML Recommendation.

kjhughes
  • 106,133
  • 27
  • 181
  • 240