mnot’s blog

Design depends largely on constraints.” — Charles Eames

Monday, 26 July 2004

Python Web XML

Dictionary as API?

From the Daily Python URL comes another noteworthy API for XML; XMLFragment. I haven’t tried it yet (it doesn’t appear to be separately available, hint, hint), but I like the look of it.

There are two interesting things going on here. First of all, XMLFragment basically gives up on modelling the complexity of XML in the language, instead punting to XPath. I think that’s a reasonable choice; it’s arguably more intuitive and simple than anything you could do with an object model or similar approach (unless you modify the language itself, as E4X did, and even then, the footprint is pretty big). By leveraging an existing standard, XMLFragment minimises the amount that developers have to learn to use it, and maximises the chances that they’ll be able to reuse that knowledge elsewhere. Neat.

This will live or die on performance, of course, but there are some pretty fast XPath engines out there (e.g., libxml2) to plug into the back end.

The other intriguing thing is XMLFragment’s use of dictionaries. Ninety-nine out of a hundred developers would have made this an object or a set of function calls; instead, Kimbro Staken has us treating XML as a data structure. This is a subtle but important difference with echoes of RESTfulness; by making it into an addressable data structure, instead of an opaque object that you pass arguments to, the focus is on the data, rather than the processor. This approach should also make it easy to layer in things like a generic dictionary caching mechanism.

Without using the code (that’s another hint, Kimbro; can we please have it as a separate library?), it’s hard to say how well this approach will work. One other interesting way to use this techique, however, might be for a Web client library, e.g.,

web = UriResolver()
mnot = web[''] # GET
mark = mnot.replace("mnot", "Mark")
web[''] = mark # PUT
del web[''] # DELETE
bob = WebRepresentation("<new_user>bob</new_user>", "text/xml")  
web[''](bob) # POST

There are a lot of details to work out here, of course, but I must say I like the feel of using a dictionary here.


James Antill said:

In python the “dictionary lookup” operation is basically equivilent to a function call … so what’s the difference? What does it provide over just replacing the []’s above with ()? – It just looks like obfuscation to me.

Monday, July 26 2004 at 11:55 AM

l.m.orchard said:

Obfuscation? It looks more like idiomatic Python to me.

Tuesday, July 27 2004 at 10:34 AM

Mark said:

Argh! Not “text/xml”!

Tuesday, July 27 2004 at 11:10 AM

Damian Cugley said:

In this case, ‘text/xml’ is OK, since the text happnens to be US-ASCII. But it would probably cause less squawking should future examples stick to using ‘application/xml’… :-)

Tuesday, August 3 2004 at 5:36 AM

James Tauber said:

The difference between [] and () is significant from a mental model perspective. As a dictionary, the web is treated a big container of resources keyed by URI.

Wednesday, August 11 2004 at 8:02 AM

Terris Linenbach said:

Being a crusty C++ programmer, I use xmlwrapp (just google for it) which is an excellent c++ wrapper around the horrid C libxml API.

The newest, unreleased version of xmlwrapp supports XPATH expressions in much the same way as described here, so you can put an XPATH expression between [ and ].



returns something like a node list.

This is extremely readable and convenient. Shane Beasley deserves the credit. My contribution has been to make sure this syntax supports namespaces too.

On an unrelated blog topic I would like to ask how programmers are supposed to query RDF XML documents. Let’s ignore inference for now (I don’t think the majority of developers will ever use it). Will XPATH statements work? I guess anything that depends on XML Schema in XPATH 2.0 (e.g., datatypes) will fail.

I am torn between RDF and XML Schema. On the one hand I can see that only RDF provides the X in XML. On the other hand I have no idea how to write or process RDF with existing tools.

Wednesday, August 11 2004 at 10:07 AM