python - XPath string as input -
considering code example below,
if 'xpath' in request.args: in = request.args['xpath'] xml = bytesio(open("/file.xml").read()) v, e in etree.iterparse(xml): return e.text
the following xml file,
<a> <c> <d>content x</d> </c> <c> <d>content y</d> </c> </a>
and in
variable receives string /a/c[2]/d
curl, bellow
curl http://localhost:5000/home?xpath=/a/c[2]/d
is there way given string (xpath expression) code returns element text (i.e. able understand element i'm referring to)?
to return element located in
, can call find
method on parsed elementtree
.
return etree.parse(xml).find(in).text
note xpath expression must not include root element itself, nor starting slash, c[2]/d
should trick.
Comments
Post a Comment