Trying to define a unique id

Having trouble installing Oxygen? Got a bug to report? Post it all here.
danwhite
Posts: 10
Joined: Wed Jul 07, 2004 7:45 pm

Trying to define a unique id

Post by danwhite »

I think I want to use xs:key as opposed to xs:unique because xs:key cannot be NULL.

OK, here's my schema fragment:

Code: Select all


	<xs:complexType name="sectorType">
<xs:sequence>
<xs:element name="id">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:key name="sec_id">
<xs:selector xpath=".//sectorType"/>
<xs:field xpath="id"/>
</xs:key>
</xs:element>
<xs:element thing1 …/>
<xs:element thing2 …/>
<xs:element thing3 …/>
</xs:sequence>
</xs:complexType>

<xs:element name="sector" type="sectorType" minOccurs="1" maxOccurs="10">
There are no more than 10 sectors, numbered 1 thru 10.

But the following XML file comes up valid. What did I miss ?

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">
<sector>
<id>1</id>
<thing1>…</thing1>
<thing2>…</thing2>
<thing3>…</thing3>
</sector>
<sector>
<id>2</id>
<thing1>…</thing1>
<thing2>…</thing2>
<thing3>…</thing3>
</sector>
<sector>
<id>1</id>
<thing1>…</thing1>
<thing2>…</thing2>
<thing3>…</thing3>
</sector>
</foo>
[/code]
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Dan,

You should define the foo element to contain sector elements in your schema. Then the place where you should define the key is the foo element declaration so that you will be able to select all the sector elements in the key selector. The following schema should work ok with your document:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="sectorType">
<xs:sequence>
<xs:element name="id">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="thing1"/>
<xs:element name="thing2"/>
<xs:element name="thing3"/>
</xs:sequence>
</xs:complexType>
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="sector" type="sectorType" minOccurs="1" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
<xs:key name="sec_id">
<xs:selector xpath=".//sector"/>
<xs:field xpath="id"/>
</xs:key>
</xs:element>
</xs:schema>
Hope that helps,
George
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Just a small note:
george wrote:

Code: Select all


        <xs:key name="sec_id">
<xs:selector xpath=".//sector"/>
<xs:field xpath="id"/>
</xs:key>
can be better written as

Code: Select all


        <xs:key name="sec_id">
<xs:selector xpath="sector"/>
<xs:field xpath="id"/>
</xs:key>
taking into account that the sector can appear only as child of the foo element.

Best Regards,
George
danwhite
Posts: 10
Joined: Wed Jul 07, 2004 7:45 pm

Post by danwhite »

Many thanks. It worked :D

As to how I wrotye the xs:key, I was shooting blindly there :lol: :wink:
Post Reply