Re: Selector Clarification

Hi Rahul,

> I have two small schemas for an instance file defining Id/y
> constraints. One works and the other does not. I doubt the other one
> is invalid.

You're correct that it isn't invalid, but it doesn't do what you want
it to do. Looking at the one that works first:

> schema1: This works
>
> <xsd:element name="root">
>   <xsd:complexType>
>     <xsd:sequence>
>       <xsd:element name="Book" maxOccurs="unbounded">
>         <xsd:complexType>
>           <xsd:sequence>
>             <xsd:element name="isbn" type="xsd:string"/>
>           </xsd:sequence>
>           <xsd:attribute name="name" type="xsd:string" use="required"/>
>         </xsd:complexType>
>       </xsd:element>
>     </xsd:sequence>
>   </xsd:complexType>
>
>   <xsd:key name="BookKey">
>     <xsd:selector xpath="./Book"/>
>     <xsd:field xpath="isbn"/>
>   </xsd:key>
>
> </xsd:element>

The key says:

  "Within each <root> element, every <Book> element child of that <root>
   element must have a unique value for its child <isbn> element."

In other words, no two <Book> elements within the <root> element can
have the same value for their <isbn> element child.

On the other hand, in the one that doesn't work:

> schema2: This does not works
>
> <xsd:element name="root">
>   <xsd:complexType>
>     <xsd:sequence>
>       <xsd:element name="Book" maxOccurs="unbounded">
>         <xsd:complexType>
>           <xsd:sequence>
>             <xsd:element name="isbn" type="xsd:string"/>
>           </xsd:sequence>
>           <xsd:attribute name="name" type="xsd:string" use="required"/>
>         </xsd:complexType>
>
>         <xsd:key name="BookKey">
>           <xsd:selector xpath="."/>
>           <xsd:field xpath="isbn"/>
>         </xsd:key>
>
>       </xsd:element>
>     </xsd:sequence>
>   </xsd:complexType>
> </xsd:element>

The key says:

  "Within each <Book> element, that <Book> element itself must have a
   unique value for its <isbn> element child."

All that this actually tests is that the <Book> element doesn't have
two <isbn> element children. You are only testing whether one <Book>
element has a unique value.

The location of the identity constraint determines the scope in which
it holds; the selector selects the elements that should have unique
values; the fields select the attributes and elements of those
elements that should be combined to give the unique values.

Basically, the only reason you'd ever want to use an XPath of '.'
within xs:selector is if you were using the identity constraint as a
hack to test a co-occurrence constraint.

Cheers,

Jeni

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

Received on Monday, 8 April 2002 05:12:31 UTC