W3C home > Mailing lists > Public > xmlschema-dev@w3.org > December 2004

Re: Understanding keyref constraints in XML Schema

From: Kasimier Buchcik <kbuchcik@4commerce.de>
Date: Fri, 03 Dec 2004 14:40:03 +0100
Message-ID: <41B06CB3.1020301@4commerce.de>
To: Sharon Krisher <sharonkr@netvision.net.il>
CC: xmlschema-dev@w3.org

Hi,

Sharon Krisher wrote:
> Hello all.
> I've been reading XML Schema Part 1 - Structures, and trying to 
> understand the semantics of keys and keyrefs, and the rules by which 
> documents are validated with respect to these constraints.. 
> Specifically: given definitions of a key and a keyref, and a node in a 
> document, I want to know which nodes are considered to be referencing it 
> (according to the keyref). In the attached pdf file I've summarized the 
> conclusions I've reached regarding keyref references in XML Schema. 
> Since the standard 
> (http://www.w3.org/TR/xmlschema-1/#cvc-identity-constraint) is not very 
> clear, I don't know whether my observations are correct. I'd appreciate 
> any comments regarding the attached document.

I hope that your observations are correct, since this is how I read the
spec here as well.

I've tried to create a schema + instance as shown below, reflecting the
example tree in your document. The results from the three validators I
used as reference are:

Xerces J- 2.6.2:
   valid

XSV 2.7-1 of 2004/04/01:
   valid

MSXML 4.0:
   _not_ valid (with a strange message):
   "The keyref '{http://FOO}theKey' does not resolve to a key for the
   Identity Constraint '{http://FOO}theKeyref'"


Removing the element "/r/a/d/c/d.val" I get:

Xerces J- 2.6.2:
   valid

XSV 2.7-1 of 2004/04/01:
   _not_ valid:
   "no key in {http://FOO}:theKey for (u'3', u'4')"

MSXML 4.0:
   _not_ valid (with the same message as above)

So XSV seems to follow your observations, but the other two validators
(at least on my side) not. So I hope someone can explain those
differences.

Regards,

Kasimier


file: idc1_0.xsd
(note that I tried to go without the numbers on the element names for
'c' and 'd')
----------------
<?xml version="1.0"?>
<xsd:schema
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   targetNamespace="http://FOO"
   xmlns="http://FOO"
   xmlns:f="http://FOO">

   <xsd:element name="r">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref="a"/>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>

   <!-- 'a' is the scoping element of 'theKeyref' -->
   <xsd:element name="a">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref="d"/>
         <!-- 'e' is the target node of 'theKeyref' -->
         <xsd:element ref="e"/>
       </xsd:sequence>
     </xsd:complexType>

     <xsd:keyref name="theKeyref" refer="theKey">
       <xsd:selector xpath="f:e"/>
       <xsd:field xpath="f:val.1"/>
       <xsd:field xpath="f:val.2"/>
     </xsd:keyref>
   </xsd:element>

   <xsd:element name="d">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref="c" minOccurs="0"/>
         <xsd:element ref="b" minOccurs="0"/>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>

   <xsd:element name="b">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref="c" maxOccurs="2"/>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>

   <!-- 'c' is the scoping element of 'theKey' -->
   <xsd:element name="c">
     <xsd:complexType>
       <xsd:sequence>
         <!-- 'd.val' is the target node of 'theKey' -->
         <xsd:element ref="d.val" minOccurs="0" maxOccurs="unbounded"/>
       </xsd:sequence>
     </xsd:complexType>
     <xsd:key name="theKey">
       <xsd:selector xpath="f:d.val"/>
       <xsd:field xpath="f:val.1"/>
       <xsd:field xpath="f:val.2"/>
     </xsd:key>
   </xsd:element>

   <!-- 'd.val' is the target node of 'theKey' -->
   <xsd:element name="d.val">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref="val.1"/>
         <xsd:element ref="val.2" minOccurs="0"/>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>

   <!-- 'e' is the target node of 'theKeyref' -->
   <xsd:element name="e">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref="val.1"/>
         <xsd:element ref="val.2"/>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>

   <!-- elements holding the key values -->
   <xsd:element name="val.1" type="xsd:string"/>
   <xsd:element name="val.2" type="xsd:string"/>
</xsd:schema>

file: idc1_0.xml
----------------
<?xml version="1.0"?>
<r xmlns="http://FOO"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://FOO idc1_0.xsd">
   <a> <!-- 'a' is the scoping element of 'theKeyref' -->
     <d>
       <c> <!-- 'c' is the scoping element of 'theKey' -->
         <d.val>
           <val.1>3</val.1>
           <val.2>4</val.2>
         </d.val>
       </c>
       <b>
         <c>
           <d.val>
             <!-- key-sequence of 'theKey' -->
             <val.1>1</val.1> <!-- key value no. 1 of 'theKey' --> 

             <val.2>2</val.2> <!-- key value no. 2 of 'theKey' --> 

           </d.val>
           <d.val>
             <!-- key-sequence of 'theKey' -->
             <val.1>3</val.1>
             <val.2>4</val.2>
           </d.val>
         </c>
         <c>
           <d.val>
             <!-- key-sequence of 'theKey' -->
             <val.1>1</val.1>
             <val.2>2</val.2>
           </d.val>
           <d.val>
             <!-- key-sequence of 'theKey' -->
             <val.1>3</val.1>
             <val.2>4</val.2>
           </d.val>
         </c>
       </b>
     </d>

     <e> <!-- 'e' is the target node of 'theKeyref' -->
       <val.1>3</val.1> <!-- key value no. 1 of 'theKeyref' -->
       <val.2>4</val.2> <!-- key value no. 2 of 'theKeyref' -->
     </e>
   </a>
</r>
Received on Friday, 3 December 2004 13:40:40 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:14:48 GMT