I have an XML Schema, a snippet of which looks like the one below:
<xs:complexType name="Operation" abstract="true">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="type" type="xs:string"/>
<xs:element name="inputFields" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="field" type="field" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="outputFields" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="field" type="field" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
I want to put a restriction/validation that the size of the list inputFields and outputFields be equal.
How can I achieve that in the XML Schema itself?
XSD 1.1
You could do this via assertions in XSD 1.1 by constraining the count of the number elements in
inputFieldsandoutputFieldsto be equal:You'd place this on the element declaration containing both
inputFieldsandoutputFields.XSD 1.0
You cannot use
xs:assertin XSD 1.0 and cannot express your constraint given your current XML design. However, if you instead redesigned your XML to pair input and output fields together in the first place,you'd naturally constrain their occurrence counts to be equal, and you'd be able to use XSD 1.0.