XML Schema: unique constraint + target namespace

T

tthunder

Hi @all,

Please check the following XML file and XML schema definition below
first:

-------
XML File (full):
-------

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="c:\example.xsd">
<Sect>
<no>6</no>
</Sect>
<Sect>
<no>6</no>
</Sect>
</Root>

-------
XML Schema File (full):
-------

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element ref="Sect" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="dateAndProdNumKey">
<xs:selector xpath="Sect"/>
<xs:field xpath="no"/>
</xs:unique>
</xs:element>
<xs:element name="Sect">
<xs:complexType>
<xs:sequence>
<xs:element ref="no"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="no" type="xs:integer"/>
</xs:schema>

-------

This example works perfect, or in other words, the validation fails,
because there is a voilation of the "unqiue" constraint.

Now, I change the files and use the following "options":

-------
XML File (snippet):
-------

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.org/graph" xsi:schemaLocation="http://
www.example.org/graph c:\example.xsd">

-------
XML Schema File (snippet):
-------

<xs:schema xmlns="http://www.example.org/graph" xmlns:xs="http://
www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/
graph" elementFormDefault="qualified"
attributeFormDefault="qualified">

-------

The only thing changed is, that I am using a target namespace.

Basically, validation still works. If I insert wrong elements, the
validation fails. Otherwise it's ok.
However, the unique constraint DOES NOT work any more! That means,
that the example XML postet above is considered valid!

I am testing about one day now... no clue, why validation does not
work as expected.
All validation tools I tested so far (XMLSpy, libxml2,...) consider
the XML file valid!

Thanks a lot in advance,
Kirsten
 
M

Martin Honnen

<xs:unique name="dateAndProdNumKey">
<xs:selector xpath="Sect"/>
<xs:field xpath="no"/>
</xs:unique>
</xs:element>

<xs:schema xmlns="http://www.example.org/graph" xmlns:xs="http://
www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/
graph" elementFormDefault="qualified"
attributeFormDefault="qualified">

-------

The only thing changed is, that I am using a target namespace.

Basically, validation still works. If I insert wrong elements, the
validation fails. Otherwise it's ok.
However, the unique constraint DOES NOT work any more! That means,
that the example XML postet above is considered valid!

The W3C XML schema language for unique and key constraints uses (a
subset of) the XPath 1.0 language. In that language the XPath expression
'Sect' always selects elements with local name 'Sect' in _no_ namespace.
However your schema after the changes has a target namespace
http://www.example.org/graph and defines its elements in that target
namespace while the unique constraint still applies to elements in no
namespace.
So you need to change those XPath expressions to

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified"
targetNamespace="http://www.example.org/graph"
xmlns:tns="http://www.example.org/graph">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element ref="tns:Sect" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="dateAndProdNumKey">
<xs:selector xpath="tns:Sect"/>
<xs:field xpath="tns:no"/>
</xs:unique>
</xs:element>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top