Tell me please, how can I create xsd schema, which successfully validate the following xml:
—> XML 1
<?xml version="1.0" encoding="UTF-8"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<list>
<typeA>
<value>test string value 123</value>
</typeA>
<typeB>
<value>test string value 456</value>
</typeB>
<typeC>
<value>test string value 789</value>
</typeC>
</list>
</start>
—> XML 2
<?xml version="1.0" encoding="UTF-8"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<list>
<typeB>
<value>test string value 456</value>
</typeB>
<typeC>
<value>test string value 789</value>
</typeC>
</list>
</start>
—> XML 3
<?xml version="1.0" encoding="UTF-8"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<list>
<typeC>
<value>test string value 789</value>
</typeC>
<typeC>
<value>test string value 123</value>
</typeC>
</list>
</start>
—> XML 4
<?xml version="1.0" encoding="UTF-8"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<list>
<typeC>
<value>test string value 789</value>
</typeC>
</list>
</start>
I write XSD, but it doesn’t work:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="typeC">
<xs:complexType>
<xs:sequence>
<xs:element name="value" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="typeB">
<xs:complexType>
<xs:sequence>
<xs:element name="value" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="typeA">
<xs:complexType>
<xs:sequence>
<xs:element name="value" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="start">
<xs:complexType>
<xs:sequence>
<xs:element ref="list"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="list">
<xs:complexType>
<xs:sequence>
<xs:element ref="typeA"/>
<xs:element ref="typeB"/>
<xs:element ref="typeC"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Is there any other way to achieve this?
Thanks in advance!
The way you have defined the inclusion of your elements typeA, typeB, and typeC is set such that one and exactly one of each must be included. It sounds like you want it to be more flexible so that 0 or many of each element can be included. Look at the maxOccurrs and minOccurs attributes.