XML验证过程中可能存在命名空间问题



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

命名空间'http://www.test.it'中的元素"choices"在命名空间'http://www.test.it'中具有无效的子元素"choice"。需要可能元素的列表:"choice"。

这是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子目录中。

有两个问题。。。

查找XSD

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

另请参阅:

  • 如何使用schemaLocation或noNamespaceSchemaLocation将XML链接到XSD
  • 如何正确引用本地XML架构文件

合格元素

elementFormDefault="qualified"添加到XSD的schema元素中。

另请参阅:

  • elementFormDefault在XSD中做什么

最新更新