Reputation: 28586
I have a XML schema schema.xsd
with custom types in outside file types.xsd
. I don't know why my complex type typeComplex
is doesn't validated correctly. Simple types like typeSimple
works ok. What wrong with that ?
Eclipse say:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'a'. One of '{"http://www.example.org/types":a}' is expected.
schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema" elementFormDefault="qualified"
xmlns:t="http://www.example.org/types">
<xs:import schemaLocation="types.xsd" namespace="http://www.example.org/types" />
<xs:element name="root">
<xs:complexType>
<xs:all>
<xs:element name="simple" type="t:typeSimple" />
<xs:element name="complex" type="t:typeComplex" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
types.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/types" xmlns="http://www.example.org/types"
elementFormDefault="qualified">
<xs:simpleType name="typeSimple">
<xs:restriction base="xs:string">
<xs:length value="3" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="typeComplex">
<xs:sequence>
<xs:element name="a" type="xs:string" />
<xs:element name="b" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
text.xml - not valid with xsd - Why ?
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.example.org/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/schema schema.xsd " xmlns:t="http://www.example.org/types">
<simple>XXX</simple>
<complex>
<a></a> <!-- not valid here; Eclipse say: cvc-complex-type.2.4.a: Invalid content was found starting with element 'a'. One of '{"http://www.example.org/types":a}' is expected. -->
<b></b>
</complex>
</root>
Upvotes: 4
Views: 10291
Reputation: 28586
I did was i want using <xs:include />
.
schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema" elementFormDefault="qualified">
<xs:include schemaLocation="types.xsd" />
<xs:element name="root">
<xs:complexType>
<xs:all>
<xs:element name="simple" type="typeSimple" />
<xs:element name="complex" type="typeComplex" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
types.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:simpleType name="typeSimple">
<xs:restriction base="xs:string">
<xs:length value="3" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="typeComplex">
<xs:sequence>
<xs:element name="a" type="xs:string" />
<xs:element name="b" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Upvotes: 1
Reputation: 31780
Your instance document is declared as being an instance of the "root" type inside the namespace "http://www.example.org/schema". This is fine. Within this document you want to use types from the namespace "http://www.example.org/types" and so you have included this namespace with a prefix. This is also fine.
However when you go to use the types contained in the "http://www.example.org/types" namespace you are ignoring the prefix you defined in your xmlns declaration.
The correct way to reference these types:
<root xmlns="http://www.example.org/schema"
xmlns:t="http://www.example.org/types">
<simple>XXX</simple>
<complex>
<t:a></t:a>
<t:b></t:b>
</complex>
</root>
UPDATE
Your alternatives are:
Use unqualified types - just change your schema definitions to make elementFormDefault="unqualified"
. This means you can now do this:
<root xmlns="http://www.example.org/schema">
<simple xmlns="">XXX</simple>
<complex xmlns="">
<a></a>
<b></b>
</complex>
</root>
Or, don't use XSD schema. This means you can just use well-formed xml:
<root>
<simple>XXX</simple>
<complex>
<a></a>
<b></b>
</complex>
</root>
Upvotes: 1