[XSD] Retriction to IDREF

Discussion in 'XML' started by Nicolas Bouillon, Mar 2, 2004.

  1. Hi

    I am writing a schema and i want to have an element's attribute, which
    is an IDREF to an other type of element.

    For example

    <Type1 id="id1"/>
    <Type2 id="id2"/>

    With
    <Type3 idref="id1"/>
    valid

    but
    <Type3 idref="id2"/>
    invalid, because only reference to element of "Type1" are allowed...

    Can we do that with XML Schema ? How ?

    Thanks.
    Nicolas.
     
    Nicolas Bouillon, Mar 2, 2004
    #1
    1. Advertisements

  2. Nicolas Bouillon wrote:

    > I am writing a schema and i want to have an element's attribute, which
    > is an IDREF to an other type of element.
    >
    > For example
    >
    > <Type1 id="id1"/>
    > <Type2 id="id2"/>
    >
    > With
    > <Type3 idref="id1"/>
    > valid
    >
    > but
    > <Type3 idref="id2"/>
    > invalid, because only reference to element of "Type1" are allowed...
    >
    > Can we do that with XML Schema ? How ?


    If you only use the types xs:ID and xs:IDREF then you can't specify the
    restriction, however with W3C XML Schema you can additionally define
    key/keyref constraints and thereby specify the restriction, here is an
    example schema

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="root">
    <xs:complexType>
    <xs:sequence maxOccurs="unbounded">
    <xs:element ref="Type1" maxOccurs="unbounded" />
    <xs:element ref="Type2" maxOccurs="unbounded" />
    <xs:element ref="Type3" maxOccurs="unbounded" />
    </xs:sequence>
    </xs:complexType>
    <xs:key name="Type1Key">
    <xs:selector xpath="Type1" />
    <xs:field xpath="@id" />
    </xs:key>
    <xs:keyref name="keyRef" refer="Type1Key">
    <xs:selector xpath="Type3" />
    <xs:field xpath="@idref" />
    </xs:keyref>
    </xs:element>

    <xs:element name="Type1">
    <xs:complexType>
    <xs:attribute name="id" type="xs:ID" />
    </xs:complexType>
    </xs:element>

    <xs:element name="Type2">
    <xs:complexType>
    <xs:attribute name="id" type="xs:ID" />
    </xs:complexType>
    </xs:element>

    <xs:element name="Type3">
    <xs:complexType>
    <xs:attribute name="idref" type="xs:IDREF" />
    </xs:complexType>
    </xs:element>

    </xs:schema>

    which when used to validate against the example instance XML

    <?xml version="1.0" encoding="UTF-8"?>
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test20040302Xsd.xml">
    <Type1 id="id1"/>
    <Type2 id="id2" />
    <Type3 idref="id1"/>
    <Type3 idref="id2"/>
    </root>

    flags an error for idref="id2"

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 2, 2004
    #2
    1. Advertisements

  3. Martin Honnen wrote:
    > If you only use the types xs:ID and xs:IDREF then you can't specify the
    > restriction, however with W3C XML Schema you can additionally define
    > key/keyref constraints and thereby specify the restriction, here is an
    > example schema


    Thanks for this excellent answer.
     
    Nicolas Bouillon, Mar 2, 2004
    #3
    1. Advertisements

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. Markus
    Replies:
    1
    Views:
    1,642
    Markus
    Nov 22, 2005
  2. Rick Razzano

    XSD document for XSD defintion

    Rick Razzano, Sep 26, 2003, in forum: XML
    Replies:
    1
    Views:
    702
    C. M. Sperberg-McQueen
    Sep 26, 2003
  3. Replies:
    1
    Views:
    1,200
    Martin Honnen
    Jan 14, 2004
  4. Peter Aberline

    xsd:any as a child of xsd:all

    Peter Aberline, Apr 5, 2004, in forum: XML
    Replies:
    0
    Views:
    1,013
    Peter Aberline
    Apr 5, 2004
  5. Bernd Oninger
    Replies:
    1
    Views:
    745
    Henry S. Thompson
    Jun 30, 2004
  6. Burak
    Replies:
    0
    Views:
    679
    Burak
    Jul 1, 2004
  7. Matt
    Replies:
    3
    Views:
    37,707
    MKNDK
    Sep 11, 2008
  8. stiank81
    Replies:
    4
    Views:
    16,040
    stiank81
    Jun 26, 2005
Loading...