I have an XSD of this form:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/example"
xmlns:tns="http://www.example.org/example" elementFormDefault="qualified">
<complexType name="bType">
</complexType>
<complexType name="aType">
<choice maxOccurs="unbounded">
<element name="a" type="tns:aType" />
<element name="b" type="tns:bType" />
</choice>
</complexType>
<element name="topelement">
<complexType>
<sequence>
<element name="a" type="tns:aType" maxOccurs="1" />
</sequence>
</complexType>
</element>
</schema>
And an XML file that I expect to match it, e.g:
<?xml version="1.0" encoding="UTF-8"?>
<topelement xmlns="http://www.example.org/example"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/example example.xsd ">
<a> <!-- Error on this line. -->
<a/>
<b/>
<b/>
<a/>
</a>
</topelement>
Unfortunately the XSD says that this is not valid with the following error:
cvc-complex-type.2.4.b: The content of element 'a' is not complete. One of '{"http://www.example.org/example":a, "http://www.example.org/example":b}' is expected. example.xml line 5
As far as I can tell, I've done everything I need to do for the tag to be complete. I've filled it with an unbounded choice of 'a' and 'b' tags. Can anyone see what's gone wrong?
To clarify, I want there to be only one 'a' tag under topelement, and underneath that, a mix of 'a' and 'b' tags.
Before posting this answer, I hadn't observed your own answer .. Anyway I don't want to let my effort/time-spent go waste .. So I won't delete this post .. Along with the same answer I have also .. written some points please go through ..
ComplexType aType
defines that it always have either <a/>
or <b/>
as child elements .. It means .. wherever element <a/>
appears it must have a child <a/>
or <b/>
.. which is not true..as per your input XML.
So this the XSD code I have written to overcome the errors, (notice "minOccurs" attribute in the code .. because the absence of which you were getting errors ..)
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/example"
xmlns:tns="http://www.example.org/example" elementFormDefault="qualified">
<element name="topelement">
<complexType>
<sequence>
<element name="a" type="tns:aType" minOccurs="0" maxOccurs="1" />
</sequence>
</complexType>
</element>
<complexType name="bType">
</complexType>
<complexType name="aType">
<sequence>
<choice maxOccurs="unbounded">
<element name="a" type="tns:aType" minOccurs="0"/>
<element name="b" type="tns:bType" minOccurs="0"/>
</choice>
</sequence>
</complexType>
</schema>
So according to my code .. The Tag <a/>
may or may not have any child elements.
If you don't want to change the XSD file .. then your XML must have <a/>
tag or <b/>
tag as children of <a/>
..
something like this :
<topelement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.org/example" xsi:schemaLocation="http://www.example.org/example example.xsd">
<a>
<a>
<b/>
</a>
<b/>
<b/>
<a>
<a>
<b/>
</a>
<b/>
</a>
</topelement>
Where as this is invalid:
<topelement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.org/example" xsi:schemaLocation="http://www.example.org/example example.xsd">
<a>
<a>
<b/>
</a>
<a/><!--this is wrong-->
<b/>
</a>
</topelement>
regards:
Infant Pro
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With