XML 关心元素的顺序吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4328867/
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
Does XML care about the order of elements?
提问by dragonmantank
XML confuses me sometimes, but I'm trying to get this figured out. What the vendor is telling me doesn't make sense but again, XML and I don't get along :)
XML 有时让我感到困惑,但我正在努力弄清楚这一点。供应商告诉我的内容没有意义,但 XML 和我不相处:)
I have some XML that I'm sending to a vendor's web service that is giving me random failures:
我有一些 XML 正在发送给供应商的 Web 服务,该服务给我带来了随机故障:
<root>
<Request>
<Driver id="1" VehId="1">...</Driver>
<Driver id="2" VehId="1">...</Driver>
<Driver id="3" VehId="2">...</Driver>
<Vehicle id="1">...</Vehicle>
<Vehicle id="2">...</Vehicle>
<Driver id="4" VehId="2">...</Driver>
</Request>
</root>
There is no XSLT or XSD to compare against to see if my XML is valid.
没有 XSLT 或 XSD 可以用来比较以查看我的 XML 是否有效。
The vendor states that the XML is invalid because Driver #4 is in the wrong area. The XPath for Driver should be root/Request/Driver and Vehicle is root/Request/Vehicle.
供应商声明 XML 无效,因为驱动程序 #4 位于错误区域。Driver 的 XPath 应该是 root/Request/Driver,Vehicle 应该是 root/Request/Vehicle。
Is it common that XML parsers would force an element order, especially if there is no XSD to compare the XML to? The vendor's support is slow to get back with me, so I want to know what good common practice is.
XML 解析器强制元素顺序是否常见,尤其是在没有 XSD 来比较 XML 的情况下?供应商的支持回复我很慢,所以我想知道什么是好的常见做法。
Followup
跟进
I complained enough to our account rep about not being able to test this (and made it sound like they were just trying to get support money) that it turns out that the Developers have the XSD, but Support doesn't. So I had been talking to the wrong group *facepalm*
我向我们的客户代表抱怨无法对此进行测试(并且听起来他们只是想获得支持资金),结果开发人员拥有 XSD,但支持人员没有。所以我一直在和错误的群体交谈 *facepalm*
I got the XSD, and it does enforce a specific order of elements.
我得到了 XSD,它确实强制执行特定的元素顺序。
Now to fight them in regards to their own sample XML doesn't follow the schema, but at least now I have something to test against.
现在就他们自己的示例 XML 与他们作斗争不遵循模式,但至少现在我有一些东西可以测试。
采纳答案by marc_s
If there is no XSD (XML schema) at hand, then all you can check for is whether or not your XML is well formed.
如果手头没有 XSD(XML 模式),那么您只能检查您的 XML 格式是否正确。
In your case - it is. There are no overlapping XML tags, no XML tag that are left open or anything of that sort.
在你的情况下 - 它是。没有重叠的 XML 标签,没有打开的 XML 标签或任何类似的东西。
If the vendor needs to enforce things like order inside the XML, he ought to provide an XSD file - otherwise, his "requirements" cannot be validated and checked....
如果供应商需要在 XML 中强制执行诸如订单之类的事情,他应该提供一个 XSD 文件 - 否则,他的“要求”无法得到验证和检查......
回答by Jürgen Steinblock
XML schema compositor "sequence" will enforce ordering
XML 模式合成器“序列”将强制排序
I know this is old but I just came upon the post.
我知道这很旧,但我刚刚看到这个帖子。
Until today I would most likely answer the question Does XML care about the order of elements?with No, unless you use a poorly written xml parser.
直到今天,我将最有可能回答这个问题Does XML care about the order of elements?有No, unless you use a poorly written xml parser.
However, today a third party application complained that the xml files I created were invalid. They use an XSDfile to validate the xml. And yes, you can enforce the order or elements within an xsd file:
但是,今天第三方应用程序抱怨我创建的 xml 文件无效。他们使用一个XSD文件来验证 xml。是的,您可以在 xsd 文件中强制执行顺序或元素:
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ComplexType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" default="" name="Value1" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" default="" name="Value2" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
The keyword is xs:sequence
关键字是 xs:sequence
The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.
sequence 元素指定子元素必须按顺序出现。每个子元素可以出现 0 次到任意次数。
which is in contrast to xs:allwhich does not care about the order but only allows elements which occur zero or once.
这与xs:all不关心顺序但只允许出现零次或一次的元素相反。
Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time
指定子元素可以以任何顺序出现。每个子元素可以出现 0 次或 1 次
(The words sequenceand allare both what is called a Compositorin the XML Schema definition.)
(这两个词sequence和在 XML 模式定义中all都是所谓的合成器。)
回答by Buggieboy
Vendors will do what they will do, but it is a nonstandard application of XML to rely on ordering.
供应商会做他们会做的事情,但依赖排序是 XML 的非标准应用程序。
XML is declarative, not procedural. So, it shouldn't be "stepwise".
XML 是声明性的,而不是过程性的。所以,它不应该是“逐步”的。
回答by Buggieboy
XML schemas can enforce element order. If there's no schema, neither order nor tags nor general structure not the kind of text (if it's a number or anything) is prescribed in any way - in theory. Of course that's not the case here.
XML 模式可以强制执行元素顺序。如果没有模式,既没有顺序也没有标签,也没有一般结构,也没有以任何方式规定的文本类型(如果它是数字或任何东西)——理论上。当然,这里并非如此。
It is perfectly possible to parse* data from XML without caring about order, but it can be easier (e.g. when using SAX, I imagine, or when you're a lazy bastard writing very sloppy code) to parse if you assume a certain order. Although they shouldinclude some schema if they want a certain order, it's perfectly possible that their parser chokes on it anyway. Yes, they shouldn't do this, but obviously they don't care.
完全有可能在不关心顺序的情况下从 XML 解析 * 数据,但是如果您假设某个顺序,解析会更容易(例如,当使用 SAX 时,我想,或者当您是一个懒惰的混蛋编写非常草率的代码时) . 尽管如果他们想要某个顺序,他们应该包含一些模式,但无论如何他们的解析器完全有可能窒息。是的,他们不应该这样做,但显然他们不在乎。
*By "parsing" I don't mean "take that XML document and turn it into e.g. a DOM", but "take that e.g. DOM and extract the information from it".
*通过“解析”,我的意思不是“获取该 XML 文档并将其转换为例如 DOM”,而是“获取例如 DOM 并从中提取信息”。
回答by Doc Brown
Accessing XML by DOM preserves the order of nodes the way they are in your XML document. Look here:
通过 DOM 访问 XML 会按照它们在 XML 文档中的方式保留节点的顺序。看这里:
http://www.w3schools.com/dom/dom_nodes_nodelist.asp
http://www.w3schools.com/dom/dom_nodes_nodelist.asp
there you find:
你会发现:
A node list object represents a list of nodes, in the same order as in the XML.
节点列表对象表示节点列表,其顺序与 XML 中的顺序相同。
If your web service is relyingon the order is a different question - it may or may not, that depends on the web service implementation.
如果您的 Web 服务依赖于订单则是另一个问题 - 可能也可能不会,这取决于 Web 服务的实现。
回答by Reinderien
An XML schema may or may not enforce element order. It depends on the schema in question. In the most general sense, XML element order does not matter, unless otherwise specified by the appropriate schema.
XML 模式可能会也可能不会强制执行元素顺序。这取决于所讨论的架构。在最一般的意义上,XML 元素顺序无关紧要,除非适当的模式另有规定。
回答by Nux
Well I think the best possible answer is - ask them. They could even be parsing your XML as a text file and so you might need to have line breaks and some "correct" order of attributes.
好吧,我认为最好的答案是 - 问他们。他们甚至可以将您的 XML 解析为文本文件,因此您可能需要有换行符和一些“正确”的属性顺序。
If they would parse this correctly order wouldn't matter (at least not in terms of valid requests). As I see it they should build two tables and join them with supplied ids.
如果他们能正确解析这个顺序就无关紧要(至少在有效请求方面不是)。在我看来,他们应该构建两个表并使用提供的 ID 将它们连接起来。

