本文介绍了带有无序必需,可选和任意标记的XSD架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 没有订购
  2. 某些元素必须只出现一次
  3. 某些元素可能出现零次或无界次数
  4. 允许使用无法识别的元素(不对其进行验证)

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

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>

而这些应该验证:

<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,无法以人们可以理解的方式捕获我正在尝试做的事情:

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>

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

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?

推荐答案

您可以使用XSD 1.1中的xs:all完成您想要的操作.

You can do what you are looking for using xs:all in XSD 1.1.

在XSD 1.0中无法实现.

It can't be achieved in XSD 1.0.

这篇关于带有无序必需,可选和任意标记的XSD架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 21:12