2013-04-02 99 views
1

我有一個簡單的XML文件 架構XS:唯一沒有被驗證

<form xmlns="http://www.example.org/form-reader/form-description" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.example.org/form-reader/form-description form-description.xsd"> 
    <pages amount="1"/> 
    <pages amount="1"/> 
</form> 

我寫架構它

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.example.org/form-reader/form-description" 
xmlns="http://www.example.org/form-reader/form-description" 
elementFormDefault="qualified"> 
    <xs:complexType name="pagesType"> 
    <xs:attribute name="amount" type="xs:string" use="required" /> 
    </xs:complexType> 

    <xs:complexType name="formType"> 
    <xs:sequence minOccurs="1"> 
     <xs:element name="pages" minOccurs="0" maxOccurs="unbounded" 
     type="pagesType" /> 
    </xs:sequence> 
    </xs:complexType> 

    <xs:element name="form" type="formType"> 
    <xs:unique name="pageNumberUnique"> 
     <xs:selector xpath="pages" /> 
     <xs:field xpath="@amount" /> 
    </xs:unique> 
    </xs:element> 
</xs:schema> 

我想驗證數額財產的uniqness,但在所有的驗證器,我的XML文件似乎有效,它不應該,爲什麼?

解決方案

應用@PetruGardea修復後,我的模式是這樣的:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:tns="http://www.example.org/form-reader/form-description" 
targetNamespace="http://www.example.org/form-reader/form-description" 
xmlns="http://www.example.org/form-reader/form-description" 
elementFormDefault="qualified"> 
    <xs:complexType name="pagesType"> 
    <xs:attribute name="amount" type="xs:string" use="required" /> 
    </xs:complexType> 

    <xs:complexType name="formType"> 
    <xs:sequence minOccurs="1"> 
    <xs:element name="pages" minOccurs="0" maxOccurs="unbounded" 
    type="pagesType" /> 
    </xs:sequence> 
    </xs:complexType> 

    <xs:element name="form" type="formType"> 
    <xs:unique name="pageNumberUnique"> 
    <xs:selector xpath="tns:pages" /> 
    <xs:field xpath="@amount" /> 
    </xs:unique> 
    </xs:element> 
</xs:schema> 

回答

1

你必須定義一個別名爲您的目標名稱和使用,在您的XPath的選擇和領域。看到這個answer on SO,並閱讀帖子和我提出的意見了解更多細節。

+0

它的工作,謝謝! –