[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. Advertising

  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. Advertising

  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. 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. rico
    Replies:
    2
    Views:
    396
  2. kevkev

    JAXB and IdRef

    kevkev, May 18, 2005, in forum: Java
    Replies:
    3
    Views:
    4,292
    kevkev
    May 19, 2005
  3. Elhanan
    Replies:
    0
    Views:
    1,126
    Elhanan
    Mar 30, 2006
  4. Lars
    Replies:
    0
    Views:
    1,190
  5. Hauke Fuhrmann

    restrict IDREF to special elements

    Hauke Fuhrmann, Oct 26, 2003, in forum: XML
    Replies:
    1
    Views:
    636
    Mark Preston
    Oct 27, 2003
Loading...

Share This Page