2013-02-04 123 views
1

XSD唯一約束似乎不適合我。我有以下XSD:XSD唯一約束不起作用

<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://beardedhen.com/form" 
    xmlns:tns="http://beardedhen.com/form" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"> 

<xs:complexType name="OptionListType"> 
    <xs:sequence> 
    <xs:element name="option" minOccurs="1" maxOccurs="unbounded"> 
     <xs:complexType> 
    <xs:attribute name="label" type="xs:string" use="required"/> 
    <xs:attribute name="value" type="xs:string" use="required"/> 
    </xs:complexType> 
    </xs:element> 
    </xs:sequence> 
    <xs:attribute name="name" type="xs:string" use="required"/> 
</xs:complexType> 

<xs:element name="OptionList" type="tns:OptionListType"> 
    <xs:unique name="uniqueOptionListLabel"> 
<xs:selector xpath="option"/> 
    <xs:field xpath="@label"/> 
</xs:unique> 
</xs:element> 

</schema> 

,當我確認下方的XML在Eclipse和在線驗證,返回任何錯誤:

<OptionList xmlns="http://beardedhen.com/form" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    name="statusLevels"> 
    <option label="Critical" value="0"/> 
    <option label="Warning" value="1"/> 
    <option label="Warning" value="1"/> 
    <option label="Warning" value="1"/> 
    <option label="Good" value="4"/> 
</OptionList> 

它看起來簡單,有例子在那裏,我隨之而來,但這讓我瘋狂! :-)

任何想法?

回答

4

在selector元素的XPath表達式中,必須使用前綴「tns」。

<xs:selector xpath="tns:option"/>