XSD: importing complex types does not work

I have an XML schema schema.xsdwith custom types in an external file types.xsd. I do not know why my complex type is typeComplexnot validated correctly. Simple types such as this typeSimplework fine. What's wrong with it?

Eclipse will 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 - invalid 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>
+3
source share
2 answers

I really wanted to use <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>
+1
source

"root" "http://www.example.org/schema". . "http://www.example.org/types", . .

, , "http://www.example.org/types", , xmlns.

:

<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

:

- , elementFormDefault="unqualified". , :

<root xmlns="http://www.example.org/schema">
  <simple xmlns="">XXX</simple>
  <complex xmlns="">
    <a></a>
    <b></b>
  </complex>
</root>

, XSD. , xml:

<root>
  <simple>XXX</simple>
  <complex>
    <a></a>
    <b></b>
  </complex>
</root>
0

All Articles