我需要创建一个嵌入式 XML 模式,即模式在与数据相同的 XML 中定义。
我正在尝试了解如何正确执行此操作,但到目前为止,我未能获得一个简单的示例来通过验证。以下是我尝试用作带有内联架构的简单示例 XML 的内容:
(注意:XML 结构(例如 root/item)已经在野外,所以我被限制不能在数据元素上使用命名空间。)
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="#mySchema">
<xs:schema id="mySchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="xs:string"
maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<item>String 1</item>
<item>String 2</item>
<item>String 3</item>
</root>
但是当我通过 w3.org 运行该 XML 时 XML Schema Validator ,XML 验证失败,并显示以下错误消息,表明它不希望看到 <xs:schema>
作为子元素!
Invalid per cvc-complex-type.1.2.4: element {http://www.w3.org/2001/XMLSchema}:schema not allowed here (1) in element {None}:root, expecting [{None}:item,$]:
问:您能否展示一个简单的 XML 文档示例,该文档具有通过验证的内联架构定义?
最佳答案
如果您的 root
child 有一个 xs:schema
元素作为 child ,那么模式需要允许它有这样的 child 。允许它的最简单方法是使用通配符:
<xs:sequence>
<xs:any processContents="skip" namespace="http://www.w3.org/2001/XMLSchema"
minOccurs="0" maxOccurs="1"/>
<xs:element name="item" type="xs:string"
maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
关于xml - 如何创建有效的内联 XML 架构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15559573/