XML schema uniqueness constraints

Olaf Meyer

Usenet Archive
Joined
Jan 9, 2004
Messages
10
Reaction score
0
I'm wondering if I can also express uniquness constraints on attributes of objects that I have referenced from somewhere else.

To be a bit more precise here a short and simple XML document. The "objects" have a key constraint on the id attribute, i.e. they need to be unique. The object-ref element may reference the previously defined objects and hence has a keyref constraint on the id attribute.

I would now like to furhter restrict the object-ref elements by only allowing refrerences, such that the class attributes of the referred objects are unique. Is there an elegant way to do this via a XML uniquness constraint?

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.mynames.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mynames.org x:/work/sjrodc/xml/constraint.xsd"> <my-objects> <object id="1" class="Car"/> <object id="2" class="Car"/> <object id="3" class="Bike"/> </my-objects> <object-ref id="1"/> <!-- How can I specify a constraint in the schema such that only objects with distinct classes can be referenced? i.e. only one of the objects with id 1 or 2 can be selected --> <!-- this should cause a constraint violation --> <object-ref id="2"/>

<object-ref id="3"/>
</root>

I already have a schema file that has a key constraint on the objects id attribute and a keyref constraint on the object-ref id attribute. Is it possible to add another constraint to get the above behavior?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.mynames.org" xmlns:mf="http://www.mynames.org" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.mynames.org" elementFormDefault="qualified"> <xs:element name="root"> <xs:complexType name=""> <xs:sequence> <xs:element name="my-objects"> <xs:complexType> <xs:sequence> <xs:element name="object" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="id" use="required"/> <xs:attribute name="class" use="required"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="object-ref" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="id"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType>

<!-- here are the constraints -->
<xs:key name="objectIdKey">
<xs:selector xpath=".//mf:eek:bject"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:keyref name="objectRef" refer="objectIdKey">
<xs:selector xpath=".//mf:eek:bject-ref"/>
<xs:field xpath="@id"/>
</xs:keyref>
<xs:unique name="classUnique">
<xs:selector xpath=".//mf:eek:bject"/>
<xs:field xpath="@id"/>
</xs:unique>

</xs:element>
</xs:schema>

Thanks,
Olaf
 

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
548,208
Messages
3,104,798
Members
249,594
Latest member
henrywalsh

Latest Threads

Top