我有一个要在python中解析的kml文件,我想要的是从SimpleData传递val3 作为参数,并且只检索该地标标签的坐标,我之前在xpath上进行过工作: xpath表达式中的一个典型示例是:
value = '..'
for val in (//Placement/ExtendedData/SimpleData[contains(text(), "+value+")]):
print val.find_element_by_xpath(//coordinates)
但是我似乎无法通过在Python中使用元素树来获得相同的效果:
这是kml文件
:
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="" id="">
<SimpleField name="NAME_0" type="string"></SimpleField>
<SimpleField name="NAME_1" type="string"></SimpleField>
</Schema>
<Folder><name></name>
<Placemark>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#gadm36_IND_3">
<SimpleData name="NAME_0">val1</SimpleData>
<SimpleData name="NAME_1">val2</SimpleData>
<SimpleData name="NAME_2">val3</SimpleData>
</SchemaData></ExtendedData>
<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>92.7877807617189,9.24416637420654 92.7888870239258,9.24305438995361 92.7897186279296,9.24306106567383 92.7902832031251,9.24250030517589 92.7905578613282,9.24250030517589 92.7911148071289,9.24194431304943 92.7913894653321,9.24194431304943 92.7922210693359,9.24110984802257 92.7922210693359,9.24083423614508 92.7930526733399,9.23999977111822
</coordinates></LinearRing>...
<Placemark>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#gadm36_IND_3">
<SimpleData name="NAME_0">val1</SimpleData>
<SimpleData name="NAME_1">val2</SimpleData>
<SimpleData name="NAME_2">val3</SimpleData>
</SchemaData></ExtendedData>
<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>92.7877807617189,9.24416637420654 92.7888870239258,9.24305438995361 92.7897186279296,9.24306106567383 92.7902832031251,9.24250030517589 92.7905578613282,9.24250030517589 92.7911148071289,9.24194431304943 92.7913894653321,9.24194431304943 92.7922210693359,9.24110984802257 92.7922210693359,9.24083423614508 92.7930526733399,9.23999977111822
</coordinates></LinearRing>...
这就是我坚持的:
import xml.etree.ElementTree as ET
tree = ET.parse('')
root = tree.getroot()
for val in root.findall('.//{http://www.opengis.net/kml/2.2}SimpleData[@text=""]//coordinates'):
print val.text
答案 0 :(得分:2)
使用lxml with XPath和namespaces。在XPath选择器中,您可以从带有文本SimpleData
的{{1}}导航回到val3
的祖先,然后从那里导航到Placemark
。
coordinates
输出:
from lxml import etree
tree = etree.parse("so.xml")
nsmap = {"kml": "http://www.opengis.net/kml/2.2"}
listOfCoordinates = tree.xpath("//kml:SimpleData[text()=\"val3\"]/ancestor::kml:Placemark//kml:coordinates", namespaces=nsmap)
print(listOfCoordinates[0].text)