Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > [XSD] Retriction to IDREF

Reply
Thread Tools

[XSD] Retriction to IDREF

 
 
Nicolas Bouillon
Guest
Posts: n/a
 
      03-02-2004
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.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-02-2004


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 xmlnss="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 xmlnssi="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/

 
Reply With Quote
 
 
 
 
Nicolas Bouillon
Guest
Posts: n/a
 
      03-02-2004
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.
 
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
is there a way to customize jaxb IDREF? Elhanan Java 0 03-30-2006 07:33 PM
JAXB and IdRef kevkev Java 3 05-19-2005 04:56 PM
[semi-OT] java objects persistence and XML id/idref rico Java 2 10-28-2003 02:12 AM
restrict IDREF to special elements Hauke Fuhrmann XML 1 10-27-2003 02:05 PM
XMLSpy: why is IDREF undefined? Lars XML 0 07-12-2003 09:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57