With the coming of you and good afternoon everyone! I have a question, how to get a parent with descendants on request to descendants using xpath request? I have this code:

from lxml import etree tree = etree.parse('out.xml') nodes = tree.xpath('/osm/node') for node in nodes: print(etree.tostring(node,pretty_print=True)) for tag in node.xpath('/osm/node/tag[@k = "name"]'): print(etree.tostring(tag,pretty_print=True)) 

The request seems to work, but the tag[@k = "name"] fragment does not work and I cannot understand why and how to fix it. Xml file structure:

 <?xml version='1.0' encoding='UTF-8'?> <osm version="0.6" generator="osmconvert 0.8.5"> <node id="429459476" lat="55.6091243" lon="37.7270414" version="2" timestamp="2012-02-20T18:13:50Z" changeset="10743203" uid="210173" user="osmmaker"> <tag k="amenity" v="library"/> <tag k="name" v="Детская библиотека №101"/> <tag k="opening_hours" v="Mo-Fr 12:00-18:00; Sa 12:00-17:00"/> <tag k="phone" v="+7-495-3995297"/> </node> </osm> 

I would be very grateful to you.

  • What are you trying to get? You have a closing tag, there is no text and nested tags. The output should be an empty string. Elements of the tag can be obtained as follows: tag.v.text() IIRC. - 0andriy

0