Colin's Journal: A place for thoughts about politics, software, and daily life.
There has been a lot of talk recently about whether TAL could be integrated with XPATH, sparked it seems from Leslie Orchard‘s ideas on the subject. Replacing or augmenting TALES with XPATH wouldn’t provide as rich a transformation capability as XSLT, but it would be far simpler to understand and use. Providing XPATH support in SimpleTAL would require integrating with something like libxml2. I’m not sure whether there’s a way to customise the node instances that libxml2 returns, a prerequisite for integrating with SimpleTAL easily.
The ElementTree library provides a simplified way of accessing XML that, while not a full XPATH implementation, can still be useful. It also provides a simple mechanism by which customised Element instances can be produced, so integrating it with SimpleTAL is straight forward.
I’ve put together a basic integration, and released it as an experimental build for people to test and play with. If the integration doesn’t break anything, and is found to be useful, I’ll document and ship this as part of SimpleTAL.
The integration allows XML documents to be placed in the SimpleTAL Context. Using the new simpleElementTree
module’s parseFile
method creates a new object that is a subclass of both simpleTALES.ContextVariable
and ElementTree._ElementInterface
.
The syntax available when using this object from TAL consists of four options:
The last form is just short-hand for the first and can be used for anything where the SEARCHPATH does not start with either ‘find’ or ‘findall’ as an element name. The use of the /@ATTNAME can be combined with the first and last forms, i.e. book/find/author/@birthday is a valid path to use. The SEARCHPATH can be any valid ElementTree search path. To find an element ‘author’ that is directly under the ‘book’ element use a path of ‘book/author’. To find the element ‘author’ anywhere in the ‘book’ hierarchy use ‘book/.//author’.
The biggest piece of missing functionality with this integration is the ability to copy whole element structures into the target document. For example if the XML document you are working with contains <p>I am over <b>here</b>, not over there.</p>
there is no way to extract all of this as a unit and include it into the target template. If ElementTree provided this facility it would probably be very simple to add to the implementation.
If this integration interests you, please take the time to download the experimental build, and test it out. There’s a small example included that should be a good starting point for exploring this functionality.
The picture is a reminder of the pleasant Autumn we had this year not, unfortunately, a reflection of the current weather.
The full list of my published Software
Email: colin at owlfish.com