Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify unique values in an XML schema

Tags:

xml

xpath

xsd

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.

like image 505
Patrick Avatar asked Oct 20 '11 11:10

Patrick


2 Answers

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>
like image 111
Kirill Polishchuk Avatar answered Sep 20 '22 13:09

Kirill Polishchuk


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

like image 45
Jon Egerton Avatar answered Sep 20 '22 13:09

Jon Egerton