3

I want an XML file split up with several includes, using XInclude. This method I prefere above others, because the included XML files can be standalone to be validated files on their own.

I have the following sample schema (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>

I made part a global element so I could start an new xml element with the root element "part". Now my xml files look like:

The main file (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>

And my include file (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>

Within XmlSpy, now I can successfully validate part2.xml. However when validating mybook.xml, I got the following error:

File mybook.xml is not valid.
 File part2.xml is not valid.
  The 'noNamespaceSchemaLocation' attribute references a schema whose target namespace was already used for validation.
   Error location: part
   Details
    The 'noNamespaceSchemaLocation' attribute references a schema whose target namespace was already used for validation.
    cvc-elt.5.2.1: The element  is not valid with respect to the actual type definition '{anonymous}'.

Because I'm failry new to XML I cannot see (but tried several things) what needs to be done to have both XML files validate successfully against the mybook.xsd.

Can anyone help me? Thanks in advance

eiri
  • 93
  • 6
  • Perhaps, see http://stackoverflow.com/questions/1098116/xinclude-schema-namespace-problem to get a no-solution to your question ? – Istao Dec 18 '10 at 10:28
  • Yes, I already did examine that question, but whatever I try, I get an error during the validate. I thought that one of the advantages of the XInclude method was that you could have two succesfully validating xml files, so there should be a way to accomplish that, I think. – eiri Dec 18 '10 at 12:29

1 Answers1

2

You are running up against a restriction on the use of noNamespaceSchemaLocation. Basically, this can be used as a hint to the XSD validator for how to find your schema. More or less, you are not allowed to have this attribute appear more than once. You can refer to this.

So, what is happening is the first noNamespaceSchemaLocation is encountered, and that tells the validator how to find the no namespace schema so that the "mybook" element can be validated. Later, when it comes to the "part" element, another noNamespaceSchemaLocation is found, but if that were to result in some other definition for, say, the "mybook" element, which is already being validated, what then? It is for that reason that the spec disallows such a thing, and thus you get the error you noted.

It seems you would need to rely on some other method to tell your validator how to find your schema document(s), rather than using noNamespaceSchemaLocation.

Kevin
  • 1,876
  • 12
  • 17