473,605 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML_Schema: problem with <xs:unique>

I googled for it for few houres, but didn't resolve it, any hints will be
thinkfull.

I want to use the following XSD file to guarantee <element1is unique, but
it doesn't work.

The following is my XSD file and XML file:

----------- test.xsd ----------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.aray.com" targetNamespace ="http://www.aray.com"
elementFormDefa ult="qualified" >

<xs:element name="root" type="rootType" >
<xs:unique name="PK_elemen t1">
<xs:selector xpath="./element1"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:unique>
</xs:element>

<xs:complexTy pe name="rootType" >
<xs:choice>
<xs:group ref="elementGro up" maxOccurs="unbo unded" minOccurs="0" />
</xs:choice>
</xs:complexType>

<xs:group name="elementGr oup">
<xs:choice>
<xs:element name="element1" type="xs:string " />
<xs:element name="element2" type="xs:string " />
</xs:choice>
</xs:group>

</xs:schema>
----------- test.xml ----------------
<?xml version="1.0" encoding="UTF-8"?>

<root xmlns="http://www.aray.com"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocat ion="http://www.aray.com test.xsd"
>
<element1>1</element1>
<element2>1</element2>
<element1>1</element1>
<!--
I expect this xml file is not valide, because the two <element1has the
same content.
But it is valide
-->
</root>

--

Jan 5 '07 #1
2 3712
Aray wrote:
I googled for it for few houres, but didn't resolve it, any hints will be
thinkfull.

I want to use the following XSD file to guarantee <element1is unique, but
it doesn't work.

The following is my XSD file and XML file:

----------- test.xsd ----------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.aray.com" targetNamespace ="http://www.aray.com"
elementFormDefa ult="qualified" >

<xs:element name="root" type="rootType" >
<xs:unique name="PK_elemen t1">
<xs:selector xpath="./element1"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:unique>
</xs:element>

<xs:complexTy pe name="rootType" >
<xs:choice>
<xs:group ref="elementGro up" maxOccurs="unbo unded" minOccurs="0" />
</xs:choice>
</xs:complexType>

<xs:group name="elementGr oup">
<xs:choice>
<xs:element name="element1" type="xs:string " />
<xs:element name="element2" type="xs:string " />
</xs:choice>
</xs:group>

</xs:schema>
----------- test.xml ----------------
<?xml version="1.0" encoding="UTF-8"?>

<root xmlns="http://www.aray.com"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocat ion="http://www.aray.com test.xsd"
<element1>1</element1>
<element2>1</element2>
<element1>1</element1>
<!--
I expect this xml file is not valide, because the two <element1has the
same content.
But it is valide
-->
</root>
You need to specify the namespace in the XPath expression, otherwise
"element1" is taken to be in "no namespace", which is not the case.
That would work if there were no namespace involved at all. Here is the
modified start of your schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.aray.com" targetNamespace ="http://www.aray.com"
xmlns:ns="http://www.aray.com"
elementFormDefa ult="qualified" >

<xs:element name="root" type="rootType" >
<xs:unique name="PK_elemen t1">
<xs:selector xpath="./ns:element1"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:unique>
</xs:element>
....

$ xmllint --noout --schema test.xsd test.xml
test.xml:9: element element1: Schemas validity error : Element
'{http://www.aray.com}el ement1': Duplicate key-sequence ['1'].
test.xml fails to validate

Jan 10 '07 #2
Thanks a lot

--

"Arndt Jonasson" <ar************ @gmail.com????
news:11******** **************@ i39g2000hsf.goo glegroups.com.. .
You need to specify the namespace in the XPath expression, otherwise
"element1" is taken to be in "no namespace", which is not the case.
That would work if there were no namespace involved at all. Here is the
modified start of your schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.aray.com" targetNamespace ="http://www.aray.com"
xmlns:ns="http://www.aray.com"
elementFormDefa ult="qualified" >

<xs:element name="root" type="rootType" >
<xs:unique name="PK_elemen t1">
<xs:selector xpath="./ns:element1"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:unique>
</xs:element>
...

$ xmllint --noout --schema test.xsd test.xml
test.xml:9: element element1: Schemas validity error : Element
'{http://www.aray.com}el ement1': Duplicate key-sequence ['1'].
test.xml fails to validate

Jan 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2699
by: Ulf Heyder | last post by:
Hello everyone, I want to add a unique constraint to a XSD I created. After I modified validation (XMLSpy, Castor-0.9.5 marshaller) of my example (see below) against the XSD (also see below) doesn't lead to any errors/warnings. I tried several different combinations for the <xs:selector> and <xs:field>.
2
2204
by: sachinik19 | last post by:
Hi, xml file : ------------- <?xml version='1.0'?> <query xmlns='jabber:iq:privacy' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="jabber:iq:privacy privacy.xsd"> <list name="hello"> <item action='allow' order="1"/>
1
4016
by: cedric.louyot | last post by:
Hi, I've written a schema that looks like : <xs:schema> <xs:complexType name="myType"> <xs:sequence> <xs:element name="e1" type="T1" maxOccurs="unbounded"/> <xs:element name="e2" type="xs:string"/> </xs:sequence>
0
8001
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8424
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8415
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8069
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8286
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.