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」で新しい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に対して正常に検証するために何をする必要があるかを理解できません(ただし、いくつかのことを試しました)。
誰か助けてもらえますか?前もって感謝します