tags:

views:

971

answers:

2

New to xml. Looking for XPath to search a xml file with python ElementTree format

<root>
<child>One</child>
<child>Two</child>
<child>Three</child>
</root>

to do search for child with "Two" and return true/false

if it was started off like

from elementtree import ElementTree
root = ElementTree.parse(open(PathFile)).getroot()

how can this be achieved

A: 

I've been playing with ElementTree lately, lets see..

>>> from xml.etree import ElementTree
>>> help(ElementTree.ElementPath)
>>> root = ElementTree.fromstring("""
<root><child>One</child><child>Two</child><child>Three</child></root>
""")
>>> ElementTree.ElementPath.findall(root, "child")
[<Element child at 2ac98c0>, <Element child at 2ac9638>, <Element child at 2ac9518>]
>>> elements = ElementTree.ElementPath.findall(root, "child")
>>> two = [x for x in elements if x.text == "Two"]
>>> two[0].text
'Two'

This is what you look for right? It says ElementPath has just limited xpath support though, but it does not say not support at all.

Cheery
in this case, "limited xpath support" ~= "an ultra-simplified query language that isn't really xpath at all, but does have a bit of inspiration"
Charles Duffy
+1  A: 

When the following XPath expression is evaluated:

    boolean(/*/*[.='Two'])

the result is true, if such an element (a child of the top element such that its string value is equal to "Two") exists,

and false otherwise.

Hope this helped.

Cheers,

Dimitre Novatchev

Dimitre Novatchev
Charles, it is universally acknowledged that using the "//" abbreviation when the structure of the document is known, is a bad practice: causing huge inefficience. The mere fact that due to this reason you downvoted a solution that is superior to yours shows that you have big problems in this area.
Dimitre Novatchev
As for using an XML DB and indices in it... where did you see such mentioned in the original question? /*/*/x is evaluated slightly faster than /nam1/name2/x, because the XPath engine doesn't have to verify the names in the first two location steps. Sorry, you're again wrong. Cheers,
Dimitre Novatchev
Dimitre, // is only a problem when there's a depth of more than 1, which the sample never has -- and an indexed XML file will be able to search by name, rather than *need to verify the name*, making it faster -- so the more performant solution depends on the circumstances.
Charles Duffy
What Dimitre gives here is a better answer than mine if one only wishes to verify that such an element exists rather than to find the element itself for further inspection, as it allows the xpath engine to short-circuit.
Charles Duffy
Charles, even for minimal depth // is a bad practice as people may get accustomed to using it always. And the value of indices for a small xml file is minimal. Once again, who said anywhere that the document was maintained in an xml db? This isn't the problem that we are solving.
Dimitre Novatchev
Dimitri, we don't know *where* it's maintained, in an XML DB or otherwise; the information is simply not given. You make one assumption, I make another -- but in cases where performance is truly a critical design decision, an indexed XML DB is likely to be in use.
Charles Duffy