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)
Ported/Archived Responses
Joseph Reagle on 2003-11-18
Yep. It’s not like COLOR is a (pythonic/object) attribute of some
namespace instance. I think the pythonic approach is one of consistency,
why remove quotes when dealing with a NS qualified element or attribute?
Oh well, unless I convince Aaron, or change it for my own version, your
preference will persist I presume!
I also really dig XPath
(used it heavily in the C14N implementations), but again have found the
“set-up” in the various toolkits to be a big pain… (What do you use?)
I’ve recently been wondering if I could add a simple XPath query on top
of XML Tramp.
anders on 2003-11-20
libxml2 has a pretty sweet XPath API. see: http://www.xmldatabases.org/WK/blog/607?t=item
anders on 2003-11-16
i think the dot notation is more pythonic, and the point does seem to
be to make XML data accessible through a more native feeling API.
are you really suggesting doc(‘ns:COLOR’) instead of
doc(ns.COLOR)?
personally, i find XPath to be intuitive
enough and don’t really desire anything more pythonic, but if i did, i
think i would agree with Schwarz’s approach.
Comments !