2013-04-02 56 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

它的工作,谢谢! –