XML 验证:为什么在这种情况下不允许显式指定名称空间?
XML validation: why is explicitly specifying a namespace not allowed in this case?
给出以下 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:foo="urn:foo"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:foo"
version="3.0">
<xsd:complexType name="Something">
<xsd:sequence>
<xsd:element name="Nested" type="foo:NestedType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="NestedType" />
</xsd:schema>
以下XML有效:
<ns:Something xmlns:ns="urn:foo"
xsi:type="ns:Something"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Nested />
</ns:Something>
而这个 XML(唯一的区别:'Nested' 是显式命名空间)不是:
<ns:Something xmlns:ns="urn:foo"
xsi:type="ns:Something"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns:Nested />
</ns:Something>
(至少根据 http://www.utilities-online.info/xsdvalidation/ 处的验证器)。
为什么不允许我在 Nested
元素上指定名称空间?难道我只是比绝对必要的更明确吗?
是因为在你定义的XSD中,elementFormDefault
隐式设置为"unqualified"
。
您不能为您的元素设置命名空间前缀。
给出以下 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:foo="urn:foo"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:foo"
version="3.0">
<xsd:complexType name="Something">
<xsd:sequence>
<xsd:element name="Nested" type="foo:NestedType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="NestedType" />
</xsd:schema>
以下XML有效:
<ns:Something xmlns:ns="urn:foo"
xsi:type="ns:Something"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Nested />
</ns:Something>
而这个 XML(唯一的区别:'Nested' 是显式命名空间)不是:
<ns:Something xmlns:ns="urn:foo"
xsi:type="ns:Something"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns:Nested />
</ns:Something>
(至少根据 http://www.utilities-online.info/xsdvalidation/ 处的验证器)。
为什么不允许我在 Nested
元素上指定名称空间?难道我只是比绝对必要的更明确吗?
是因为在你定义的XSD中,elementFormDefault
隐式设置为"unqualified"
。
您不能为您的元素设置命名空间前缀。