XML XSD 架构 - 在架构中强制执行唯一属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5541305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML XSD Schema - Enforce Unique Attribute Values in Schema
提问by MrEyes
Lets say I have a schema that defines the following XML:
假设我有一个定义以下 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>
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:
我希望在架构级别能够强制 Key 属性的值是唯一的,即上面的示例有效,但以下示例无效:
<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>
Notice that there are two Addelements with a Keyof Key2
请注意,有两个Add元素Key为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.
我的印象是这在模式级别是不可能的,但是我全神贯注。
回答by Michael Kay
@BatteryBackupUnit has the right idea, but the syntax is more like:
@BatteryBackupUnit 有正确的想法,但语法更像是:
<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>
回答by kevinarpe
More on Michael Kay's answer: If your schema (XSD) declares a namespace, you must include this in your selection.xpath. If you are using Microsoft Visual Studio 2010, you may find a namespace automatically declared.
更多关于 Michael Kay 的回答:如果您的架构 (XSD) 声明了一个命名空间,您必须将其包含在您的 selection.xpath 中。如果您使用的是 Microsoft Visual Studio 2010,您可能会发现一个自动声明的命名空间。
<xs:schema id="MyData"
targetNamespace="http://tempuri.org/MyData.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/MyData.xsd"
xmlns:mstns="http://tempuri.org/MyData.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
...
<xs:unique name="UniqueAddKey">
<xs:selector xpath="mstns:Add" />
<xs:field xpath="@Key" />
</xs:unique>
</xs:schema>
回答by BatteryBackupUnit
you can achieve this by using xs:unique
您可以通过使用 xs:unique 来实现这一点
<xs:element name="Instrument">
<xs:complexType>
<xs:all>
<xs:unique name="ModuleId">
<xs:selector xpath="./*" />
<xs:field xpath="@id" />
</xs:unique>
</xs:all>
</xs:complexType>
</xs:element>
The above example will enforce a unique attribute "id" for all Instrument Elements. there's also xs:key, which can be used to establish a Primary Key - Foreign Key relationship: http://www.datypic.com/books/defxmlschema/chapter17.html
上面的示例将为所有仪器元素强制执行唯一的属性“id”。还有xs:key,可以用来建立主键-外键关系:http: //www.datypic.com/books/defxmlschema/chapter17.html

