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:
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-idis now known.
Insert a
sectionelement in the master file and assign a new ID.Replace the preceding placeholder with the second
includeelement. 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
xpointerattribute selects everything belowsectionbut 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.
Copyright © 2009 Millin Verlag
You may print the online version for your private use.
Otherwise, this chapter from "DocBook-XML: Medienneutrales und plattformunabhängiges Publizieren" is subject to the same provisions as the printed edition: the work, including all its parts, is protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Millin Verlag, Siebengebirgsring 36, 53797 Lohmar, info(at)millin.de
back to top