简体   繁体   中英

order of elements in XML important?

I was discussing the requirements of a function that converts an XML to JSON and then back to XML.

Assuming I have the following XML and no XML schema:

 <pets>
 <pet1>dog</pet1> 
 <pet2>cat</pet2>
 </pets>

In JSON an object is a set of unordered elements. Therefore both jsons are equal and would be a valid output of an xml2json converter.

{
  "pets": 
    {
     "pet1": "dog",
     "pet2": "cat"
    }
}


{
  "pets": 
    {
     "pet2": "cat",
     "pet1": "dog"
    }
}

Therefore when converting back from json to xml I might end up with

 <pets>
 <pet2>cat</pet2>
 <pet1>dog</pet1> 
 </pets>

I only found this questions but they ask for elements of the same type. Therefore, I have some questions.

  • Are both XML documents to be considered equal?
  • Is there any authoritative source regarding the order of elements in xml?
  • Would a DOM handle both XML documents as equal?

Are both XML documents to be considered equal?

Not in general XML terms. A particular XML application might not give the order significance.

Is there any authoritative source regarding the order of elements in xml?

The XML spec doesn't appear to make explicit mention of it, but references the SGML specification which I'm not going to spend the money on to prove the point.

Consider, however, any XHTML document. If you scramble the order if the child nodes then it is unlikely to make sense.

Would a DOM handle both XML documents as equal?

No, it would express the elements in different orders

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM