XInclude Examples

Beginning with version 0.9, eXist comes with partial support for the XInclude standard. If configured, the server will scan a document stored in the database for XInclude tags and expand those tags if possible. The expansion is applied during the serialization of the document, i.e. whenever you retrieve a document containing XInclude tags.

eXist's support for XInclude is not complete. You cannot include raw text, just XML. The fallback element is ignored. Also just basic XPath is allowed for XPointers.

Why do we need XInclude? One reason is that eXist does not preserve entity declarations. Entities are resolved by the XML parser, so eXist just sees the expanded text, not the entity itself. However, external entities are handy if you want to include a given fragment at several locations in different files. For example, the navigation sidebar at the left will probably be used by several documents throughout a site, so it would be wise to keep it in an external file.

XInclude provides a powerful alternative. The following sections present some examples.

1. Viewing the Examples

If you came here from the examples page, you are already looking at a page that has been passed through XInclude, and you can see the live effect of all the examples below. If not, try to browse to

rest/db/xinclude/xinclude.xml

and continue to read there.

By the way, this page also demonstrates some of the possibilities of eXist's REST interface. All the files required to render this page reside in the database and are read from there, including the two XSLT stylesheets used for the formatting.

2. Including a Document

To include an entire document, just specify its path in the href attribute of an <xi:include> tag. For example, the sidebar shown at the left of this documents has been included as follows:

<xi:include href="sidebar.xml"/>

Please note that, as usual, you have to provide the correct namespace for XInclude, e.g. in the root element of the document. The official namespace is:

http://www.w3.org/2001/XInclude

3. Shorthand Pointer

The fragment part of the URI reference in the href attribute is interpreted as an XPointer. If the fragment is just a barename - now called shorthand pointer - it will select the first element of the target document that has an attribute of type ID matching the name. For example, the following xinclude selects the p element from file disclaimer.xml, which has an ID attribute with value "statement".

<xi:include href="disclaimer.xml#statement"/>

The result of the XInclude will be displayed below:

<xi:include href ="disclaimer.xml#statement" />

4. Selecting Fragments by an XPath Expression

We may also use an XPath expression to select fragments. The XPath expression is passed to the xpointer() scheme. The results of the expression will be included in place of the <xi:include> element. The following expression includes a section taken from Shakespeare's Macbeth:

<xi:include href="/db/shakespeare/plays/macbeth.xml
#xpointer(//SPEECH[SPEAKER&='witch' and near(LINE, 'fenny snake')])/>

As before, the results are included below:

<xi:include href ="/db/shakespeare/plays/macbeth.xml#xpointer(//SPEECH[SPEAKER&='witch' and near(LINE, 'fenny snake')])" />

An XPath expression will be applied to the entire collection if the path in href points to a collection and not a single document:

<xi:include href="/db/shakespeare#xpointer(//SPEECH[near(LINE, 'cursed spite')])"/>
<xi:include href ="/db/shakespeare#xpointer(//SPEECH[near(LINE, 'cursed spite')])" />

Test namespaces: all namespace/prefix mappings declared in the source document are passed to the query context. Alternatively, you may declare mappings with xmlns().

<xi:include href ="disclaimer.xml#xpointer(//comment:comment)xmlns(t=http://nop.com)xmlns(comment=http://test.org)" />