获取具有命名空间和属性的值

Reach out for Values with Namespace and Attributes

以下格式的文件(我尽可能地缩小了数据,因为它是一个非常大的文件)。这是未来 274 小时的原始天气预报:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<kml:kml xmlns:dwd="https://opendata.dwd.de/weather/lib/pointforecast_dwd_extension_V1_0.xsd" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    <kml:Document>
        <kml:ExtendedData>
            <dwd:ProductDefinition>
                ... #Several more Values
                <dwd:ForecastTimeSteps>
                    <dwd:TimeStep>2021-07-22T10:00:00.000Z</dwd:TimeStep>
                    ... #247 Values
                </dwd:ForecastTimeSteps>
            </dwd:ProductDefinition>
     </kml:ExtendedData>
     <kml:Placemark>
         <kml:name>K2932</kml:name>
         <kml:description>TUTTLINGEN</kml:description>
         <kml:ExtendedData>
             <dwd:Forecast dwd:elementName="PPPP">
                 <dwd:value>###247 PPPP-Values according to the Time above###.</dwd:value>
             </dwd:Forecast>
             <dwd:Forecast dwd:elementName="E_PPP">
                <dwd:value>###247 E_PP-Values according to the Time above###</dwd:value>
             </dwd:Forecast>
             ... ### Many more of there Elements
             <dwd:Forecast dwd:elementName="RR1c">
                <dwd:value>###247 RR1c-Values according to the Time above###</dwd:value>
             </dwd:Forecast>
         </kml:ExtendedData>
     </kml:Placemark>
    </kml:Document>
</kml:kml>

我需要获取 RR1C 的值。这是我的方法:

from xml.etree import ElementTree as etree
dwd = '{https://opendata.dwd.de/weather/lib/pointforecast_dwd_extension_V1_0.xsd}'

with open('./tmp/forecast/MOSMIX_L_2021072209_K2932.kml', 'rt') as f:
    root = etree.parse(f)

root.find('.//{0}Forecast'.format(dwd)).attrib['{0}elementName'.format(dwd)]

输出[]: 'PPPP'

我不知道如何深入到“RR1c”以及“值”本身。有人吗?

正如 Michael Kay 在评论中指出的那样,这可以在 XPath 的帮助下完成。这是一个例子:

from xml.etree import ElementTree as etree

# declare namespaces map
ns = {
    'dwd' : 'https://opendata.dwd.de/weather/lib/pointforecast_dwd_extension_V1_0.xsd'
}

with open('./tmp/forecast/MOSMIX_L_2021072209_K2932.kml', 'rt') as f:
    root = etree.parse(f)

# retrieve value using xpath (returns list of elements matching criteria)
forecasts = root.findall('.//dwd:Forecast[@dwd:elementName="RR1c"]/dwd:value', ns)

# print text of the first element found
print(forecasts[0].text)