Next / Previous / Contents / TCC Help System / NM Tech homepage

16. Test driver for a document fragment: crefrag

This script generates a small Web page without the enclosing html element. It demonstrates use of the DocumentFragment object, in particular the ability of such an object to have multiple children (unlike a Document), and to produce a file with no initial <?xml ...> processing instruction.

crefrag
#!/usr/bin/env python
#================================================================
# crefrag: Fragment output testing for pyxml4.py.
# For documentation, see:
#    http://www.nmt.edu/tcc/help/pubs/pyxml4/
#----------------------------------------------------------------

import xml4create as xc

frag = xc.DocumentFragment()

head = xc.Element ( frag, 'head' )
title = xc.Element ( head, 'title' )
xc.Text ( title, 'No title' )

body = xc.Element ( frag, 'body' )
h1 = xc.Element ( body, 'h1' )
xc.Text ( h1, 'No body either' )

frag.write()

Here's the output of this script:

<head>
  <title>No title</title>
</head>
<body>
  <h1>No body either</h1>
</body>