I am trying to come up with a XSD 1.0 schema with the following constraints:
- There is no ordering
- Some elements must appear exactly once
- Some elements may appear zero or unbounded times
- Allow unrecognized elements (do not validate them)
The reason for 3. is that I would like to validate the type if the element is present.
For example, a person must have exactly one name, an optional age (at most one), optional phone numbers (unlimited) and any other tag. These should validate:
<person>
<name>Bob</name>
<age>33</age>
<phone>123456789</phone>
<phone>123456788</phone>
</person>
<person>
<name>Alice</name>
</person>
<person>
<name>John</name>
<!-- unrecognized, arbitrary tags: -->
<location>city</location>
<occupation>laywer</occupation>
</person>
Whereas, these should not validate:
<person>
<!-- I am missing a name -->
<phone>123456789</phone>
</person>
<person>
<!-- I should only have one name -->
<name>Sally</name>
<name>Mary</name>
</person>
<person>
<name>Josh</name>
<!-- Phone number is not an int -->
<phone>not a number</phone>
</person>
This is invalid XSD that captures in a human-understandable way what I am trying to do:
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element type="xs:string" name="name" minOccurs="1" maxOccurs="1"/>
<xs:element type="xs:int" name="age" minOccurs="0" maxOccurs="1"/>
<xs:element type="xs:int" name="phone" minOccurs="0" maxOccurs="unbounded"/>
<xs:any />
</xs:all>
</xs:complexType>
</xs:element>
This XSD is invalid because you cannot have <any>
under an <all>
, and because XSD 1.0 does not allow you to have maxOccurs="unbounded"
in an <all>
element. Does anybody know how this can be accomplished?
You can do what you are looking for using xs:all
in XSD 1.1.
It can't be achieved in XSD 1.0.
来源:https://stackoverflow.com/questions/31677266/xsd-schema-with-unordered-required-optional-and-arbitrary-tags