KarmaEDV
KarmaEDV

Reputation: 1691

XSD Schema Unique Constraint not Working for Numeric Values?

Consider this XSD Schema, resulting in the following graph

<?xml version="1.0" encoding="UTF-8"?>
<s:schema xmlns:s="http://www.w3.org/2001/XMLSchema" 
    xmlns:target="http://www.url.com/validation" 
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
    elementFormDefault="qualified" attributeFormDefault="unqualified">

    <s:element name="myObjects" type="typeObjects">
        <s:unique name="uniqueObjectId">
            <s:selector xpath="myObject"/>
            <s:field xpath="id"/>
        </s:unique>
    </s:element>
    <s:complexType name="typeObjects">
        <s:sequence maxOccurs="unbounded">
            <s:element name="myObject">
                <s:complexType>
                    <s:complexContent>
                        <s:extension base="typeObject"/>
                    </s:complexContent>
                </s:complexType>
            </s:element>
        </s:sequence>
    </s:complexType>
    <s:complexType name="typeObject">
        <s:sequence>
            <s:element name="id" type="s:int"/>
        </s:sequence>
    </s:complexType>
</s:schema>

still passes validation

Note the unique constraint set on the "id" element, typed as s:int.

The following instance XML still validates, even having the same <id>1</id> twice!

<?xml version="1.0" encoding="utf-8"?>
<myObjects 
    xmlns:target="http://www.url.ch/validation" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="unique.xsd">

    <myObject>
        <id>1</id>
    </myObject>

    <myObject>
        <id>1</id>
    </myObject>
</myObjects>

I can force the fail condition if I change the element type to s:string

This will not validate the instance XML anymore, as expected: fails validation as expected

I tried the validation in oXygen XML and ALtova XML Spy with the same result respectively.

The question is obviously if this a bug or a known limitation in the unique constraint implementation of XSD?

Upvotes: 0

Views: 333

Answers (2)

KarmaEDV
KarmaEDV

Reputation: 1691

I think I discovered where the problem lies. When using the validation engine Xerces in oXygen the validation passes if the element has the type int. When using the Saxon engine to parse it works either way!

Therefor pretty sure it is a bug in Xerces..

enter image description here

Upvotes: 0

Filip Skurniak
Filip Skurniak

Reputation: 46

Your xsd looks good and I confirmed it with oxygen. I got the expected error:

cvc-identity-constraint.4.1: Duplicate unique value [1] declared for identity 
constraint "uniqueObjectId" of element "myObjects".

I see that validation works fine for SAXON-EE 9.6.0.7

Non-unique value found for constraint uniqueObjectId: 1

and for other engines.

Try checking your validation scenarios, caching and file names.

Upvotes: 0

Related Questions