2010-02-25 33 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> 

根據您所使用的解析器,您可能需要打開更多的選擇,讓這個驗證(如「滿架構驗證」 ,身份約束驗證等)。