Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Understanding keyref constraints in XML Schema

From: Kasimier Buchcik <kbuchcik@---------.-->
To: Sharon Krisher <sharonkr@---------.---.-->
Date: 12/3/2004 3:40:00 PM
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>

From Xan.Gregg@j... Fri Dec 03 13:43:52 2004
Received: from lisa.w3.org ([128.30.52.41])
	by frink.w3.org with esmtp (Exim 4.34)
	id 1CaDiy-0000xP-NB
	for xmlschema-dev@l...; Fri, 03 Dec 2004 13:43:52 +0000
Received: from merc94.na


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent