Reputation: 199
I have the following XSD:
<element name="OrderElement" type="tns:OrderType"></element>
<complexType name="OrderType">
<sequence>
<element name="Name" type="tns:NameType"></element>
<element name="Address" type="tns:AddressType"></element>
</sequence>
</complexType>
<complexType name="NameType">
<sequence>
<element name="FirstName" minOccurs="1" maxOccurs="1" type="string">
</element>
<element name="Surname" minOccurs="1" maxOccurs="1" type="string">
</element>
</sequence>
</complexType>
<complexType name="AddressType">
<sequence>
<element name="AddressLine1" minOccurs="1" maxOccurs="1"
type="string">
</element>
<element name="AddressLine2" minOccurs="1" maxOccurs="1"
type="string">
</element>
<element name="Country" minOccurs="1" maxOccurs="1"
type="tns:CountriesDeliveryType">
</element>
</sequence>
</complexType>
<complexType name="CountriesDeliveryType">
<choice minOccurs="1">
<element name="USA" type="string" maxOccurs="1" minOccurs="0"></element>
<element name="Brazil" type="string" maxOccurs="1" minOccurs="0"></element>
<element name="China" type="string" maxOccurs="1" minOccurs="0"></element>
</choice>
</complexType>
What I want to do is have a complex type so that I can re-use the countries throughout the code. The problem I am having is that within the XML it validates when I remove the country:
<?xml version="1.0" encoding="UTF-8"?>
<tns:OrderElement xmlns:tns="http://www.example.org/NewXMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/NewXMLSchema NewXMLSchema.xsd ">
<tns:Name>
<tns:FirstName>tns:FirstName</tns:FirstName>
<tns:Surname>tns:Surname</tns:Surname>
</tns:Name>
<tns:Address>
<tns:AddressLine1>tns:AddressLine1</tns:AddressLine1>
<tns:AddressLine2>tns:AddressLine2</tns:AddressLine2>
<tns:Country>
<tns:USA>tns:USA</tns:USA>
</tns:Country>
</tns:Address>
</tns:OrderElement>
i.e. If I remove the
<tns:USA>tns:USA</tns:USA>
it still validates.
I've been looking into this for ages but haven't come across a solution. It's probably something simple but I can't seem to work it out. Any suggestions?
Upvotes: 1
Views: 52
Reputation: 6632
It's all in your minOccurs="0"
. All of the countries can be not set at the same time due to this. Fix would look like
<complexType name="CountriesDeliveryType">
<choice minOccurs="1" maxOccurs="3">
<element name="USA" type="string" maxOccurs="1" minOccurs="1"></element>
<element name="Brazil" type="string" maxOccurs="1" minOccurs="1"></element>
<element name="China" type="string" maxOccurs="1" minOccurs="1"></element>
</choice>
</complexType>
Though, there still could be two <USA>
elements.
Upvotes: 1