[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Can't get simple key constraint to work

  • From: "Karol Zielonko" <karol.zielonko@g...>
  • To: <xml-dev@l...>
  • Date: Sat, 29 Nov 2008 19:02:33 -0500

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? 



[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.