|
Using LL2XML
This module converts a list of lists into xml.
With the proper arguments, the XML output will be an HTML table.
(See the test function for examples.)
If you want to use a csv as input, you will first need to get
hold of a csv parser to create the list of lists.
Examples include those at:
http://tratt.net/laurie/python/asv/
and
http://www.object-craft.com.au/projects/csv/
The function LL2XML in this module accepts up to five arguments:
1) a list of lists, each sublist having the same number of elements
(required)
2) a tuple of heading values (optional - see below for default behaviour),
same length as the sublists
3) a root element name (optional - default is "rows")
4) a row element name (optional - default is "row")
5) a "yes" or "no" string controlling if the
XML declaration is included (optional - default is "yes").
If no header tuple is supplied the first sublist is taken
to be a list of
headers,and its values used to create the element names for the
XML rendition
of the other sublists.
If you need to supply the XML element names, add them as the first
sublist or
use the optional headings tuple argument.
Element names (This paragraph updated 13 July 2002)
Headings are normalised by the script to string type, lower case
and have any
spaces replaced by underscores. The current version of LL2XML does
not do any
element name validation. You should check that your proposed headings
are XML compliant.
See this
article for more information on XML element names.
I plan to add some degree of element name validation in version
0.6 of the script.
Using the root_element and row_element arguments
Example: If your input is a list of lists containing employee details,
you may
want to use "employees" as the root element name, and
"employee" as the
row level element name. (See example 2 in the module to see one
way to
do this.)
Note that "<",">" and "</"
should not be used to wrap these names.
Just define them in plain text. If these two arguments are not defined
in the function call, the defaults - "rows" and "row"
are used.
See the examples in the test function below for more detail.
Headers and content are converted to string representations if
they are not
already strings. Certain characters that have meaning in XML are
replaced by
standard XML entities in the output:
< becomes &
' becomes '
< becomes <
> becomes >
" becomes "
Using LL2XML
To use LL2XML in another module:
import LL2XML
LL = *whatever your list of lists is*
xml = LL2XML.LL2XML(LL)
print xml
To test:
import LL2XML
LL2XML.test()
See the test function in the script
for more on how to use it.
What's new in version 0.5.1: A
tidy up. The backtick `s` string conversion now uses str(s), a redundant
function call is removed, and literal empty strings are used to
check against empty headings. The example list of lists with no
headings is now derived from the one with headings (D'oh!).
What was new in LL2XML 0.5: Now
headings are properly escaped, and the digit 0 is allowed as a heading.
What was new in version 0.4:
- I fixed a bug which made the heading list length exception not
work properly.
- I've used list comprehensions to check that all of the sublists
are of equal length. This means that the module requires Python
2.0 or above.
- The string module is not imported, as string methods are used
throughout.
Version 0.3.1 is still available for
those who need to use earlier versions of Python. It's a bug fixed
version of 0.3, with all of the fixes present in 0.4, but list comprehensions
and string methods are not used. Please let me know if there is
anything I've missed in making this Python 1.5.2 compliant.
Please send comments and suggestions to LL2XML
at outwardlynormal dot com
Many thanks to Dave Cole for the optimisation suggestions.
I hope people find this useful. Please do let
me know if you use it and if you have any comments or suggestions.
Note for people using
the O'Reilly version of this module
An edited version of this module appears in the Python Cookbook.
Unfortunately the hard copy version has three errors, introduced
during the editing/publishing process.
Lines 30 and 31 in the O'Reilly hard
copy version are:
s = s.replace("<", "<")
s = s.replace(">", ">")
they should be
s = s.replace("<", "<")
s = s.replace(">", ">")
The other entity replacements seem to have gone through unscathed.
Line 37
s = 's'
This should be
s = `s`
or, better,
s = str(s)
As of 23 August 2002, these have been corrected in the code
download on the O'Reilly site, so their version should now work
fine.
|