开发者

Should xml represent a set or a list?

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?


  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.


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜