[XSD] Retriction to IDREF

  • Thread starter Nicolas Bouillon
  • Start date
N

Nicolas Bouillon

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

Martin Honnen

Nicolas said:
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"
 
N

Nicolas Bouillon

Martin said:
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.
 

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
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top