如何确保 XML 模式中的唯一元素值? [英] How do I ensure unique element values in an XML schema?
本文介绍了如何确保 XML 模式中的唯一元素值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要确保以下 xml 中没有重复的书名:
I want to ensure that there are no duplicate book titles in the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="books3.xsd">
<book>
<title>Book1</title>
</book>
<book>
<title>Book2</title>
</book>
<book>
<title>Book1</title> <!-- duplicate should not be allowed -->
</book>
</books>
我正在使用以下架构:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="book"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="testUnique">
<xs:selector xpath="book"/>
<xs:field xpath="title"/>
</xs:unique>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="title" type="xs:NCName"/>
</xs:schema>
oXygen XML 编辑器在我验证时告诉我这是有效的.
oXygen XML editor tells me this is valid when I validate.
谁能看到我做错了什么?
Can anybody see what I am doing wrong?
推荐答案
架构似乎没问题,应该检测到重复项.可能是 Oxygen 中的错误?
the schema seems ok and should detect the duplicate. may be a bug in Oxygen?
你可以试试这个网站来验证你的 xml:http://www.xmlvalidation.com
you can try this site to validate your xml : http://www.xmlvalidation.com
你会看到它在你的 xmldocument 中发现了错误:
and you'll see it finds errors in your xmldocument:
重复的唯一值 [Book1]声明为身份约束元素书"
Duplicate unique value [Book1] declared for identity constraint of element "books"
这篇关于如何确保 XML 模式中的唯一元素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文