Tarek
Tarek

Reputation: 1924

How to validate cross-document references?

i want to check if an attribute is exist in another xml from an xsd i made for another xml.
for an example i have this xsd

<xs:schema version="1.0"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       elementFormDefault="qualified">
<xs:element name="models">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="model" maxOccurs="unbounded" minOccurs="1">
                <xs:complexType>   
                    <xs:attribute name="name" type="xs:string" use="required"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:unique name="uniqueModelName">
        <xs:selector xpath="./model"/>
        <xs:field xpath="@name"/>
    </xs:unique>
</xs:element>  

and i have another xsd

<xs:element name="language">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="word" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="value" maxOccurs="unbounded" minOccurs="1">
                            <xs:complexType>
                                <xs:attribute name="lange" type="xs:string" use="required"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                    <xs:attribute name="key" type="xs:string" use="required"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

and i want to insure that the attribute named name of the elements named model in the first xsd are exist in the attribute named key of the element value in the second xsd
in other words, if there are attributes value named name in the first xsd doesn't exist in the second xsd, an error must occur.
xml example:
xml for first xsd:

<model name="A"/>
<model name="B"/>

xml for second xsd:

<word key="A">
    <value lange="english">Add</value>
    <value lange="frensh">ajouter</value>
</word>

it must tell there is an error because there is no tag word in the second xml that has an attribute B can this happen in xsd :) ?
while this is a correct one

<word key="A">
    <value lange="english">Add</value>
    <value lange="frensh">ajouter</value>
</word>
<word key="B">
    <value lange="english">Add</value>
    <value lange="frensh">ajouter</value>
</word>

Upvotes: 1

Views: 811

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

XSD validation is designed to be context-free, so that any element can be validated in isolation (isolation both from its ancestors and siblings and from other documents). So, no, the kind of cross-document validation you have in mind is not possible with XSD.

You can do what you describe with Schematron (which essentially allows you to write arbitrary XPath expressions to constrain your documents), with the Service Modeling Language (which is designed to enable various kinds of cross-document validation), or with any Turing-complete programming language. Or, of course, you can use devise a process to check your external XML documents and construct appropriate XSD types from them (in particular, listing the legal values for the attribute in question), and use that generated XSD to validate your initial XML document.

Upvotes: 2

Related Questions