XSD schema with unordered required, optional and arbitrary tags

青春壹個敷衍的年華 提交于 2019-12-02 01:57:06

问题


I am trying to come up with a XSD 1.0 schema with the following constraints:

  1. There is no ordering
  2. Some elements must appear exactly once
  3. Some elements may appear zero or unbounded times
  4. 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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!