Connecting Tech Pros Worldwide Help | Site Map

XML Schema: unique constraint + target namespace

  #1  
Old September 25th, 2008, 10:35 PM
tthunder@gmx.de
Guest
 
Posts: n/a
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
  #2  
Old September 26th, 2008, 12:45 PM
Martin Honnen
Guest
 
Posts: n/a

re: XML Schema: unique constraint + target namespace


tthunder@gmx.de wrote:
Quote:
<xs:unique name="dateAndProdNumKey">
<xs:selector xpath="Sect"/>
<xs:field xpath="no"/>
</xs:unique>
</xs:element>
Quote:
<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/
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML Schema (XSD): allow an attribute OR a sub-element NOT BOTH Eric answers 4 March 7th, 2007 11:35 PM
Either element or attribute in XSD gisleyt@gmail.com answers 2 April 25th, 2006 11:15 PM
[XXE] XMLmind XML Editor V3.0 Patch 1 Olivier Ishacian answers 0 December 5th, 2005 02:25 PM