Re: Problem with key!

Hi Cyrill,

> I road many article to the theme unique, key an keyref but i'm still
> not understanding how it works.
>
> In my projekt there is an element user with the attribute *id*. The
> user element can be found many times but the id must always be a
> different value.
>
> <user id="1">
>         <name></name>
>         <email></email>
> </user>
> <user id="2">
>         <name></name>
>         <email></email>
> </user>
>
> I thougt it would be possible with "key", wouldn't it? If not, how
> would you realize this problem?

Yes, this would be possible with an identity constraint -- either a
key or a unique identity constraint (probably a key would be
appropriate here -- I guess that all users have ids?).

When you use identity constraints you have to work out three things:

  - what element contains all the elements that you want to say
    have unique values?
  - what path leads from that element to the elements that you want to
    say have unique values?
  - what path leads from the elements that have unique values to those
    unique values?

The first identifies the scope of the identity constraint and lets you
know what element declaration you need to place the identity
constraint within. The second selects the unique elements themselves
-- you place that path in the xs:selector element within the identity
constraint. The third identifies the unique fields for the elements --
you place that path (or those paths) in the xs:field element(s) within
the identity constraint.

In your case, I'll assume that your user elements are all wrapped in a
"users" element. The element declaration and key definition will look
something like:

<xs:element name="users">
  <xs:complexType>
    <xs:element ref="user" maxOccurs="unbounded" />
  </xs:complexType>
  <!-- place the identity constraint an element that contains all the
       elements you're interested in -->
  <xs:key name="userIDs">
    <!-- select those elements using an XPath in the xs:selector -->
    <xs:selector xpath="user" />
    <!-- identify a unique field for those elements using an XPath in
         the xs:field -->
    <xs:field xpath="@id" />
  </xs:key>
</xs:element>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Monday, 29 July 2002 05:44:21 UTC