[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Elementtree 1.3 and xpath

Fredrik Lundh

1/10/2008 9:50:00 PM

Andrew Lonie wrote:

> Hi I noticed that the xpath functionality of elementtree has been
> upgraded in version 1.3. However I can't seem to get the [postion]
> predicate to function. All the other new functionality seems to be
> working.

ET 1.3 is only available in an early alpha yet, and the posted release
only supports the [tag] form; from the CHANGES document:

- Added new path engine, for "find", "findall", and "findtext". The
new engine is a bit faster, and supports a number of predicates
forms: [@attr], [@attr='value'], and [tag] (in the last case, only
single tag names are supported). The engine also provides limited
support for the ".." parent selector; you can use it inside the
subtree, but it cannot go above the context element (the element
you called "find" on).

The next alpha will be out in February, and will do the right thing with
your example:

>>> from elementtree import ElementTree as ET
>>> xml = ET.XML("""<root><tag att="1">text</tag><tag
att="2">text2</tag></root>""")
>>> ET.tostring(xml.find("tag[@att]"))
'<tag att="1">text</tag>'
>>> ET.tostring(xml.find("tag[@att]/.."))
'<root><tag att="1">text</tag><tag att="2">text2</tag></root>'
>>> ET.tostring(xml.find("tag[1]"))
'<tag att="1">text</tag>'

</F>