I've been playing with Aaron Swarz's XML Tramp as an intuitive/pythonic way of processing XML. The model/syntax isn't explicitly documented, but from the source and (mostly) examples, this is what I've figured out:
XML Tramp Conventions
-
Elements have a list of children '[]'
-
iterate over children:
for child in doc -
get first and second (splice) child:
doc[0:2] -
get named child element book:
doc.bookordoc['book']
-
iterate over children:
-
Elements have a dict of attributes '()'
-
test for an attribute COLOR:
if 'COLOR' in doc.book() -
get attribute COLOR value:
doc.book('COLOR') -
assign attribute value:
doc.book(COLOR='blue')
-
test for an attribute COLOR:
-
Namespaces (NS) are indicated with a period, the period indicates its
not a literal value (as with quotes), but a namespace corresponding to an
arbitrary but specified prefix
-
NS qualified elments now appear unquoted within brackets:
doc[ns.book] -
NS qualified attributes now appear unquoted within parenthesis:
doc(ns.COLOR)
-
NS qualified elments now appear unquoted within brackets:
-
To reserialize an object use
print doc.__repr__(True)