XML technologies / XML / XML in a Nutshell / XInclude / The include element
(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)
The key component of XInclude is the element include. It must belong to the namespace "http://www.w3.org/2001/XInclude". The prefixes xi or xinclude are arbitrary. As always, the prefix may change as long as the URI stays the same. This element has an attribute href containing the URL of the document to include. For example, this element includes the document at the relative URL AlanTuring.xml:
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="AlanTuring.xml"/>Absolute URLs work just as well:
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="http://cafeconleche.org/books/xian3/examples/12/AlanTuring.xml"/>Note: Technically, the attribute href contains an IRI rather than a URI or URL. An IRI resembles a URI but may contain non-ASCII characters such as é and Δ. Usually these are encoded in UTF-8, then each byte in the UTF-8 sequence is percent-escaped to turn the IRI into a URI before resolution. Unless you work with non-English text or write an XInclude processor, this can safely be ignored. Every standard URL is a legal IRI. For non-English, non-ASCII IRIs, just use them as expected without manually encoding the characters in hexadecimal.
Normally the namespace is declared on the including document’s root rather than repeated on every element xi:include. Throughout the rest of this XInclude section, assume the prefix xi is bound to the correct namespace URI.
The next example contains two elements xi:include. The first loads the document at the relative URL AlanTuring.xml, the second the document at RichardPFeynman.xml.
<?xml version="1.0"?>
<leute xmlns:xi="http://www.w3.org/2001/XInclude" >
<xi:include href="AlanTuring.xml"/>
<xi:include href="RichardPFeynman.xml"/>
</leute>Code example: A document loading two others with XInclude
An XInclude processor parses the XML documents at both URLs and inserts their contents, excluding XML and document type declarations, at the positions of the elements xi:include. Those elements xi:include are removed. XInclude is not processed by default, and many parsers do not understand or support it. Either use a filter to resolve the elements xi:include before further processing, or tell the parser to handle XInclude. Details vary by parser. For example, the parser xmllint from libxml is told by the option --xinclude to resolve XIncludes:
$ xmllint --xinclude http://cafeconleche.org/books/xian3/examples/12/12-1.xml
<?xml version="1.0"?>
<leute xmlns:xi="http://www.w3.org/2001/XInclude">
<person geboren="1912" gestorben="1954" xml:base="http://cafeconleche.org/books/xian3/examples/12/AlanTuring.xml">
<name>
<vorname>Alan</vorname>
<nachname>Turing</nachname>
</name>
<beruf>Informatiker</beruf>
<beruf>Mathematiker</beruf>
<beruf>Kryptograph</beruf>
</person>
<person geboren="1918" gestorben="1988" xml:base="http://cafeconleche.org/books/xian3/examples/12/RichardPFeynman.xml">
<name>
<vorname>Richard</vorname>
<mittelinitial>P</mittelinitial>
<nachname>Feynman</nachname>
</name>
<beruf>Physiker</beruf>
<hobby>Bongos spielen</hobby>
</person>
</leute>Notice the processor added attributes xml:base to preserve the included elements’ base URIs. This matters little here because all three documents share a directory. When assembling documents from different servers and directories, however, it helps relative URLs in their text resolve correctly.
Inclusion works on the parsed document, rather than copying raw text. XML declarations are not copied. Insignificant whitespace may change. Prolog whitespace is not copied at all. Document type declarations are not copied, but the default attribute values they define are.
libxml has fairly complete XInclude support. Xerces-J 2.7 has partial support, while other parsers have none and need third-party libraries such as XOM’s package nu.xom.xinclude. This is still a young technology.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML in a Nutshell” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top