如何使用xsd:unique? [英] How to use xsd:unique?
问题描述
我正在创建一个XML文件,我想在其中存储对项目所做的更改只是出于娱乐目的.因此,我写了一个小的XML模式.问题是我希望每个Revision
的id
属性都是唯一的.
I'm creating an XML file where I would like to store my changes of a project just for fun purposes. And therefore I have written a small XML schema. The problem is that I want that each Revision
's id
attribute to be unique.
因此,我已经通过Internet和堆栈溢出进行了搜索,但无法解决此问题.我正在使用Visual Studio 2015-不知道这是否会成为问题.
So I have searched through the Internet and Stack Overflow but I could not fix this problem. I'm using Visual Studio 2015 - not sure if this would be a problem.
我正在使用的XML模式是:
The XML Schema that I'm using is:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="Revisions">
<xs:complexType>
<xs:sequence>
<xs:element name="Revision" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Author" type="xs:string"/>
<xs:element name="Date" type="xs:date"/>
<xs:element name="Comments" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="Revision">
<xs:annotation>
<xs:documentation>
The id of each Revision element must be unique.
</xs:documentation>
</xs:annotation>
<xs:selector xpath="Revision"/>
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
</xs:schema>
以及我正在使用的XML文件:
And the XML file that I'm using:
<?xml version="1.0" encoding="utf-8"?>
<Revisions xmlns="http://tempuri.org/XMLSchema.xsd">
<Revision id="1">
<Author>JP</Author>
<Date>2014-07-09</Date>
<Comments>Initial version</Comments>
</Revision>
<Revision id="2">
<Author>JP</Author>
<Date>2016-01-26</Date>
<Comments>
Created a RegistrationValidator class which uses regular expressions to check
if usernames, passwords, email addresses, etc.. are in correct format
</Comments>
</Revision>
<Revision id="3">
<Author>JP</Author>
<Date>2016-08-14</Date>
<Comments>Created an XML schema which validates this XML file</Comments>
</Revision>
<Revision id="3">
<Author>Test</Author>
<Date>2016-08-14</Date>
<Comments>dummy comments</Comments>
</Revision>
</Revisions>
如您所见,我为最后一个Revision
标签分配了相同的id值,并且从错误列表中没有收到任何错误,警告或消息.
有人知道我在做什么错吗?
As you can see I've assigned the same id value to the last Revision
-tags and I get no errors, warnings or messages from the error list.
Does somebody know what I'm doing wrong?
推荐答案
您非常亲密.只需将名称空间前缀mstns
添加到xs:selector/@xpath
即可解决问题:
You were very close. Just add the namespace prefix, mstns
, to the xs:selector/@xpath
to fix the problem:
<xs:unique name="Revision">
<xs:annotation>
<xs:documentation>
The id of each Revision element must be unique.
</xs:documentation>
</xs:annotation>
<xs:selector xpath="mstns:Revision"/>
<xs:field xpath="@id"/>
</xs:unique>
这篇关于如何使用xsd:unique?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!