← Prev in month ← Prev in thread

Can't get simple key constraint to work

From
Karol Zielonko <>
To
Date
2008-11-30T00:02:33Z
ID
<7C7CB146E4214B83B492D40575B0F86E@KZVLT1>
Thread
Can't get simple key constraint to work
Hi,

I'm a newbie to XML schema design and am having trouble getting what would 
seem
to be a painfully simple key constraint to work. I've looked at tutorial 
pages
on the creation of keys and use of Xpath therein and still no joy.

Here is the stripped down schema, with my futile attempt at a key constraint
named "OuterNameKey":

 <?xml version="1.0" encoding="UTF-8" ?>
 <xsd:schema targetNamespace="http://TBD-URI" elementFormDefault="qualified"
  attributeFormDefault="unqualified" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="http://TBD-URI">

  <xsd:element name="MyRoot" type="MyRootType">
     <xsd:key name="OuterNameKey">
        <xsd:selector xpath="./Outer"/>
        <xsd:field xpath="@OuterName"/>
     </xsd:key>
  </xsd:element>

  <xsd:complexType name="MyRootType">
    <xsd:sequence>
       <xsd:element name="Outer" type="OuterType" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="OuterType">
     <xsd:attribute name="OuterName" type="xsd:string" use="required"/>
  </xsd:complexType>
 </xsd:schema>

I want the OuterNameKey key constraint to say, "The value of the OuterName
attribute of the <Outer> element must be unique within the enclosing 
<MyRoot>
element.".

Here is a test XML file that (as far as I can understand the Xpath / XML key
tutorials I've seen) should *not* validate because a duplicate value for
OuterName has been used in two <Outer> elements:

 <?xml version="1.0" encoding="UTF-8"?>
 <MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://TBD-URI Example.xsd" xmlns="http://TBD-URI">
     <Outer OuterName="Must Be Unique"/>
     <Outer OuterName="Must Be Unique"/>
 </MyRoot>

I'm using Oxygen and when I hit validate for the above XML it validates.

Now I *was* able to get the following version of the "OuterNameKey" key
constraint to catch the error in the XML above. I changed the xpath in
xsd:selector to "*", that is:

   <xsd:key name="OuterNameKey">
      <xsd:selector xpath="*"/>
      <xsd:field xpath="@OuterName"/>
   </xsd:key>

If the change above is made to the schema then the example XML will not
validate. Oxygen will say:

 Duplicate key value [Must Be Unique] declared for identity constraint
 "OuterNameKey" of element "MyRoot".

which is what I want, except I don't want to use this wildcard hack.

So what am I missing?
← Prev in month ← Prev in thread