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

时间:2016-02-14 20:48:50

标签: xsd

是否有办法在考虑属性值的情况下指定元素是唯一的?

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

1 个答案:

答案 0 :(得分:1)

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子集有限。

相关问题