XML schema uniqueness constraints

Discussion in 'XML' started by Olaf Meyer, Jan 15, 2004.

  1. Olaf Meyer

    Olaf Meyer Guest

    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
    Olaf Meyer, Jan 15, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ognen Ivanovski
    Replies:
    0
    Views:
    939
    Ognen Ivanovski
    Jul 15, 2003
  2. Victor
    Replies:
    3
    Views:
    958
    Martin Honnen
    Feb 6, 2004
  3. Robert Ludewig

    force uniqueness in xml schema

    Robert Ludewig, Jun 28, 2004, in forum: XML
    Replies:
    1
    Views:
    369
    Martin Honnen
    Jun 28, 2004
  4. Robert Ludewig

    force uniqueness with xml schema

    Robert Ludewig, Jun 28, 2004, in forum: XML
    Replies:
    1
    Views:
    418
  5. Adam Gardner
    Replies:
    5
    Views:
    140
    Sebastian Hungerecker
    Nov 19, 2008
Loading...

Share This Page