W3C home > Mailing lists > Public > xmlschema-dev@w3.org > February 2005

Re: Use of key/keyref - any best practices? warnings?

From: Nigel Hardy <nwh@aber.ac.uk>
Date: Sun, 27 Feb 2005 21:09:26 +0000
Message-ID: <42223706.9060506@aber.ac.uk>
To: xmlschema-dev@w3.org


I have used these for implementation of what would just be 1:N and 1:1 
referential integrity in a relational system and found no difficulty at 
that level. I think I lack the standard working methods and "mind set" 
that help carry me through design and implementation with relational or 
other traditional systems when it comes to using the facilities that 
key/keyref offer. Once off the "beaten track" and into more complicated 
territory it is easy to get in a muddle. There is a lot of power there, 
but ...

I would  echo Jeff Rafter's following comments:

> (1) Scope. Like Michael Kay said scope is very important. If you are 
> not defining the keys and keyrefs in the same complexType then you 
> need to make sure that the keyref is on an ancestor of the key. 
> Otherwise you run into interop problems with different implementations.

I got bitten by this when trying to use a schema with the new Offfice 
2003 facilities (Excel in fact) to put some backbone into a spreadsheet 
for die hard users. Excel certainly seemed to have a different 
understanding of the rules from mine and that of the other processors I 
had used.

> (2) Namespaces. In order to correctly refer to a key in an XML Schema 
> with a targetNamespace and have it work the same across multiple 
> implementations, you *must* have the reference to the keyname be both 
> prefixed and qualified. Having your targetNamespace and default 
> namespace declaration be the same is not enough. I seem to recall 
> needing to have the XPath steps to the key prefixed and qualified as 
> well.

This is a pain and feels wrong when you have to do it - though the need 
for it is obvious on reflection. The XPath steps do need prefixing. An 
issue with this is that you will get the schema parsed and the 
constraints you want will (silently) not be imposed if you forget the 

Nigel Hardy

Nigel Hardy     Tel: +44 1970 622 434.   http://users.aber.ac.uk/nwh/
Dept. Computer Sci,  University of Wales, Aberystwyth, SY23 3DB, UK
Adran Cyfrifiadureg, Prifysgol Cymru,     Aberystwyth, SY23 3DB, UK
Received on Sunday, 27 February 2005 21:10:10 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:15:27 UTC