为什么 xmllint 不验证元素的唯一约束?

Why is xmllint not validating the unique constraint for elements?

我想验证某个元素是否唯一。不幸的是 xmllint 没有验证它。这是 xmllint(或 libxml)中的错误吗?

我创建了一个最小的例子:

示例。xml

<?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>

示例。xsd

<?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>

在我看来,当我根据 xsd:

验证 xml 时,lint 应该抛出一个错误
xmllint --schema example.xsd --noout example.xml

为什么 xmllint 没有抛出错误?

如果您在架构中使用 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>