Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am unable to get the xs:unique specifier to work in an XML file. I just don't seem to be able to work out an XPath that works. My apologies for the amount of code in this question, but I would be extremely grateful to anyone who can point out what I am doing wrong below. No matter what I do, I cannot get the @ref attribute in the <artwork> element to report an error for my duplicating the value (each ref must be unique).

As an aside, does anyone know of a good XPath evaluation tool? This seems like it would be such a useful thing to have that I can't believe someone hasn't knocked something together.

Any help or pointers to information would be very gratefully received.

Kind wishes, Patrick

This is my schema:

XML
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Artworks"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:aw="http://www.fourthwish.co.uk/data/Artworks.xsd"
    xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd"
    targetNamespace="http://www.fourthwish.co.uk/data/Artworks.xsd"
    elementFormDefault="qualified"
>
    <xs:element name="artworks">
        <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
                <xs:element name="artwork" type="ArtworkType">
                    <xs:unique name="uniqueRef">
                        <xs:selector xpath="."/>
                        <xs:field xpath="@ref"/>
                    </xs:unique>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="ArtworkType">
        <xs:sequence>
            <xs:element name="title" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="ref" type="xs:nonNegativeInteger"/>
    </xs:complexType>
</xs:schema>


And this is my XML file:

XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<artworks
    xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.fourthwish.co.uk/data/Artworks.xsd Artworks.xsd"
>
    <artwork ref="1">
        <title>Title String</title>
    </artwork>
    <artwork ref="1">
        <title>Title String</title>
    </artwork>
</artworks

>
Posted
Updated 18-Oct-11 4:06am
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900