首页 > 解决方案 > XML 验证期间可能出现的命名空间问题

问题描述

以下带有 XSD 的 XML 会导致验证错误:

命名空间中的元素“选择”在命名空间'http://www.test.it'中具有无效的子元素“选择” 'http://www.test.it'。预期的可能元素列表:“选择”。

这是choices.xml

<?xml version="1.0" encoding="utf-8"?>
<choices xmlns="http://www.test.it"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
         xsd:schemaLocation="http://www.test.it ./schema/choices.xsd">
    <choice>yes</choice>
</choices>

这是schema/choices.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema  xmlns="http://www.test.it"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.test.it">

    <xs:simpleType name="yes_or_no_t">
        <xs:restriction base="xs:string">
            <xs:enumeration value="yes" />
            <xs:enumeration value="no" />
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="choices" >
        <xs:complexType>
            <xs:all>
                <xs:element name="choice" type="yes_or_no_t" />
            </xs:all>
        </xs:complexType>
    </xs:element>

</xs:schema>

我必须保留xmlns="http://www.test.it"在 XML 中。XML 和 XSD 是本地文件(不通过网络发布)。我宁愿将 XSD 保存在schema子目录中。

标签: xmlxsdxml-namespacesxsd-validationxml-validation

解决方案


有两个问题...

寻找 XSD

用于xsi:schemaLocation命名空间的 XML,例如您的,而不是xsi:noNamespaceSchemaLocation.

也可以看看:

合格的元素

添加elementFormDefault="qualified"到 XSD 的架构元素。

也可以看看:


推荐阅读