XSD Validation of XML file having non-deterministic occurance of elements
-
19-09-2019 - |
Question
The following XML snippet is parsable using standard XML lib (tried with Java and Scala).
<?xml version="1.0" encoding="UTF-8"?>
<list>
<a>value1</a>
<b>value2</b>
<a>value3</a>
<a>value4</a>
<a>value5</a>
<b>value6</b>
<b>value7</b>
</list>
As you can see 'a' and 'b' elements are mixed (non deterministic). Is it possible to write a XSD for this "mixed" behaviour?
Solution
This should work .. :-)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="list">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="a" type="xs:string" />
<xs:element name="b" type="xs:string" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow