XML XSD 架构 - 在架构中强制使用唯一的属性值
假设我有一个定义以下 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 属性的值是唯一的,即上面的示例有效,但下面的示例无效:
<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
元素,其 Key
为 Key2
作为参考,这里是简单的架构:
<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>
我的印象是,这是在架构级别上不可能,但我洗耳恭听。
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>
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>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@BatteryBackupUnit 有正确的想法,但语法更像是:
@BatteryBackupUnit has the right idea, but the syntax is more like:
更多关于 Michael Kay 的回答:如果您的架构 (XSD) 声明了一个命名空间,则必须将其包含在您的 Selection.xpath 中。如果您使用的是 Microsoft Visual Studio 2010,您可能会发现自动声明的命名空间。
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.
您可以通过使用 xs:unique 来实现这一点
。上面的示例将为所有乐器元素强制使用唯一的属性“id”。
还有 xs:key,可用于建立主键 - 外键关系:
http://www.datypic.com/books/defxmlschema/chapter17.html
you can achieve this by using xs:unique
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