考虑到属性的值,有没有办法指定元素是唯一的?

Is there a way to specify that elements are unique taking into consideration the values of the attributes?

有没有一种方法可以在考虑属性值的情况下指定元素是唯一的?

<fruit>orange</fruit> <!-- valid -->
<fruit>apple</fruit> <!-- valid -->
<fruit>apple</fruit> <-- invalid, duplicate value -->
<fruit color=green">apple</fruit> <!-- valid, because attributes are different -->
<fruit color=red">apple</fruit> <!-- valid, because attributes are different -->

XSD 1.1 解决方案

这可以在 XSD 1.1 中使用断言解决。此示例断言测试每个有色水果是唯一具有该颜色和名称的水果,并测试每个无色水果是唯一具有该名称但没有颜色的水果:

<assert test="every $fruit in ./fruit satisfies
    if ($fruit/@color)
        then count(fruit[@color=$fruit/@color and text()=$fruit/text()])=1
    else
        count(fruit[text()=$fruit/text() and not(@color)])=1"/>

我认为这不能直接使用 XSD 1.0 中的 xs:unique 来完成,因为允许的 XPath 子集有限。