XML XSD 架构 - 强制架构中的唯一属性值 [英] XML XSD Schema - Enforce Unique Attribute Values in Schema

查看:44
本文介绍了XML XSD 架构 - 强制架构中的唯一属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个定义以下 XML 的架构:

Lets say I have a schema that defines the following XML:

<Values>
    <Add Key="Key1">Value 1</Add>
    <Add Key="Key2">Value 2</Add>
    <Add Key="Key3">Value 3</Add>
    <Add Key="Key4">Value 4</Add>
</Values>

我希望,在架构级别,能够强制 Key 属性的值是唯一的,即上面的示例有效,但以下示例无效:

I would like, at a schema level, to be able to enforce that the values for the Key attribute are unique, i.e. the example above is valid, but the following example would be invalid:

<Values>
    <Add Key="Key1">Value 1</Add>
    <Add Key="Key2">Value 2</Add>
    <Add Key="Key2">Value 3</Add>
    <Add Key="Key3">Value 4</Add>
</Values>

注意有两个 Add 元素的 KeyKey2

Notice that there are two Add elements with a Key of Key2

这里是简单的架构供参考:

For reference here is the simple schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="Values">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Add" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="Key" type="xs:token" use="required"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

我的印象是这在模式级别是不可能的,但我全神贯注.

I am under the impression that this is not possible at a schema level, however I am all ears.

推荐答案

@BatteryBackupUnit 有正确的想法,但语法更像是:

@BatteryBackupUnit has the right idea, but the syntax is more like:

<xs:element name="Values">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="Add" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:unique name="UniqueAddKey">
    <xs:selector xpath="Add" /> 
    <xs:field xpath="@Key" /> 
  </xs:unique>
</xs:element>

这篇关于XML XSD 架构 - 强制架构中的唯一属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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