Nerdio
Nerdio

Reputation: 1003

XSD Include of schema does not work with maxOccurs="unbounded"

I am slowly but surely getting some XML together with a lot of help from the stackoverflow community.

I have a schema for some XML which works fine, and contains a lot of Type definitions. So, I have split my Schema to put the type definitions in a different schema so I can use them in other Schema. If you follow; This is what I have done...

Here is my XML

<?xml version="1.0" encoding="UTF-8"?>

<ns:Root
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ns="urn:TestNamespace"  
    xsi:schemaLocation="urn:TestNamespace Test1.xsd"
    >
    <ns:element1 id="001">
        <ns:element2 id="001.1" order="1">
            <ns:element3 id="001.1.1" />
        </ns:element2>
        <ns:element2 id="001.2" order="2">
            <ns:element3 id="001.1.2" />
        </ns:element2>        
    </ns:element1>
    <ns:element1 id="002">
        <ns:element2 id="002.1" order="3">
        <ns:element3 id="002.1.1" />
        </ns:element2>
        <ns:element2 id="002.2" order="4">
            <ns:element3 id="002.1.2" />
        </ns:element2> 
    </ns:element1>    
</ns:Root>

This is my XSD

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:TestNamespace"
            xmlns:ns="urn:TestNamespace"
            elementFormDefault="qualified">

    <xsd:include schemaLocation="TestTypes1.xsd"/>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="element1" maxOccurs="unbounded" type="ns:element1Type"/>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:unique name="uniqueElement2OrderInElement1">
            <xsd:selector xpath="./ns:element1" />
            <xsd:field xpath="ns:element2/@order" />
        </xsd:unique>        
    </xsd:element>
</xsd:schema>

And here is my new Schema for the Types The elements defined here are directly copied out of the above schema where they worked.

<?xml version="1.0"?>

<xsd:schema version="1.0"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:TestNamespace"
           xmlns:ns="urn:TestNamespace"           
           elementFormDefault="unqualified">
    <xsd:complexType name="element1Type">
        <xsd:sequence>
            <xsd:element name="element2" maxOccurs="unbounded" type="ns:element2Type"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>

    <xsd:complexType name="element2Type">
        <xsd:sequence>
            <xsd:element name="element3" type="ns:element3Type" />
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string" />
        <xsd:attribute name="order" type="xsd:string" />
    </xsd:complexType>

    <xsd:complexType name="element3Type">
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>        
</xsd:schema>

When I try to validate my XML now, I get this error;

cvc-complex-type.2.4.a: Invalid content was found starting with element 'ns:element2'. One of '{element2}' is expected. [9] cvc-complex-type.2.4.a: Invalid content was found starting with element 'ns:element2'. One of '{element2}' is expected. [17]

Where lines 9 and 17 relate to the opening of the first 'element2' in each 'element1'. As I say this worked before I split them out, so I think now I must have lost something, or failed to translate something from one to the other. It seems to be complaining that it expects one 'element2' and even if I delete one of the 2nd instances of 'element2' I get the same error. The attributes for the elements seem to be correct in that it allows an unlimited number of occurrences.

I think then I am mis-reading the error.

As ever, any help or guidance is much appreciated. Thanks in anticipation

Upvotes: 1

Views: 831

Answers (1)

Istao
Istao

Reputation: 7585

I suspect you shoud put elementFormDefault="qualified", not elementFormDefault="unqualified" in the types schema, because it's qualified in the main xsd, and it's qualified in the xml.

Upvotes: 2

Related Questions