xml - 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
子目录中。
解决方案
有两个问题...
寻找 XSD
用于xsi:schemaLocation
命名空间的 XML,例如您的,而不是xsi:noNamespaceSchemaLocation
.
也可以看看:
合格的元素
添加elementFormDefault="qualified"
到 XSD 的架构元素。
也可以看看:
推荐阅读
- r - 根据R中另一个变量的平均值重命名分类变量
- apache-kafka - Clickhouse 将 kafka 导入线程优先于其他查询
- visual-studio-code - VSCode 如何在源代码管理树视图面板中折叠/展开
- reactjs - 推送钩子 useHistory 后,我得到“警告:React 检测到 ... 调用的钩子顺序发生了变化”
- google-apps-script - 谷歌工作表脚本将工作表复制到带有值而不是公式的新选项卡
- r - 从数据框值创建复杂的字符串向量
- javascript - 显示带有不同按钮的图片 html css javascript
- dictionary - 去确定字符串切片上的单词出现次数
- javascript - 如何将任何视频格式的视频播放成 HTML5(之前使用 Flash 播放)
- python - 类方法返回类实例:在单独的文件中定义时如何避免循环导入错误?