tags:

views:

272

answers:

11

Is the order that elements of a common parent appear in XML a meaningful piece of data captured by the XML document, or is order not specified as being meaningful? For example, consider the two XML documents:

<people>
 <person name="sam"/>
 <person name="juni"/>
</people>

and

<people>
 <person name="juni"/>
 <person name="sam"/>
</people>

Are these documents considered to represent identical data, or is the difference in order captured?

+1  A: 

There is a difference. You can use various XML API's to process elements in sequence or find an element by index. Of course order may not matter in your particular example, but that depends on the semantics of the data.

Martin Liversage
Yeah I am aware that many APIs preserve the order. But I wonder if this behavior is required by the XML specification.
landon9720
Each XML element can have a content model. The content models for your elements are very simple, but you can create very complex content models in XML involving sequences or lists of elements. The XHTML <html> element should contain a <head> and a <body> element in that order. In that sense the XML specification allows you to specify order. If you have a content model of a sequence (zero or more) <person> elements it is up to you if the order has any semantics.
Martin Liversage
+1  A: 

The order is captured.

John Saunders
+9  A: 

They are not identical - Whether the sequence matters is up to the program or user that processes it. For example, the sequence of elements in an XHTML document determine how they are displayed in a browser, and search engines use the position in the document to judge relative importance of the data.

l0b0
Yeah I knew XHTML would come up. I'm not sure that's a good example, because isn't XHTML not fully XML compliant?
landon9720
Absolutely it is. That's the raison d'etre of XHTML.
skaffman
XHTML is XML compliant, however XML is not XHTML compliant.
txwikinger
A: 

The order of the elements is irrelevant in XML. The consumers and producers may agree on a certain order but the XML documents above convey exactly the same information.

Otávio Décio
I strongly disagree. The order of elements in XML is significant and the two XML files don't carry the same information. The order of attributes on a given element, on the other hand is *not* significant.
Joachim Sauer
They are only significant to the producers and consumers. As a pure XML document they carry the exact same information.
Otávio Décio
Joachim is correct, all XML specification require that the order of elements be significant. The order of *attributes* however is not.
Adam Ruth
@ocdecia, the ordering of the elements *is* information, and *is* relevent. The XML Infoset W3C Standard includes the element order as part of the information definition.
skaffman
How can you determine the order of the very same element? You have "person" as the *only* element, it is not like you have person and address.
Otávio Décio
@ocdecio: in that sense all data in the XML is only significant to the producers and consumers. The pure XML document doesn't care about any of it. But it does *represent* both the information and the order.
Joachim Sauer
Here's my position, if I am given an XML document I would not trust its order, I have sorting functions to do that for me. Even if it is a set of bank transactions I would use one of the elements to indicate order, not their physical position. I do that for database tables, and to me this is no different.
Otávio Décio
Sorry, but facts beat opinions every time. The order of elements matters as much as the order of open) )
MSalters
@MSalters - not really, the order of elements is a semantic matter, the order of open/close tags is a syntatic matter.
Otávio Décio
I think the problem here is the dual purpose of xml. If xml is seen solely as a datastore (database) the order of equivalent elements seems quite irrelevant for the purpose of storage. However, xml can also be used for the purposes of presentation of the information contained. In that moment, order can matter.
txwikinger
@txwikinger - is it correct to say then that to get a different presentation (for example, different sorting order) we need to produce an entire new xml document? Wouldn't it be best left to a transformation process to do that instead?
Otávio Décio
@ocdecio - I think it depends. In another answer XHTML is mentioned. XHTML does not have such a tranformation process that would do that. Other applications of XML might have that. Take i.e. ajax, where the order could be determined before the response is sent, or after it is received, depending on the way the application is designed.
txwikinger
@txwikinger - that was what I tried to say in my answer - as pure XML documents, regardless of client applications, both documents posted by the OP carry the exact same information and are valid by the same DTD or schema. All else is semantics.
Otávio Décio
@ocdecio - Well, I was commenting more on the comments by others to your answer than your answer.
txwikinger
@txwikinger - thanks.
Otávio Décio
A: 

According to this article, the 1.0 version of the standard doesn't even require that parsers report siblings in the order they appear in the document. In that light, they would not be considered different, as both children are there. Perhaps this has changed, so that other answers are for newer versions of XML, though.

unwind
If you need to ensure order you can add 'index=' attributes to each entry
Martin Beckett
+8  A: 

Order of elements is significant in XML, so in your example the two documents are different. Attribute order is not significant, though.

<people>
  <person name="kathy" id="1"/>
</people>

This is exactly the same as:

<people>
  <person id="1" name="kathy"/>
</people>
Adam Ruth
A: 

I think those should be considered identical, but it's really up to the software or person reading it to decide. XML is just a way of writing out data. The application determines how that data is used and therefore much of the meaning.

If your application reads in all of the person elements and then alphabetizes them by name, then the order in the XML document is meaningless. If your application reads them in and assigns seats in the same order the people appear in the XML, then the order is very important.

It's up to the application that uses the data. If the order is important, it should be described in the specs for people generating the files.

Scott Saunders
The XML specification is explicit on this, the order of elements is significant.
Adam Ruth
+1  A: 

http://www.ibm.com/developerworks/xml/library/x-eleord.html

Maybe the discussion in the article will help to answer your question. Since your question is somewhat open, I am not sure if it covers you concerns.

txwikinger
+5  A: 

The order is potentially important, but it depends on what's being transmitted.

In XHTML, for example, the order is extremely important - if you had sibling paragraphs in a random order, it would be very confusing!

In many other cases, it's unimportant.

XML is just a way of representing a tree of nodes. XML itself says that the order is important: APIs must preserve the order, for example - but it's up to whatever produces/interprets the data to really care about the order or not.

The XML specification effectively has to "err on the side of ordering" - it's easy to ignore ordering if you don't care about it, but it's a pain to reconstruct ordering if APIs decide to switch things around. (You'd have to put the order into attributes etc.)

Jon Skeet
+1  A: 

While XML attribute ordering is not significant as far as the XML standard is concerned, the textual representation of XML does by necessity place the attributes in a specific order. This can be an issue for things like XML Signature, which generates a digital signature for XML documents. A different attribute order would generate a different signature, which clearly is wrong.

For this (and other) reasons, there is now a standard for XML Canonicalization, which defines rules for re-organising XML documents such that they retain the same information content, but have things like whitespace, namespace declarations and attributes rearranged in a predictable way.

From xml.com

Canonical XML requires the inclusion of namespace declarations and attributes in ascending lexicographic order.

skaffman
+1  A: 

The XML 1.0 Spec does not say anything about the order of elements with equal names as children of the same parent element. So it seems, the issue is undefined.

However, most XML parsers and APIs will preserve the sequence as given in the textual representation. So, it is possible to implement applications that care about the element order. And the de facto answer to your question is: Yes, the order matters. The two examples are different.

Looking more closely, you'll need to eveluate what your use case is. If your XML needs to interoperate with different (maybe 3rd party) applications, you should always assume that order matters. If you have total control on the producing and consuming application, then you might relax this rule.

As always, you'll have to judge.

mkoeller