Validating XML elements with other XML elements

This should cover W3C XML Schema, Relax NG and DTD related problems.
shand1234
Posts: 1
Joined: Tue Mar 13, 2007 2:41 am
Location: Colorado

Validating XML elements with other XML elements

Post by shand1234 »

To whom it may concern,

I have figured out how to validate an element within an XML instance with another element in the same XML instance file. I used keys and keyref and xpath.

How does one validate an XML element in one XML instance file with another XML element in another XML Instance file?

Thanks in advance
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

Hello,

You can refer in one XML instance to an element from other XML instance only if the two instances are part of the same XML document, that is one of the two XML instances or both XML instances are included in a master XML instance using XInclude. You must use key and keyref too for referring the element from the other XML instance but the two instances must be included in the same document and this document is validated against the XML Schema which defines the key and keyref. For example the following schema:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="childID" type="xs:ID"/>
<xs:element name="childIDREF" type="xs:IDREF"/>
</xs:sequence>
</xs:complexType>
<xs:key name="checkChildReference">
<xs:selector xpath="childID"/>
<xs:field xpath="."/>
</xs:key>
<xs:keyref refer="checkChildReference" name="checkRefKeyref">
<xs:selector xpath="childIDREF"/>
<xs:field xpath="."/>
</xs:keyref>
</xs:element>
</xs:schema>
can be used to validate the master XML file:

Code: Select all


<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="testKeyXInclude.xsd">
<xi:include href="childID.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
<xi:include href="childIDREF.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
</root>
where childID.xml is for example:

Code: Select all


<childID>qwerty</childID>
and childIDREF.xml is:

Code: Select all


<childIDREF>qwerty</childIDREF>

Regards,
Sorin
Post Reply