Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML_Schema: problem with <xs:unique>

Reply
Thread Tools

XML_Schema: problem with <xs:unique>

 
 
Aray
Guest
Posts: n/a
 
      01-05-2007
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 <element1> is 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 xmlnss="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.aray.com" targetNamespace="http://www.aray.com"
elementFormDefault="qualified">

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

<xs:complexType name="rootType">
<xs:choice>
<xs:group ref="elementGroup" maxOccurs="unbounded" minOccurs="0" />
</xs:choice>
</xs:complexType>

<xs:group name="elementGroup">
<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"
xmlnssi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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 <element1> has the
same content.
But it is valide
-->
</root>



--



 
Reply With Quote
 
 
 
 
Arndt Jonasson
Guest
Posts: n/a
 
      01-10-2007
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 <element1> is 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 xmlnss="http://www.w3.org/2001/XMLSchema"
> xmlns="http://www.aray.com" targetNamespace="http://www.aray.com"
> elementFormDefault="qualified">
>
> <xs:element name="root" type="rootType">
> <xs:unique name="PK_element1">
> <xs:selector xpath="./element1"></xs:selector>
> <xs:field xpath="."></xs:field>
> </xs:unique>
> </xs:element>
>
> <xs:complexType name="rootType">
> <xs:choice>
> <xs:group ref="elementGroup" maxOccurs="unbounded" minOccurs="0" />
> </xs:choice>
> </xs:complexType>
>
> <xs:group name="elementGroup">
> <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"
> xmlnssi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="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 <element1> has 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 xmlnss="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.aray.com" targetNamespace="http://www.aray.com"
xmlns:ns="http://www.aray.com"
elementFormDefault="qualified">

<xs:element name="root" type="rootType">
<xs:unique name="PK_element1">
<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}element1': Duplicate key-sequence ['1'].
test.xml fails to validate

 
Reply With Quote
 
 
 
 
Aray
Guest
Posts: n/a
 
      01-15-2007
Thanks a lot

--

"Arndt Jonasson" <> ????
news: oups.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 xmlnss="http://www.w3.org/2001/XMLSchema"
> xmlns="http://www.aray.com" targetNamespace="http://www.aray.com"
> xmlns:ns="http://www.aray.com"
> elementFormDefault="qualified">
>
> <xs:element name="root" type="rootType">
> <xs:unique name="PK_element1">
> <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}element1': Duplicate key-sequence ['1'].
> test.xml fails to validate
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57