XSD Validate XML file with non-deterministic element detection

The following XML fragment can be split using the standard XML library (using 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, the elements 'a' and 'b' are mixed (not deterministic). Is it possible to write an XSD for this "mixed" behavior?

+3
source share
1 answer

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>
+9
source

Source: https://habr.com/ru/post/1727749/


All Articles