3

我希望使用 XInclude 将 XML 文件拆分为多个包含。这种方法我比其他方法更喜欢,因为包含的 XML 文件可以是独立的,可以自己验证文件。

我有以下示例架构(mybook.xsd):


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
 xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 elementFormDefault="qualified" 
 attributeFormDefault="unqualified">
 <xs:import 
  namespace="http://www.w3.org/XML/1998/namespace" 
  schemaLocation="http://www.w3.org/2001/xml.xsd"/>
 <xs:element name="mybook">
  <xs:annotation>
   <xs:documentation>Comment describing your root element</xs:documentation>
  </xs:annotation>
  <xs:complexType>
   <xs:sequence>
    <xs:element name="toc">
     <xs:complexType>
      <xs:attributeGroup ref="xml:specialAttrs"/>
     </xs:complexType>
    </xs:element>
    <xs:element ref="part" maxOccurs="unbounded"/>
    <xs:element name="index">
     <xs:complexType>
      <xs:attributeGroup ref="xml:specialAttrs"/>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="part">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="chapter" maxOccurs="unbounded">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="page" maxOccurs="unbounded">
        <xs:complexType>
         <xs:simpleContent>
          <xs:extension base="xs:string">
           <xs:attributeGroup ref="xml:specialAttrs"/>
          </xs:extension>
         </xs:simpleContent>
        </xs:complexType>
       </xs:element>
      </xs:sequence>
      <xs:attributeGroup ref="xml:specialAttrs"/>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
   <xs:attributeGroup ref="xml:specialAttrs"/>
   <xs:attribute name="chaptername" use="required"/>
  </xs:complexType>
 </xs:element>
</xs:schema>

我将 part 设为全局元素,因此我可以使用根元素“part”开始一个新的 xml 元素。现在我的 xml 文件看起来像:

主文件(mybook.xml):


<?xml version="1.0" encoding="UTF-8"?>
<mybook 
 xsi:noNamespaceSchemaLocation="mybook.xsd" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xi="http://www.w3.org/2001/XInclude" >
 <toc/>
 <part chaptername="Chapter 1" >
  <chapter>
   <page>String</page>
   <page>String</page>
  </chapter>
  <chapter>
   <page>String</page>
   <page>String</page>
  </chapter>
 </part>
  <xi:include href="part2.xml"/>
 <index/>
</mybook>

还有我的包含文件(part2.xml):


<?xml version="1.0" encoding="UTF-8"?>
<part chaptername="Chapter 2"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:noNamespaceSchemaLocation="mybook.xsd" >
 <chapter>
  <page>String</page>
  <page>String</page>
 </chapter>
 <chapter>
  <page>String</page>
  <page>String</page>
 </chapter>
</part>

在 XmlSpy 中,现在我可以成功验证 part2.xml。但是,在验证 mybook.xml 时,出现以下错误:

文件 mybook.xml 无效。
 文件 part2.xml 无效。
  'noNamespaceSchemaLocation' 属性引用其目标命名空间已用于验证的模式。
   错误位置:部分
   细节
    'noNamespaceSchemaLocation' 属性引用其目标命名空间已用于验证的模式。
    cvc-elt.5.2.1:元素对于实际类型定义“{anonymous}”无效。

因为我对 XML 不熟悉,所以我看不到(但尝试了几件事)需要做什么才能使两个 XML 文件都针对 mybook.xsd 成功验证。

谁能帮我?提前致谢

4

1 回答 1

2

您遇到了使用 noNamespaceSchemaLocation 的限制。基本上,这可以用作 XSD 验证器如何查找架构的提示。或多或少,你不能让这个属性出现多次。你可以参考这个

所以,发生的事情是遇到第一个 noNamespaceSchemaLocation,它告诉验证器如何找到无命名空间模式,以便可以验证“mybook”元素。稍后,当涉及到“part”元素时,会找到另一个 noNamespaceSchemaLocation,但如果这会导致一些其他定义,例如“mybook”元素,它已经在验证中,那会怎样?正是由于这个原因,规范不允许这样的事情,因此你会得到你注意到的错误。

您似乎需要依靠其他方法来告诉您的验证器如何找到您的架构文档,而不是使用 noNamespaceSchemaLocation。

于 2011-07-07T17:04:38.453 回答