XPath-like filtering for ElementTree

Jean-Paul Calderone exarkun at divmod.com
Tue Dec 27 04:45:29 CET 2005


On 26 Dec 2005 15:42:56 -0800, Gerard Flanagan <grflanagan at yahoo.co.uk> wrote:
>Pseudo-XPath support for ElementTree with the emphasis on 'Pseudo'.
>
>    http://gflanagan.net/site/python/pagliacci/ElementFilter.html
>
> [snip]
>
>    ns = "xmlns1"
>    path = r"{%s}To/{%s}mailto[@name=='Mrs Jones' and @test==3]" %
>(ns,ns)

How about promoting the query to a set of Python objects?  eg,

    path = Query(
        ns.xmlns1.To / ns.xmlns1.mailto['name': 'Mrs Jones',
                                        'test': '3'])

That's just off the top of my head as an example of the kind of thing 
I mean.  There is probably a better (more consistent, flexible, easier 
to read, etc) spelling possible.

The advantages of this over strings is that you can break the query up 
into multiple pieces, pass parts of it around as real, live objects with 
introspectable APIs, allow for mutation of portions of the query, 
re-arrange it, etc.  All this is possible with strings too, just way 
harder :)

Jean-Paul



More information about the Python-list mailing list