469,643 Members | 1,225 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

XML Schema: unique constraint + target namespace

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
Sep 25 '08 #1
1 5790
tt******@gmx.de wrote:
<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>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 26 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Olaf Meyer | last post: by
2 posts views Thread by John Carron | last post: by
7 posts views Thread by Robert Stearns | last post: by
5 posts views Thread by Lemon Tree | last post: by
1 post views Thread by Nichino | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.