Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict ID references to a particular element group

Tags:

xml

xsd

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?

like image 773
Max Avatar asked Nov 15 '11 19:11

Max


1 Answers

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

like image 83
kennethmay Avatar answered Nov 15 '22 20:11

kennethmay