在针对 WSDL(xsd 架构)验证 xml 时了解 elementFormDefault 限定/不限定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19954278/
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
Understanding elementFormDefault qualified/unqualified when validating xml against a WSDL (xsd schema)
提问by Felix Schwarz
I'm trying to understand the implications of elementFormDefault="qualified/unqualified"in an XML schema which is embedded in WSDL (SOAP 1.1, WSDL 1).
我试图了解elementFormDefault="qualified/unqualified"嵌入在 WSDL(SOAP 1.1、WSDL 1)中的 XML 模式的含义。
For example I have this schema inside a WSDL:
例如,我在 WSDL 中有这个架构:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.example.com/library">
<xsd:element name="person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
In plain XML this is obviously invalid because "name" has no specified namespace:
在纯 XML 中,这显然是无效的,因为“name”没有指定的命名空间:
<lib:person xmlns:lib="http://www.example.com/library">
<name>XML Schema</name>
</lib:person>
while this is obviously valid because all elements are qualified:
虽然这显然是有效的,因为所有元素都是合格的:
<lib:person xmlns:lib="http://www.example.com/library">
<lib:name>qualified xml</lib:name>
</lib:person>
But surprisingly libxml says that the following is also valid:
但令人惊讶的是,libxml 表示以下内容也是有效的:
<person xmlns="http://www.example.com/library">
<name>XML Schema</name>
</person>
Question 1: I assumed that qualifiedmeant <person>should look something like <lib:person xmlns:lib="...">. But the results seem to indicate that the xmlnsattribute does the same?
问题 1:我认为qualifiedmean<person>应该看起来像<lib:person xmlns:lib="...">. 但结果似乎表明该xmlns属性的作用相同?
Now assume that the above XML is part of a SOAP request, e.g.
现在假设上述 XML 是 SOAP 请求的一部分,例如
...
<s:Body>
<person xmlns="http://www.example.com/library">
<name>XML Schema</name>
</person>
</s:Body>
...
Question 2: Is the request above valid if the WSDL contains a qualifiedschema as displayed above? (plain SOAP, disregarding WS-I basic profile)
问题 2:如果 WSDL 包含qualified如上所示的模式,则上述请求是否有效?(普通 SOAP,不考虑 WS-I 基本配置文件)
Question 3When I consider WS-I Basic profile (especially 4.1.13 SOAP Body and Namespaces) is the above request still valid? (is personconsidered "namespace qualified"?)
问题 3当我考虑 WS-I Basic 配置文件(尤其是4.1.13 SOAP Body 和 Namespaces)时,上述请求是否仍然有效?(被person认为是“命名空间合格”?)
回答by Michael Kay
Specifying "qualified" in the schema, which is nearly always the right thing to do, means that local element declarations (xs:element within xs:complexType) refers to elements in the target namespace of the schema. Without it, they refer to elements in no namespace.
在模式中指定“合格”,这几乎总是正确的做法,意味着本地元素声明(xs:complexType 中的 xs:element)引用模式目标命名空间中的元素。没有它,它们将引用没有命名空间的元素。
So with qualified, in your case, the name element must be in the namespace http://www.example.com/library. It will be in this namespace if either
因此,在您的情况下,name 元素必须位于命名空间http://www.example.com/library 中。它将在这个命名空间中,如果
(a) you explicitly put it in this namespace, as in this example:
(a) 你明确地把它放在这个命名空间中,如本例所示:
<lib:person xmlns:lib="http://www.example.com/library">
<lib:name>qualified xml</lib:name>
</lib:person>
(b) or you use a default namespace, as in this example:
(b) 或者您使用默认命名空间,如本例所示:
<person xmlns="http://www.example.com/library">
<name>qualified xml</name>
</person>

