XML technologies / DocBook / Modular documents / Partial inclusion with XPointer / Including a file multiple times

(Excerpt from "DocBook-XML: Media-neutral and platform-independent publishing" by Thomas Schraitle)

A file can be included repeatedly, but the resulting document must be valid. The attributes id or xml:id in included files can cause problems: repeated inclusion duplicates their values and invalidates the document.

One solution is to omit all IDs, but that prevents references to the file’s content. A less drastic option excludes the root element carrying the ID and includes only its children. Consider this file:

<section ANKER="irgendeine-id">
    <title>...</title>
    <para>Irgendein Text ... </para>
</section>

Replace the placeholder ANKER with id in DocBook 4 and xml:id in DocBook 5.

To include the file repeatedly while excluding its ID-bearing root, proceed as follows:

  1. Create a reference in the master file:

<xi:include href="Datei.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>

This includes the entire file. The ID value irgendeine-id is now known.

  1. Insert a section element in the master file and assign a new ID.

  2. Replace the preceding placeholder with the second include element. This time use an XPointer expression selecting all children:

DocBook 4:

<xi:include href="Datei.xml" xmlns:xi="http://www.w3.org/2001/XInclude" xpointer="xpointer(/section/*)"/>

DocBook 5:

<xi:include href="Datei.xml" xmlns:xi="http://www.w3.org/2001/XInclude" xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(/db:section/*)"/>

The expression in the xpointer attribute selects everything below section but not the root itself.

The following example shows the complete file.

Example: Repeated inclusion with an XPointer expression

DocBook 4:

<book>
 <chapter>
  <!-- ... -->
  <xi:include href="datei.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
 </chapter>
 <appendix>
  <!-- ... -->
  <section id="irgendeine-id">
     <xi:include href="datei.xml" xpointer="xpointer(/section/*)" xmlns:xi="http://www.w3.org/2001/XInclude"/>
  </section>
 </appendix>
</book>

DocBook 5:

<book version="5.0" xmlns="http://docbook.org/ns/docbook">
 <chapter>
  <!-- ... -->
  <xi:include href="datei.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
 </chapter>
 <appendix>
  <!-- ... -->
  <section xml:id="irgendeine-id">
    <xi:include href="datei.xml" xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(/db:section/*)" xmlns:xi="http://www.w3.org/2001/XInclude"/>
  </section>
 </appendix>
</book>

This produces a second section element with a different ID, making the whole document valid.

This method fails if the included file has additional IDs: duplicates still invalidate the document. Consider restructuring, splitting the file into smaller units, or omitting IDs and references.

  

back to top