Re: Problem with multi-field keys

"Jim Stanley" <JimS@Media-Services.Com> writes:

> I'm old to programming but new to XML and XML Schema.  I'm trying to
> define a unique or primary key on a parent-child combination (i.e.
> individual elements may not be unique, but the combination of element
> and category must be).  Although I think I'm forming the key correctly
> (you use "." to establish the node context, start from the parent and
> work downward to establish uniqueness), the sample document is telling
> me I have too many values - in both XMLSpy and MSXML, so I know I'm
> doing something wrong.

George Cristian Bina's answer is probably correct (although both his
and your Cat.PK1 are doing no work not already done by the content
model).  I say 'probably' because it's not completely clear to me from
your message and examples what you're trying to rule in/out.

You might find the section on keys/keyrefs in the (revised) Primer
helpful [1].

One way of trying to understand what George fixed in your schema is
that uniqueness is a property of items _in a set_.  The selector
pattern determines the set, namely those elements within _each
instance_ of the element being declared which match the selector
pattern form the set.  The field pattern then determines the key for
each selected element.  Each collection of keys thus determined must
have no duplicates.


So it's very rare that a key with <xs:selector path='.'/> all by
itself is of any use.

Your 'Category' element provides the domain within which the selector
pattern works -- I _think_ that means that the catName field is
already taken care of.  All the descendants of a given Category
already 'have' the same catName, by inheritance, as it were.

In other words, the tree structure of XML is doing some of the work
for you, that in a DB you would have to do explicitly.

I hope that if you look again at George's solution with this
explanation in hand things may be a bit clearer. . .

ht

[1] http://www.w3.org/TR/2004/PER-xmlschema-0-20040318/#specifyingUniqueness
-- 
 Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
                     Half-time member of W3C Team
    2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
            Fax: (44) 131 650-4587, e-mail: ht@inf.ed.ac.uk
                   URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]

Received on Thursday, 8 July 2004 05:14:22 UTC