XML关心元素的顺序吗?

时间:2023-01-14 08:02:54

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:

我向供应商的web服务发送了一些XML,它们会给我随机的失败:

<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在错误的区域。驱动程序的XPath应该是根/请求/驱动程序,而车辆是根/请求/车辆。

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,但支持没有。所以我找错人了

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与它们进行斗争并不遵循模式,但至少现在我有一些要测试的东西。

7 个解决方案

#1


17  

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文件,否则,他的“需求”不能验证和检查....

#2


13  

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

直到今天,我最可能回答的问题是XML是否关心元素的顺序,除非您使用的是编写得很糟糕的XML解析器。

However, today a third party application complained that the xml files I created are invalid. They use an XSD file 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:序列

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.

序列元素指定子元素必须出现在序列中。每个子元素可以出现从0到任意次数。

which is in contrast to xs:all which does not care about the order but only allows only elements which occur zero or once.

与xs形成对比的是:所有这些都不关心顺序,但只允许出现零或一次的元素。

Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time

指定子元素可以以任何顺序出现。每个子元素可以发生0或1次。

#3


3  

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是声明性的,不是过程性的。所以,这不应该是“逐步的”。

#4


3  

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 should include 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并从中提取信息”。

#5


2  

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元素顺序并不重要,除非由适当的模式指定。

#6


2  

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 relying on the order is a different question - it may or may not, that depends on the web service implementation.

如果您的web服务依赖于订单是另一个问题——它可能会也可能不会依赖于web服务实现。

#7


0  

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连接它们。

#1


17  

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文件,否则,他的“需求”不能验证和检查....

#2


13  

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

直到今天,我最可能回答的问题是XML是否关心元素的顺序,除非您使用的是编写得很糟糕的XML解析器。

However, today a third party application complained that the xml files I created are invalid. They use an XSD file 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:序列

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.

序列元素指定子元素必须出现在序列中。每个子元素可以出现从0到任意次数。

which is in contrast to xs:all which does not care about the order but only allows only elements which occur zero or once.

与xs形成对比的是:所有这些都不关心顺序,但只允许出现零或一次的元素。

Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time

指定子元素可以以任何顺序出现。每个子元素可以发生0或1次。

#3


3  

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是声明性的,不是过程性的。所以,这不应该是“逐步的”。

#4


3  

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 should include 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并从中提取信息”。

#5


2  

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元素顺序并不重要,除非由适当的模式指定。

#6


2  

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 relying on the order is a different question - it may or may not, that depends on the web service implementation.

如果您的web服务依赖于订单是另一个问题——它可能会也可能不会依赖于web服务实现。

#7


0  

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连接它们。