tags:

views:

64

answers:

3

I always think of xml like a set data structure. Ie:

<class>
 <person>john</person>
 <person>sarah</person>
</class>

Is equivalent to:

<class>
 <person>sarah</person>
 <person>john</person>
</class>

Question One: Are these two things logicly equivalant?


Are you allowed to make things like this in xml?

<methodCall>
  <param>happy</param>
  <param>sad</param>
</methodCall>

Or do you need to do it like this:

<methodCall>
  <param arg="1">happy</param>
  <param arg="2">sad</param>
</methodCall>

Question Two: Are these two things logically equivalent?


Question Three: Is xml usually treated like a set or a list?

+4  A: 
  1. They are logically equivalent, if you don't care about sort orders.
  2. Both are legal XML. They are not logically equivalent as the second sample has additional information.
  3. XML is treated as a hierarchy, not a set or list.

XML can represent a list and it can represent a set. It is not structured to be either. It is not an error to have multiple identical items, so in that respect it is not set based.

Oded
+1 @sixtyfootersdude Side note: XML has something called "document order". The order of nodes is relevant in XML, so even if "logical equivalence" (which is a matter of interpretation anyway) exists in samples 1.1 and 1.2, the documents are not *acutally* equivalent (@Oded's point #1).
Tomalak
+1  A: 

XML nodes constitute a list, not a set, though to be more accurate, they're actually more of a tree-like structure. Consider the fact that you can do this in XML:

<example>
  <foo/>
  <foo/>
</example>

Obviously if it was a set, the last /example/foo element would not be accessible as the first /example/foo element already exists in the set. However, regardless of whether they appear to be the same, they are in fact different nodes, meaning that they cannot be represented using a set without some extra work whereas a list is a natural structure to represent the nodes of an XML document (as long as they're at the same "height" in the tree).

Dustin
+1  A: 

This may be better split into three separate questions, but anyway:

1) The order of elements is important in XML, but it may not be to your application. Consider XML representing a book: clearly the order of paragraphs is paramount. As such, the XML in your second example is fine.

2) They are not equivalent XML documents, but your application may choose to see them as equivalent. Or not. XML is a format for describing things; it is up to applications to interpret them. You may want to look into XML Schema for ways to validate documents.

3) Elements can be duplicated in XML and their order is important. However a given document schema can enforce rules on uniqueness and/or ordering. Therefore, both sets and lists can be represented. I suppose you could consider raw XML an ordered list by default.

Adrian Mouat