Suppose we have the following schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="a_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="a_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="b_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="b_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c_elements">
<xs:complexType>
<xs:sequence>
<xs:element name="c_element" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="ref" type="xs:IDREF" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
and here's the sample xml file:
<root>
<a_elements>
<a_element id="id1"/>
<a_element id="id2"/>
</a_elements>
<b_elements>
<b_element id="id3"/>
<b_element id="id4"/>
</b_elements>
<c_elements>
<c_element id="id5" ref="id1"/>
<c_element id="id6" ref="id2"/>
</c_elements>
</root>
So that c_elements can reference a_elements and b_elements by id. Is it possible to somehow restrict ref attribute to only accept references to elements from one group, say a_elements?
I'm not aware of any mechanism to do this using ID and IDREF. By design ID and IDREF refer to all tags in the document.
That said, you could work around this in some way - perhaps with validation rules on whatever processes the data structure. It would be fairly easy to do this using Xpath expressions for example. You could certainly achieve this using a Schematron assertion. There's an example of this here: http://zvon.org/xxl/SchematronTutorial/Examples/Example16/example.html
Hope this helps.
Ken
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With