3

我正在尝试提出具有以下约束的 XSD 1.0 架构:

  1. 没有订单
  2. 有些元素必须只出现一次
  3. 某些元素可能出现零次或无限次
  4. 允许无法识别的元素(不验证它们)

3. 的原因是如果元素存在,我想验证类型。

例如,一个人必须只有一个姓名、一个可选的年龄(最多一个)、可选的电话号码(无限制)和任何其他标签。这些应该验证:

<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>

鉴于,这些不应该验证

<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>

这是无效的 XSD,它以人类可以理解的方式捕获我正在尝试做的事情:

<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>

此 XSD 无效,因为您不能在<any>下拥有<all>,并且因为 XSD 1.0 不允许您maxOccurs="unbounded"<all>元素中拥有。有谁知道如何做到这一点?

4

1 回答 1

2

xs:all您可以在 XSD 1.1中使用您正在寻找的东西。

它不能在 XSD 1.0 中实现。

于 2015-07-28T15:13:28.093 回答