Discussion:
XML Key Validation
(too old to reply)
Jeff Hosler
2005-05-23 03:20:19 UTC
Permalink
I am trying to get an XML schema to validate unique key name attributes, and
have been unsuccessful. Can anyone show me what I am doing wrong?

Here is the schema:

<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="bar_key">
<xsd:selector xpath=".//bar"/>
<xsd:field xpath="@name"/>
</xsd:key>
</xsd:element>

<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>

</xsd:schema>

-----------------------

Here is the XML file that should report being invalid:

<?xml version="1.0" encoding="UTF-8"?>
<foo
xmlns="http://foobartest"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://foobartest foo.xsd">

<bar name="One"/>
<bar name="Two"/>
<bar name="Three"/>
<bar name="One"/>
</foo>

----------------------------

The XML file should fail, but does not. Can anyone show me what is
incorrect in my schema?

Thank you for your help.

Jeff
Martin Honnen
2005-05-23 11:54:30 UTC
Permalink
Post by Jeff Hosler
I am trying to get an XML schema to validate unique key name attributes, and
have been unsuccessful. Can anyone show me what I am doing wrong?
<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="bar_key">
<xsd:selector xpath=".//bar"/>
</xsd:key>
</xsd:element>
<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:schema>
-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<foo
xmlns="http://foobartest"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://foobartest foo.xsd">
<bar name="One"/>
<bar name="Two"/>
<bar name="Three"/>
<bar name="One"/>
</foo>
----------------------------
The XML file should fail, but does not. Can anyone show me what is
incorrect in my schema?
As you use a target namespace your XPath expression needs a prefix bound
to that namespace. There is also the unique element which seems more
appropriate than a key here:

<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:pf1="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="unique-name">
<xsd:selector xpath="pf1:bar"/>
<xsd:field xpath="@name"/>
</xsd:unique>
</xsd:element>

<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>

</xsd:schema>
--
Martin Honnen
http://JavaScript.FAQTs.com/
Jeff Hosler
2005-05-24 01:30:53 UTC
Permalink
Thank you so much! This problem was driving me nuts.

Since you indicate that I should have used the "unique" identifier in this
case, what case might I use the "key" identifier?

Jeff
Post by Martin Honnen
Post by Jeff Hosler
I am trying to get an XML schema to validate unique key name attributes,
and have been unsuccessful. Can anyone show me what I am doing wrong?
<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="bar_key">
<xsd:selector xpath=".//bar"/>
</xsd:key>
</xsd:element>
<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:schema>
-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<foo
xmlns="http://foobartest"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://foobartest foo.xsd">
<bar name="One"/>
<bar name="Two"/>
<bar name="Three"/>
<bar name="One"/>
</foo>
----------------------------
The XML file should fail, but does not. Can anyone show me what is
incorrect in my schema?
As you use a target namespace your XPath expression needs a prefix bound
to that namespace. There is also the unique element which seems more
<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:pf1="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="unique-name">
<xsd:selector xpath="pf1:bar"/>
</xsd:unique>
</xsd:element>
<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:schema>
--
Martin Honnen
http://JavaScript.FAQTs.com/
Martin Honnen
2005-05-24 10:56:02 UTC
Permalink
Post by Jeff Hosler
Since you indicate that I should have used the "unique" identifier in this
case, what case might I use the "key" identifier?
If you elsewhere in the schema respectively the instance XML want to
have a keyref to reference keys.
--
Martin Honnen
http://JavaScript.FAQTs.com/
Jeff Hosler
2005-05-24 12:00:04 UTC
Permalink
Thanks Martin. You've been a huge help.

Cheers,

Jeff
Post by Jeff Hosler
Since you indicate that I should have used the "unique" identifier in
this case, what case might I use the "key" identifier?
If you elsewhere in the schema respectively the instance XML want to have
a keyref to reference keys.
--
Martin Honnen
http://JavaScript.FAQTs.com/
Loading...