"./Outer" doesn't select the "Outer" declared in your schema. This is because the default namespace does not apply to node tests [1] in XPath expressions. You must use prefixes for names which have a namespace. The following should work:
<?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:tns="http://TBD-URI">
<xsd:element name="MyRoot" type="tns:MyRootType">
<xsd:key name="OuterNameKey">
<xsd:selector xpath="./tns:Outer"/>
<xsd:field xpath="@OuterName"/>
</xsd:key>
</xsd:element>
<xsd:complexType name="MyRootType">
<xsd:sequence>
<xsd:element name="Outer" type="tns:OuterType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="OuterType">
<xsd:attribute name="OuterName" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:schema>
Thanks.
[1] http://www.w3.org/TR/xpath#node-tests
Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org
"Karol Zielonko" <karol.zielonko@gmail.com> wrote on 11/29/2008 07:02:33 PM:
> 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?
>
> _______________________________________________________________________
>
> XML-DEV is a publicly archived, unmoderated list hosted by OASIS
> to support XML implementation and development. To minimize
> spam in the archives, you must subscribe before posting.
>
> [Un]Subscribe/change address: http://www.oasis-open.org/mlmanage/
> Or unsubscribe: xml-dev-unsubscribe@lists.xml.org
> subscribe: xml-dev-subscribe@lists.xml.org
> List archive: http://lists.xml.org/archives/xml-dev/
> List Guidelines: http://www.oasis-open.org/maillists/guidelines.php