如何在python中使用xpath查询带有命名空间的xml数据 [英] how to query xml data with namespaces using xpath in python

查看:35
本文介绍了如何在python中使用xpath查询带有命名空间的xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将 XPath 查询应用于具有命名空间的 XML 数据:

I am trying to apply an XPath query to XML data which has namespaces using the following code:

from lxml import etree
from io import StringIO

xml = '''
<gpx creator="udos" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
 <metadata>
  <time>2015-07-07T15:16:40Z</time>
 </metadata>
 <trk>
  <name>some name</name>
  <trkseg>
   <trkpt lat="46.3884140" lon="10.0286290">
    <ele>2261.8</ele>
    <time>2015-07-07T15:30:42Z</time>
   </trkpt>
   <trkpt lat="46.3884050" lon="10.0286240">
    <ele>2261.6</ele>
    <time>2015-07-07T15:30:43Z</time>
   </trkpt>
   <trkpt lat="46.3884000" lon="10.0286210">
    <ele>2262.0</ele>
    <time>2015-07-07T15:30:46Z</time>
   </trkpt>
   <trkpt lat="46.3884000" lon="10.0286210">
    <ele>2261.8</ele>
    <time>2015-07-07T15:30:47Z</time>
   </trkpt>
  </trkseg>
 </trk>
</gpx>
'''

# this is to simulate that above xml was read from a file
file = StringIO(unicode(xml))   # with python 3 use "file = StringIO(xml)"

# reading the xml from a file
tree = etree.parse(file)

ns = {'xmlns': 'http://www.topografix.com/GPX/1/1',
      'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
      'xmlns:gpxtpx': 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1',
      'xmlns:gpxx': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3'}

expr = 'trk/trkseg/trkpt/ele'

for element in tree.xpath(expr, namespaces=ns):
    print(element.text)

我希望代码有以下输出:

I expect the following output from the code:

2261.8
2261.6
2262.0
2261.8

2261.8
2261.6
2262.0
2261.8

当您替换 XML 根元素时

when you substitute the XML root element

<gpx creator="udos" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">

<gpx>

代码正在运行...

关于如何让它也与命名空间一起工作的任何建议?

any suggestions how to get it to work with namespaces as well?

推荐答案

您可以将命名空间定义为 -

You can define your namespaces as -

ns = {'n': 'http://www.topografix.com/GPX/1/1',
      'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
      'gpxtpx': 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1',
      'gpxx': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3'}

这会将 'http://www.topografix.com/GPX/1/1' 的前缀定义为 n ,然后在您的 XPath 查询中,您可以使用该前缀.示例 -

This would define the prefix for 'http://www.topografix.com/GPX/1/1' as n , and then in your XPath query, you can use that prefix. Example -

expr = 'n:trk/n:trkseg/n:trkpt/n:ele'

for element in tree.xpath(expr, namespaces=ns):
        print(element.text)

这是因为根节点的 xmlns 是 - 'http://www.topografix.com/GPX/1/1' - 因此所有子节点都会自动继承它作为 xmlns(namespace) ,除非子节点使用不同的前缀或指定自己的命名空间.

This is because the xmlns for the root node is - 'http://www.topografix.com/GPX/1/1' - hence all the child nodes automatically inherit that as the xmlns (namespace) , unless the child node uses a different prefix or specifies an namespace of its own.

示例/演示 -

In [44]: ns = {'n': 'http://www.topografix.com/GPX/1/1',
   ....:       'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
   ....:       'gpxtpx': 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1',
   ....:       'gpxx': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3'}

In [45]:

In [45]: expr = 'n:trk/n:trkseg/n:trkpt/n:ele'

In [46]: for element in tree.xpath(expr, namespaces=ns):
   ....:         print(element.text)
   ....:
2261.8
2261.6
2262.0
2261.8

这篇关于如何在python中使用xpath查询带有命名空间的xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆