In XML, is order important?

后端 未结 11 2197
无人共我
无人共我 2020-11-28 11:25

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 examp

相关标签:
11条回答
  • 2020-11-28 12:22

    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.

    0 讨论(0)
  • 2020-11-28 12:24

    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.

    0 讨论(0)
  • 2020-11-28 12:25

    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.

    0 讨论(0)
  • 2020-11-28 12:26

    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.

    0 讨论(0)
  • 2020-11-28 12:28

    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.

    0 讨论(0)
提交回复
热议问题