Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML schema uniqueness constraints

Reply
Thread Tools

XML schema uniqueness constraints

 
 
Olaf Meyer
Guest
Posts: n/a
 
      01-15-2004
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"
xmlnssi="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"
xmlnss="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=".//mfbject"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:keyref name="objectRef" refer="objectIdKey">
<xs:selector xpath=".//mfbject-ref"/>
<xs:field xpath="@id"/>
</xs:keyref>
<xs:unique name="classUnique">
<xs:selector xpath=".//mfbject"/>
<xs:field xpath="@id"/>
</xs:unique>

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


Thanks,
Olaf
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclassing Hash to enforce value uniqueness ala key uniqueness. Adam Gardner Ruby 5 11-19-2008 07:36 AM
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. Bobby Edward ASP .Net 4 11-03-2008 10:53 PM
Gridview error: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. Bobby Edward ASP .Net 1 10-14-2008 04:09 AM
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. bazzer ASP .Net 1 04-06-2006 01:31 PM
XML Schema keys, uniqueness based on ancestor's attribute Ognen Ivanovski XML 0 07-15-2003 02:36 PM



Advertisments