Patrick
Patrick

Reputation: 779

How to specify unique values in an XML schema

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 could point out what I am doing wrong below. No matter what I do, I cannot get the @ref attribute in the element to report an error for my duplicating the value (each ref must be unique).

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

Kind wishes, Patrick

This is my schema:

<?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="artwork"/>
                    <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 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>

Why don't I get an error for the duplicate ref values? Arrrggghhh! I've read everything on the internet. Please help someone.

Upvotes: 5

Views: 1121

Answers (3)

Michael Kay
Michael Kay

Reputation: 163322

If you run this schema through Saxon-EE, it tells you:

Warning: on line 13 of test.xsd: The complex type ArtworkType does not allow a child element named {}artwork

which is basically telling you that you forgot to say that artwork is in a namespace, and therefore needs a prefix.

Upvotes: 1

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Use this:

<?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:sequence>
    </xs:complexType>

    <xs:unique name="uniqueRef">
      <xs:selector xpath="aw:artwork"/>
      <xs:field xpath="@ref"/>
    </xs:unique>

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

Upvotes: 3

Jon Egerton
Jon Egerton

Reputation: 41549

Have you looked at this question to see if its similar - the asker posted an anwer to his own question.

How make univoque my enumeration by xs:unique

Upvotes: 1

Related Questions