ElementTree XPath-根据属性选择元素 [英] ElementTree XPath - Select Element based on attribute

查看:254
本文介绍了ElementTree XPath-根据属性选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ElementTree中使用属性XPath选择器时遇到麻烦,根据文档

I am having trouble using the attribute XPath Selector in ElementTree, which I should be able to do according to the Documentation

以下是一些示例代码

XML

<root>
 <target name="1">
    <a></a>
    <b></b>
 </target>
 <target name="2">
    <a></a>
    <b></b>
 </target>
</root>

Python

def parse(document):
    root = et.parse(document)
    for target in root.findall("//target[@name='a']"):
        print target._children

我收到以下异常:

expected path separator ([)


推荐答案

您要使用的语法是 ElementTree 1.3

The syntax you're trying to use is new in ElementTree 1.3.

该版本随附 Python 2.7 或更高。
如果您使用的是Python 2.6或更低版本,那么您仍然拥有ElementTree 1.2.6或更低版本。

Such version is shipped with Python 2.7 or higher. If you have Python 2.6 or less you still have ElementTree 1.2.6 or less.

这篇关于ElementTree XPath-根据属性选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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