Click to See Complete Forum and Search --> : How to use key, key ref in XSD


fhurtad
February 1st, 2012, 11:33 AM
Hi all:
I am trying to use keys and keyrefs in XSD without any success (I am a newbie in XSD). For example, this XSD does not work as the attached XML object demonstrates:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/sc"
xmlns:fh="http://www.example.org/sc" elementFormDefault="qualified">
<element name="Orders">
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<element name="AnOrder">
<complexType>
<attribute name="orderId" type="string" use="required" />
</complexType>
</element>
</sequence>
</complexType>
<unique name="orderKey">
<selector xpath="Orders/Order" />
<field xpath="@orderId" />
</unique>
</element>
</schema>

I thought that with this definitions any order must have a unique orderId attribute.
The prof that this does not work is this XML object (I am validating the objects with the Eclipse support tools):

<?xml version="1.0" encoding="UTF-8"?>
<fh:Orders xmlns:fh="http://www.example.org/sc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/sc sc.xsd ">
<fh:AnOrder orderId="1" />
<fh:AnOrder orderId="1" />
</fh:Orders>
Coul anybody with some skills in XSD help me with this?

fhurtad
February 1st, 2012, 02:31 PM
I found a solution using elements instead of attributes:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.fhgg.org"
xmlns="http://www.fhgg.org"
xmlns:fh="http://www.fhgg.org"
elementFormDefault="qualified">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Orders">
<xs:complexType>
<xs:sequence>
<xs:element name="AnOrder" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="orderId" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="orderKey">
<xs:selector xpath="fh:Orders/fh:AnOrder" />
<xs:field xpath="fh:orderId" />
</xs:key>
</xs:element>
</xs:schema>