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:

textobject

Use textobject inside programlisting and reference the external file in the element textdata. File inclusion requires an extension function in your XSLT processor (see Extension functions and elements).

XInclude

Use the element xi:include from the XInclude specification (see Including content with XIncludes). You need an XSLT processor able to resolve XInclude elements. Currently, this works with xsltproc; 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.

  

  

  

back to top