XML Key Validation

J

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?

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
 
M

Martin Honnen

Jeff said:
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>

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>
 
J

Jeff Hosler

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
 
M

Martin Honnen

Jeff Hosler wrote:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top