python - XPath search element by child value -
i've got xml next structure:
<?xml version="1.0" encoding="iso-8859-2"?> <partners> <partner> <id>001</id> ... </partner> <partner> <id>002</id> ... </partner> ... </partners>
i want search partner given id, using xpath. i'm using python xml.etree, may irrelevant in case. far i'm trying: './partner[/id = "001"]'
find partner has id=001
:
for e in doc.findall('./partner[/id = "001"]'): print e
and throws syntax error of syntaxerror: expected path separator ([)
, total stack trace below:
traceback (most recent phone call last): file "<stdin>", line 1, in <module> file "/usr/lib/python2.6/xml/etree/elementtree.py", line 647, in findall homecoming self._root.findall(path) file "/usr/lib/python2.6/xml/etree/elementtree.py", line 355, in findall homecoming elementpath.findall(self, path) file "/usr/lib/python2.6/xml/etree/elementpath.py", line 198, in findall homecoming _compile(path).findall(element) file "/usr/lib/python2.6/xml/etree/elementpath.py", line 176, in _compile p = path(path) file "/usr/lib/python2.6/xml/etree/elementpath.py", line 93, in __init__ "expected path separator (%s)" % (op or tag) syntaxerror: expected path separator ([)
can point me doing wrong?
edit: i'm afraid it's due python 2.6.8 version used. docs different in case: https://docs.python.org/release/2.6.8/library/xml.etree.elementtree.html. , no, can't update python version, unfortunately...
xpath back upwards of xml.etree.elementtree limited. how using lxml? using xpath
method
>>> x = '''<?xml version="1.0" encoding="iso-8859-2"?> ... <partners> ... <partner> ... <id>001</id> ... </partner> ... <partner> ... <id>002</id> ... </partner> ... </partners>''' >>> >>> import lxml.etree >>> root = lxml.etree.fromstring(x) >>> root.xpath('.//partner[id/text()="001"]') [<element partner @ 0x2e29090>]
python xml xpath
No comments:
Post a Comment