473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 6011
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olaf Meyer | last post by:
Apprentently xerces 2.6.0 (Java) does not validate against contraints specified in the schema (e.g. constraints specified via unique element). The validation works with the XML editor I'm using...
0
by: Olaf Meyer | last post by:
I'm wondering if I can also express uniquness constraints on attributes of objects that I have referenced from somewhere else. To be a bit more precise here a short and simple XML document. The...
2
by: John Carron | last post by:
Hi All, I have written a simple schema (see below) that uses substitution groups. I don't know if this the correct usage because I'm fairly new to xml schema. The structure is as follows: ...
7
by: Robert Stearns | last post by:
I ran the following bit of SQL and my PRIMARY KEY wound up in schema SYSIBM called SQL.... not schema is3 called primary. The index registation did wind up there. Obviously there's something I...
5
by: Lemon Tree | last post by:
Hello everybody. I have a problem with Schema Definitions and I cannot figure out how to solve it (provided that it could be solved :)) Here we go... Let's suppose to have a simple XML file...
1
by: Nichino | last post by:
Hello, I'm trying to define my first Xml Schema and have a problem. The XML I'd like to define through the Schema is the following one. Can you help me please? I don't know how to insert the 3...
13
by: Martin Z | last post by:
I'm making a CRUD screen for an Oracle database... but problem is that the primary key in that table is populated via an autonumber, which in Oracle is done just with triggers and sequences. ...
4
by: Eric | last post by:
Attached is an example of my question. Note the "values" attribute is optional. Also the <valuesub-element is optional. Here, the XML can contain, 1 or both or neither. I would like to allow...
0
by: icesign | last post by:
I know that the selector of these elements has a scope relative to the element being declared, but maybe there is a way to get beyond bounds of this scope or maybe just a way to extend base element?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.