xmlxsdschemaunique

Schema xs:unique is not being validated


I have a simple xml file

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

And i wrote schema for it

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

I want to validate uniqness of amount property, but in all validators, my xml file seems valid and it shouldn't, why?


Solution

  • You have to define an alias for your targetNamespace and use that in your xpath for selectors and fields. See this answer on SO, and read the post and the comments I made for more details.