XML technologies / DocBook / Block elements / Program listings
(Excerpt from "DocBook-XML: Media-neutral and platform-independent publishing" by Thomas Schraitle)
To document source code, use the element programlisting. Its content is processed exactly as entered, preserving spaces and line breaks. This matters particularly for languages such as Python. Use the element as follows:
<programlisting language="python">#!/usr/bin/python -u
import sys
import string
import commands
cmd = ["file", sys.argv[1], "| cut -d',' -f2" ]
cmd = string.join(cmd)
(res, out) = commands.getstatusoutput(cmd)
print res, out
</programlisting>The attribute language is optional but helps distinguish languages later during XSLT processing.
For text displayed on a user’s screen rather than source code, use screen. It also displays command output:
$ ls -F /
bin/ dev/ home/ local/ mnt/ proc/ sbin/ sys/ usr/
boot/ etc/ lib/ media/ opt/ root/ srv/ tmp/ var/
The preceding examples include content directly. For content in an external file, use one of two methods:
textobjectUse
textobjectinsideprogramlistingand reference the external file in the elementtextdata. File inclusion requires an extension function in your XSLT processor (see Extension functions and elements).- XInclude
Use the element
xi:includefrom the XInclude specification (see Including content with XIncludes). You need an XSLT processor able to resolve XInclude elements. Currently, this works withxsltproc; Saxon requires customization.
Note: Avoid unwanted indentation in XML editors
Some XML editors automatically indent programmlisting and its content, or even destroy line breaks and remove spaces. This damages significant formatting.
Configure your XML editor to leave programmlisting, screen and similar elements untouched during formatting. Start source code in the first column for a consistent appearance.
These methods apply to both programlisting and screen. Both elements have the same attributes.
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