架构xs:unique未经过验证

时间:2013-04-02 21:32:24

标签: xml xsd schema unique

我有一个简单的xml文件

<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>

我想验证amount属性的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 个答案:

答案 0 :(得分:1)

您必须为targetNamespace定义别名,并在xpath中将其用于选择器和字段。请参阅此answer on SO,并阅读我发布的帖子和评论以获取更多详细信息。

相关问题