2010-02-25 71 views
2

考虑这个XML -限制元素的值相同的XPath下是元素中唯一

<Root> 
    <Foo> 
    <UniqueValue>A100</UniqueValue> 
    </Foo> 
    <Foo> 
    <UniqueValue>A101</UniqueValue> 
    </Foo> 
    <Foo> 
    <UniqueValue>B102</UniqueValue> 
    </Foo> 
    <Foo> 
    <UniqueValue>A101</UniqueValue> <!-- This should be an error --> 
    </Foo> 
    <Foo> 
    <UniqueValue> A101 </UniqueValue> <!-- This should be an error but not critical for now --> 
    </Foo> 
</Root> 

如何限制的简单类型元素<UniqueValue>使得它的值是唯一的? 也不是我不想限制为数字。

回答

2

尝试在“根”上添加唯一性约束。请注意,这需要ONT他元素定义,而不是一个类型定义:

<xsd:element name="Root" type="RootType"> 
    <xsd:unique name="uniqueValues"> 
    <xsd:selector xpath="Foo"/> 
    <xsd:field xpath="UniqueValue"/> 
    </xsd:unique> 
</xsd:element> 

根据您所使用的解析器,您可能需要打开更多的选择,让这个验证(如“满架构验证” ,身份约束验证等)。