A simple example of using XInclude

The XInclude specification allows XML document to contain references to other documents. Nodes are returned in a NodeList collection object and can be iterated using FOR OBJECT.

This example extends the earlier XPath example using a pair of input documents and, as before, we will return the titles of books tagged with a "cat" attribute of "classics" using an XPath expression.

This is primary document containing the XInclude expression. It is a modification of the earlier document. Copy it and save it as books2.xml.

<?xml version="1.0"?>
<library xmlns:xi="http://www.w3.org/2001/XInclude">
	<book isbn="1001" cat="classics">
		<title>A Tale of two Cities</title>
		<author>Charles Dickens</author>
	</book>
	<book isbn="1002" cat="classics">
		<title>Hamlet</title>
		<author>William Shakespeare</author>
	</book>
	<book isbn="1003" cat="computers">
		<title>Inside OLE</title>
		<author>Kraig BrockSchmit</author>
	</book>
	<book isbn="1004" cat="computers">
		<title>The C programming language</title>
		<author>Brian W Kernigan</author>
		<author>Dennis M Richie</author>
	</book>
	<xi:include href="books3.xml"/>
</library>

The <include> tag refers to the secondary document which you should copy and save as books3.xml:

<?xml version="1.0"?>
<book isbn="1010" cat="classics">
	<title>Treasure Island</title>
	<author>Robert Louis Stevenson</author>
</book>

You must explicitly force the expansion of the XInclude directives using the ProcessXInclude() method.

DIM OBJECT x, OBJECT p, OBJECT list, OBJECT i, OBJECT doc, OBJECT xp, n
OBJECT x = CREATE "dynamic", "klibxml"
OBJECT p = x.CreateParser()
p.DoNamespaces = FALSE
p.validationScheme = _VAL_SCHEME_NEVER
p.LoadExternalDTD = FALSE
p.parse("books2.xml")
n = p.ProcessXInclude()
PRINT n;"substitutions"
OBJECT doc = p.Document
OBJECT xp=doc.createXPath()
OBJECT list = xp.evaluate("//book[@cat='classics']/title/text()")
FOR OBJECT i IN list
	PRINT i.NodeValue$
NEXT OBJECT i
OBJECT list = NULL
OBJECT p = NULL
OBJECT x = NULL