在 XML 中,顺序重要吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1131495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In XML, is order important?
提问by Landon Kuhn
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:
公共父元素在 XML 中出现的顺序是 XML 文档捕获的有意义的数据片段,还是未指定的顺序有意义?例如,考虑两个 XML 文档:
<people>
<person name="sam"/>
<person name="juni"/>
</people>
and
和
<people>
<person name="juni"/>
<person name="sam"/>
</people>
Are these documents considered to represent identicaldata, or is the difference in order captured?
这些文档是否被视为代表相同的数据,或者是否捕获了顺序差异?
回答by Adam Says - Reinstate Monica
Order of elements is significant in XML, so in your example the two documents are different. Attribute order is not significant, though.
元素的顺序在 XML 中很重要,因此在您的示例中,这两个文档是不同的。不过,属性顺序并不重要。
<people>
<person name="kathy" id="1"/>
</people>
This is exactly the same as:
这与以下内容完全相同:
<people>
<person id="1" name="kathy"/>
</people>
回答by l0b0
They are not identical - Whether the sequence mattersis 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.
它们并不相同 - 顺序是否重要取决于处理它的程序或用户。例如,XHTML 文档中元素的顺序决定了它们在浏览器中的显示方式,搜索引擎使用文档中的位置来判断数据的相对重要性。
回答by Jon Skeet
The order is potentiallyimportant, but it depends on what's being transmitted.
顺序可能很重要,但它取决于传输的内容。
In XHTML, for example, the order is extremelyimportant - if you had sibling paragraphs in a random order, it would be very confusing!
例如,在 XHTML 中,顺序非常重要——如果您有随机顺序的同级段落,那将会非常混乱!
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 reallycare about the order or not.
XML 只是表示节点树的一种方式。XML 本身说顺序很重要:例如,API 必须保留顺序 - 但它取决于生成/解释数据的任何东西是否真正关心顺序。
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.)
XML 规范实际上必须“在排序方面犯错”——如果您不关心排序,很容易忽略排序,但如果 API 决定改变顺序,则重建排序会很痛苦。(您必须将订单放入属性等中)
回答by Luke Puplett
From purely an XML validity point of view, it depends on the schema, if any, that describes what the rules are for formatting the XML.
纯粹从 XML 有效性的角度来看,它取决于描述用于格式化 XML 的规则的模式(如果有的话)。
That said, order must be preserved (see 2.1.1 in http://www.w3.org/TR/xml-infoset/) but whether it's "important" to an application is up to its author.
也就是说,必须保留顺序(参见http://www.w3.org/TR/xml-infoset/ 中的2.1.1 ),但它对应用程序是否“重要”取决于其作者。
Within a schema, order can be made unimportant with the use of the xs:allcompositor, though I'm not sure that this impacts the preservation of captured order, i.e. I'd still expect the order at origin/serialization to be maintained by XML processors.
在模式中,使用xs:all合成器可以使顺序变得不重要,尽管我不确定这会影响捕获顺序的保存,即我仍然希望 XML 处理器维护原始/序列化的顺序。
However, in 1.0 this compositor adds a restriction on the child elements so that they must occur 0 or 1 times. In XSD 1.1, this restriction is removed, making it easier to contractually specify that order is not important, effectively xs:allhas become the unordered version of xs:sequence.
但是,在 1.0 中,此合成器对子元素添加了限制,因此它们必须出现 0 次或 1 次。在 XSD 1.1 中,删除了此限制,从而更容易在合同中指定顺序不重要,实际上xs:all已成为xs:sequence.
Because of the overly-restrictive 1.0 xs:allcompositor, the ordered xs:sequencecompositor had to be used. Thus, order was often artificially imposed.
由于过度限制 1.0xs:all合成器,xs:sequence必须使用有序合成器。因此,秩序常常是人为地强加的。
Adoption of 1.1 by vendors of validator libraries is slow.
验证器库供应商采用 1.1 的速度很慢。
As such, APIs need to consider order when being evolved with new elements. I am not aware of any XML serialization framework that can work with 1.1 and you have to assume your clients will be using 1.0 and will validate the 1.1 incoming messages with 1.0 rules and choke.
因此,API 在使用新元素进行演进时需要考虑顺序。我不知道任何可以与 1.1 一起使用的 XML 序列化框架,您必须假设您的客户将使用 1.0,并将使用 1.0 规则和阻塞来验证 1.1 传入消息。
Luke
卢克
回答by skaffman
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.
虽然就 XML 标准而言,XML 属性排序并不重要,但 XML 的文本表示必须按特定顺序放置属性。这可能是 XML 签名之类的问题,它为 XML 文档生成数字签名。不同的属性顺序会生成不同的签名,这显然是错误的。
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.
由于这个(和其他)原因,现在有一个 XML 规范化标准,它定义了重新组织 XML 文档的规则,以便它们保留相同的信息内容,但以可预测的方式重新排列空格、命名空间声明和属性等内容.
From xml.com
来自xml.com
Canonical XML requires the inclusion of namespace declarations and attributes in ascending lexicographic order.
规范 XML 要求按字典序升序包含命名空间声明和属性。
回答by unwind
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.
根据这篇文章,该标准的 1.0 版本甚至不要求解析器按照它们在文档中出现的顺序报告兄弟姐妹。从这个角度来看,他们不会被认为是不同的,因为两个孩子都在那里。也许这已经改变了,所以其他答案适用于较新版本的 XML。
回答by mkoeller
The XML 1.0 Specdoes 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.
该XML 1.0规格并没有说明具有相同的名称作为相同的父元素的子元素的顺序东西。所以看起来,这个问题是不确定的。
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.
但是,大多数 XML 解析器和 API 将保留文本表示中给出的序列。因此,可以实现关心元素顺序的应用程序。对您问题的事实上的回答是:是的,顺序很重要。这两个例子是不同的。
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.
仔细观察,您需要评估您的用例是什么。如果您的 XML 需要与不同的(可能是第 3 方的)应用程序互操作,您应该始终假定顺序很重要。如果您可以完全控制生产和消费应用程序,那么您可以放宽此规则。
As always, you'll have to judge.
和往常一样,你必须做出判断。
回答by Scott Saunders
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.
我认为这些应该被认为是相同的,但这真的取决于软件或阅读它的人来决定。XML 只是一种写出数据的方式。应用程序决定了如何使用这些数据,因此决定了它的大部分含义。
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.
如果您的应用程序读入所有 person 元素,然后按名称按字母顺序排列它们,那么 XML 文档中的顺序就毫无意义。如果您的应用程序读取它们并按照人员在 XML 中出现的相同顺序分配座位,那么顺序非常重要。
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.
这取决于使用数据的应用程序。如果顺序很重要,则应在生成文件的人员的规范中对其进行描述。
回答by txwikinger
http://www.ibm.com/developerworks/xml/library/x-eleord.html
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.
也许文章中的讨论将有助于回答您的问题。由于您的问题有些开放,我不确定它是否涵盖了您的疑虑。
回答by Martin Liversage
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.
它们是有区别的。您可以使用各种 XML API 来按顺序处理元素或按索引查找元素。当然,在您的特定示例中,顺序可能无关紧要,但这取决于数据的语义。

