我想验证某个元素是否唯一。不幸的是xmllint没有验证它。这是xmllint(或libxml)中的错误吗?
我创建了一个最小的例子:
<?xml version="1.0" encoding="utf-8"?>
<book-list
xmlns="https://example.com/book"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="https://example.com/book example.xsd">
<book>Example 1</book>
<book>Example 2</book>
<book>Example 1</book>
</book-list>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns="https://example.com/book"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://example.com/book"
elementFormDefault="qualified">
<xs:element name="book-list">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:unique name="unique-books">
<xs:selector xpath="book" />
<xs:field xpath="." />
</xs:unique>
</xs:element>
</xs:schema>
在我看来xmllint应该抛出错误,当我对xsd验证xml时:
xmllint --schema example.xsd --noout example.xml
为什么xmllint没有抛出错误?
答案 0 :(得分:2)
如果您在架构中使用 targetNamespace 属性,则应将完全限定的XPath指定为唯一节点 - 检查此架构,xmlint按预期失败:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:lib="https://example.com/book" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://example.com/book" elementFormDefault="qualified">
<xs:element name="book-list">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="unique-books">
<xs:selector xpath="lib:book"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:schema>