Question

My xml file is

<?xml version="1.0" encoding="UTF-8"?>
<edge xmlns="http://www.example.org/stext">
<ellipse rx="189" ry="22" cx="279" cy="531"
    style="fill:#FFFDFD;stroke:#000000;" id="Ellipse_23" isLocked="false" />
<ellipse rx="130" ry="38" cx="580" cy="393"
    style="fill:#FFFDFD;stroke:#000000;" id="Ellipse_19" isLocked="false" />
<ellipse rx="172" ry="92" cx="539" cy="245"
    style="fill:#BD7272;stroke:#D9B5B5;" id="Ellipse_20" isLocked="false" />
<circle r="51" cx="426" cy="284" style="fill:#BD7272;stroke:#D9B5B5;"
    id="Circle_16" isLocked="false" />
<circle r="45" cx="428" cy="397" style="fill:#FFFDFD;stroke:#302E2E;"
    id="Circle_17" isLocked="false" />
</edge>

This is my XSD schema

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/stext"
xmlns:tns="http://www.example.org/stext" elementFormDefault="qualified">
<element name="edge">
    <complexType>
        <sequence>

            <element name="circle" maxOccurs="unbounded">
                <complexType>
                    <attribute name="r" type="int" />
                    <attribute name="cx" type="int" />
                    <attribute name="cy" type="int" />
                    <attribute name="style" type="string" />
                    <attribute name="id" type="string" />
                    <attribute name="isLocked" type="boolean" />
                </complexType>
            </element>
            <element name="ellipse" maxOccurs="unbounded">
                <complexType>
                    <attribute name="rx" type="int" />
                    <attribute name="ry" type="int" />
                    <attribute name="cx" type="int" />
                    <attribute name="cy" type="int" />
                    <attribute name="style" type="string" />
                    <attribute name="id" type="string" />
                    <attribute name="isLocked" type="boolean" />
                </complexType>
            </element>
        </sequence>
    </complexType>
</element>
</schema>

I faced this kind of exception

Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element 'ellipse'. One of '{"http://www.example.org/stext":circle}' is expected.

Was it helpful?

Solution

This happens, because <sequence> imposes a specific order.

I think what might solve your problem, is to use an additional xsd:choice element.

If you write it like

<sequence>
   <choice  maxOccurs="unbounded">
        <element name="circle">
            <complexType>
                <attribute name="r" type="int" />
                <attribute name="cx" type="int" />
                <attribute name="cy" type="int" />
                <attribute name="style" type="string" />
                <attribute name="id" type="string" />
                <attribute name="isLocked" type="boolean" />
            </complexType>
        </element>
        <element name="ellipse">
            <complexType>
                <attribute name="rx" type="int" />
                <attribute name="ry" type="int" />
                <attribute name="cx" type="int" />
                <attribute name="cy" type="int" />
                <attribute name="style" type="string" />
                <attribute name="id" type="string" />
                <attribute name="isLocked" type="boolean" />
            </complexType>
        </element>
    </choice>    
</sequence>

that would allow for multiple circle OR ellipse elements in any order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top