Reputation: 615
I have the following xml schema.
I can restrict the maximum and minimum limits on occurrence but how can i define restriction that f1 should occur in all elements of field or should not occur at all.
Any help would be appreciated.
<xs:element name="fields">
<xs:complexType>
<xs:sequence>
<xs:element name="field" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="f1" type="xs:integer" minOccurs="0" />
<xs:element name="f2" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Upvotes: 0
Views: 251
Reputation: 31299
After some thinking I think I now understand what you mean. You don't want f1 to occur in all or none of the child elements of <field>
like I thought but you want it one level higher: you want all <field>
elements in one <fields>
parent to either have an <f1>
child, or none of them. That's still not possible in xml schema 1.0 and in xml schema 1.1 only with an xpath-assertion. It's also not a natural way of modelling with the concept of types used by the xml schema specification.
However it is easy to model something like what you want if you create different types of fields within the <fields>
parent. Say you have one complex type assigned to an element '' which has a required '' child, and one other complex type assigned to an element <field>
which cannot have such a child at all. You can then easily say using a <xs:choice>
that all children of <fields>
should be either <f1field>
or `'.
Schema:
<xs:element name="fields">
<xs:complexType>
<xs:choice>
<xs:element name="field" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="f2" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="f1field" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="f1" type="xs:integer" />
<xs:element name="f2" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
XML:
<fields>
<f1field>
<name>x</name>
<f1>3</f1>
<f2>2</f2>
</f1field>
<f1field>
<name>x</name>
<f1>3</f1>
<f2>2</f2>
</f1field>
<!-- Next field will be rejected by validator because there are
already f1field elements -->
<field>
<name>df</name>
<f2>5</f2>
</field>
</fields>
Upvotes: 1