【问题标题】:How to access a tag in XML with namespace in Python如何在 Python 中使用命名空间访问 XML 中的标签
【发布时间】:2016-04-10 09:10:17
【问题描述】:

我有一个非常复杂的 XML 文档,我想对其进行解析。 这是该 XML 的简化版本:

<file
    xmlns="http://www.namespace.co.il"
    Media="MetTTV"
    Date="2015-03-29"
    FileType="Consolidated"
    SchemaVersion="1.2">

    <H Id="1012532" W="2198.05">
        ///more tags
    </H>
    <H Id="623478" W="3215.05">
        ///more tags
    </H>
   etc.
</file>

我想访问 标记以便计算它们。

这是我的代码:

import import lxml.etree
tree=lxml.etree.parse(xml_file)
count=1 
for HH in tree.xpath('//H'):
   print count
   count=count+1

如果我删除,这段代码可以正常工作

xmlns="http://www.namespace.co.il"

线。

但如果我不这样做 - 它不会向控制台打印任何内容。

我尝试以多种组合方式更改循环,例如

for HH in tree.xpath('//{http://www.namespace.co.il}H'):

或与

ns={'nmsp':'http://www.namespace.co.il'}
for HH in tree.xpath('//nmsp:H', ns)

但似乎没有任何效果。

【问题讨论】:

标签: python xml


【解决方案1】:

lxml 的xpath 方法需要一个名为namespaces 的命名参数(关键字参数)。

findall 方法类似,但略有不同(它不需要命名的 namespaces 参数,它适用于大括号内的命名空间 URI)。

所有这些变体都有效:

for HH in tree.xpath('//nmsp:H', namespaces=ns):

for HH in tree.findall('//{http://www.namespace.co.il}H'):

for HH in tree.findall('//nmsp:H', namespaces=ns):

for HH in tree.findall('//nmsp:H', ns):

另见http://lxml.de/xpathxslt.html#xpath

【讨论】:

  • 这个答案有帮助吗?如果它解决了您的问题,请将其标记为已接受。如果没有解决问题,请说明原因。
猜你喜欢
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多