对 complexType 而不是元素的唯一约束 [英] Unique constraint on a complexType instead of an element

查看:35
本文介绍了对 complexType 而不是元素的唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XSD 结构:

I have the following XSD structure:

<xs:schema xmlns:ns="http://abc/">
  ...
  <xs:element name="abc">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="map"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  ...
  <xs:element name="map">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="entry" type="ns:MapEntryType" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="entry">
      <xs:selector xpath="entry"/>
      <xs:field xpath="key"/>
    </xs:unique>
  </xs:element>
  <xs:complexType name="MapEntryType">
    <xs:sequence>
      <xs:element name="key" type="xs:string"/>
      <xs:element name="value" type="xs:anyType"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

这是它的工作.

map 元素现在必须根据包装器的不同命名为不同的名称,因此名称有时是 map,有时是 properties,有时options,等等

The map element now has to be called something different based on whichever is the wrapper, so the name is sometimes map, sometimes properties, sometimes options, etc.

因此我想对 map 元素进行泛化.

Therefore I want to genericize the map element.

我尝试执行以下操作:

  • 使 map 成为 xs:complexType 并将 ref 更改为 type.
    • 这导致 xs:unique 不被接受并失败
    • Making map a xs:complexType and changing ref to type.
      • This resulted in xs:unique not being accepted and failed
      • 这行得通,但导致 XSD 在文档中存在大量 xs:unique.

      难道没有一种方法可以简单地告诉我我想要一个特定的结构并且它包含独特的元素,而不必到处重复独特的约束吗?

      Isn't there a way to simply tell that I want a specific structure and it containing unique elements without having to repeat the unique constraint everywhere?

      推荐答案

      简而言之,这是不可能的.XSD 1.0 和 1.1 都将标识约束置于元素之下;约束不能全局定义,因此除了封闭元素之外,本身没有重用".鉴于您的场景(不同需求的不同元素名称),无法重用.

      Short answer, it is not possible. Both XSD 1.0 and 1.1 place the identity constraints under an element; a constraint cannot be globally defined, therefore there is no "reuse" per se, other than that of the enclosing element. Given your scenario (different element names for different needs) it is not possible to reuse.

      这篇关于对 complexType 而不是元素的唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆