我希望使用 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 成功验证。
谁能帮我?提前致谢