Reputation: 902
I am writing a schema for the following XML. I want to limit the occurrence of Element_E, Element_F and Element_G to '1' and Element_D can occur n number of times. I tried to use xs:sequence
but it enforces the element order, xs:choice
does not check the max occurrences of above elements.
Is it possible to validate the occurrence of each element in a group through XML Schema?
<?xml version="1.0" encoding="utf-8"?>
<Element_A> <!-- One time occurrence -->
<Element_B> <!-- One time occurrence -->
<Element_C> <!-- One time occurrence -->
<Element_D /> <!-- n time occurrence -->
<Element_D />
<Element_D />
<Element_E /> <!-- One time occurrence -->
<Element_F /> <!-- One time occurrence -->
<Element_G /> <!-- One time occurrence -->
</Element_C>
</Element_B>
</Element_A>
Upvotes: 0
Views: 1185
Reputation: 111726
With XSD 1.0, you'll have to check such a constraint in code.
With XSD 1.1, you can use xs:assert
to state count invariants:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.1"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:element name="Element_A">
<xs:complexType>
<xs:sequence>
<xs:element name="Element_B">
<xs:complexType>
<xs:sequence>
<xs:element name="Element_C">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Element_D"/>
<xs:element name="Element_E"/>
<xs:element name="Element_F"/>
<xs:element name="Element_G"/>
</xs:choice>
<xs:assert test=" count(Element_E) = 1
and count(Element_F) = 1
and count(Element_G) = 1"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Upvotes: 1
Reputation: 2775
Have you looked into maxOccurs ?
<xsd:element ref="Element_E" maxOccurs="1"/>
http://www.w3schools.com/schema/schema_complex_indicators.asp
Upvotes: 0