Schema restrictions on objects and instances

V

Victor

I have a Schema with some elements which are "types" or objects, and
some elements which correspond to instances of the types. How can I
specify that a <locationInstance> must have an attribute "type" which
equals any one of the "id" attributes of the <locationType>s?
 
M

Martin Honnen

Victor said:
I have a Schema with some elements which are "types" or objects, and
some elements which correspond to instances of the types. How can I
specify that a <locationInstance> must have an attribute "type" which
equals any one of the "id" attributes of the <locationType>s?

Have a look at ID/IDREF and key/keyref.
 
V

Victor

Martin said:
Have a look at ID/IDREF and key/keyref.

Thanks!

After a lot of looking around for working and well-explained examples, I
managed to get the following (in case someone is looking for something
similar):

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="ImportData">
<xsd:complexType>
<xsd:sequence>
.. definitions of the "locationType" and "location" elements ..
</xsd:sequence>
</xsd:complexType>

<xsd:key name="locationTypeId">
<xsd:annotation>
<xsd:documentation>Within the whole data set, every location
type must have a unique ID attribute</xsd:documentation>
</xsd:annotation>
<xsd:selector xpath=".//locationType"/>
<xsd:field xpath="@id"/>
</xsd:key>

<xsd:key name="locationId">
<xsd:annotation>
<xsd:documentation>Within the whole data set, every location
must have a unique ID attribute</xsd:documentation>
</xsd:annotation>
<xsd:selector xpath=".//location"/>
<xsd:field xpath="@id"/>
</xsd:key>

<xsd:keyref name="locationIdRef" refer="locationTypeId">
<xsd:annotation>
<xsd:documentation>Locations must be of an existing
locationType</xsd:documentation>
</xsd:annotation>
<xsd:selector xpath=".//location"/>
<xsd:field xpath="@type"/>
</xsd:keyref>

.... The rest of the Schema ...

A few notes:

- <key> is basically the same as <unique>, except that the elements
and/or attributes referenced in the <field> XPath _has_ to exist, i.e.
they cannot be empty.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top