7

我需要编写一个嵌入式 XML 模式,即模式在与数据相同的 XML 中定义。

我试图了解如何正确地做到这一点,但到目前为止,我还没有得到一个简单的例子来通过验证。这是我试图用作带有内联模式的简单示例 XML 的内容:(
注意:XML 结构(例如根/项)已经在野外,所以我被限制不能在数据元素上使用命名空间.)

<?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 Schema Validator运行该 XML 时,XML 验证失败,并显示以下错误消息,表明它不希望将其<xs:schema>视为子元素!

每个 cvc-complex-type.1.2.4 无效: 元素 { http://www.w3.org/2001/XMLSchema }:此处不允许架构 (1) 在元素 {None}:root 中,期望 [{None}:项目,$]:

问:您能否向我展示一个简单的 XML 文档示例,该文档具有通过验证的内联模式定义?

4

1 回答 1

6

如果您的root孩子有一个xs:schema元素作为孩子,那么架构需要允许它拥有这样的孩子。允许它的最简单方法是使用通配符:

<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>
于 2013-03-22T09:26:45.607 回答