如何定义一个模式限制,允许枚举值或者匹配某个模式?

8
我正在定义一个simpleType,它有一个restriction,要么是来自enumeration的值,要么是与pattern匹配的值。我知道可以从pattern中完成所有操作,但我想要enumeration提供的选取列表。
这就是我期望能够做到的:
<xs:simpleType name="both">
  <xs:restriction base="xs:string">
    <xs:enumeration value="one" />
    <xs:enumeration value="two" />
    <xs:pattern value="[0..9]+" />
  </xs:restriction>
<xs:simpleType>

但这种方法会失败,因为一个值不能同时满足两个约束条件。如果我修改模式以允许任何枚举值,则它将无法匹配该模式。

1个回答

12

结果证明我需要使用union。将枚举类型定义为单独的类型:

<xs:simpleType name="enumeration">
  <xs:restriction base="xs:string">
    <xs:enumeration value="one" />
    <xs:enumeration value="two" />

  </xs:restriction>
<xs:simpleType>

然后将目标类型创建为枚举:

<xs:simpleType name="both">
  <xs:union memberTypes="enumeration">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:pattern value="[0..9]+" />
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>

现在我已经获得了选择列表或匹配模式。正是我想要的!

更新:实际上可以将两个简单类型定义为union的子级或通过memberTypes属性定义。


1
谢谢你的提示!我想提一下,它也适用于在 <xs:union> 中列出的两个 <xs:simpleType> 元素。顺便说一句,你应该将答案(即使是你自己的)标记为已接受,以明确它是正确的解决方案。 - Melebius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接