XSD 验证非命名空间元素上的命名空间属性?
XSD to validate namespaced attributes on non-namespaced elements?
我被要求创建模式来验证文档,其结构是在 XML 模式成为现实之前开发的。 None 的元素名称中有冒号,但某些属性有。似乎理论上应该可以用这样的方法来做到这一点:
$ cat repro.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="" xmlns:x="namespace-x" targetNamespace="">
<xsd:import namespace="namespace-x" schemaLocation="repro.inc"/>
<xsd:element name="a" type="a"/>
<xsd:complexType name="a">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute ref="x:y"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
$ cat repro.inc
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="namespace-x">
<xsd:attribute name="y" type="xsd:string"/>
</xsd:schema>
但是模式验证器抱怨:
$ cat repro.xml
<a xmlns:x="namespace-x" x:y="foo"/>
$ xmllint --schema repro.xsd repro.xml
<?xml version="1.0"?>
<a xmlns:x="namespace-x" x:y="foo"/>
repro.xml:1: element a: Schemas validity error : Element 'a': No matching global declaration available for the validation root.
repro.xml fails to validate
targetNamespace=""
是不是意味着此架构文档中定义的元素不使用名称空间?如果不是,如何使用 XML 模式验证此类文档?
不幸的是,将元素放在没有前缀的命名空间中 (xmlns="other-ns"
) 会破坏大量使用命名空间感知解析器的软件,因此按照这些思路的解决方案没有吸引力。
Doesn't targetNamespace="" mean the elements defined in this schema
document don't use a namespace? If not, how does one go about
validating such documents using XML Schema?
不可以,targetNamespace
的值绝不能为空;简单地省略 targetNamespace
代替没有命名空间。您还可以从 xsd:schema
.
中删除 xmlns=""
repro.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:x="namespace-x">
<xsd:import namespace="namespace-x" schemaLocation="repro.inc"/>
<xsd:element name="a" type="a"/>
<xsd:complexType name="a">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute ref="x:y"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
repro.inc 和 repro.xml 没问题。
通过上述更改,您的 XSD 将成功验证您的 XML。
我被要求创建模式来验证文档,其结构是在 XML 模式成为现实之前开发的。 None 的元素名称中有冒号,但某些属性有。似乎理论上应该可以用这样的方法来做到这一点:
$ cat repro.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="" xmlns:x="namespace-x" targetNamespace="">
<xsd:import namespace="namespace-x" schemaLocation="repro.inc"/>
<xsd:element name="a" type="a"/>
<xsd:complexType name="a">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute ref="x:y"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
$ cat repro.inc
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="namespace-x">
<xsd:attribute name="y" type="xsd:string"/>
</xsd:schema>
但是模式验证器抱怨:
$ cat repro.xml
<a xmlns:x="namespace-x" x:y="foo"/>
$ xmllint --schema repro.xsd repro.xml
<?xml version="1.0"?>
<a xmlns:x="namespace-x" x:y="foo"/>
repro.xml:1: element a: Schemas validity error : Element 'a': No matching global declaration available for the validation root.
repro.xml fails to validate
targetNamespace=""
是不是意味着此架构文档中定义的元素不使用名称空间?如果不是,如何使用 XML 模式验证此类文档?
不幸的是,将元素放在没有前缀的命名空间中 (xmlns="other-ns"
) 会破坏大量使用命名空间感知解析器的软件,因此按照这些思路的解决方案没有吸引力。
Doesn't targetNamespace="" mean the elements defined in this schema document don't use a namespace? If not, how does one go about validating such documents using XML Schema?
不可以,targetNamespace
的值绝不能为空;简单地省略 targetNamespace
代替没有命名空间。您还可以从 xsd:schema
.
xmlns=""
repro.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:x="namespace-x">
<xsd:import namespace="namespace-x" schemaLocation="repro.inc"/>
<xsd:element name="a" type="a"/>
<xsd:complexType name="a">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute ref="x:y"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
repro.inc 和 repro.xml 没问题。
通过上述更改,您的 XSD 将成功验证您的 XML。