1

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

My schema after applying @PetruGardea fix looks like this:

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

1

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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.