ElementTree解析xml文档,如何搜寻以什么开头的属性标签?

用ElementTree解析xml文档。varlist标签的name属性有许多不同的值。
目标:列出包含a开头的name属性的所有varlist标签。

import xml.etree.ElementTree as ET
tree = ET.ElementTree(file=’test.xml’)
root = tree.getroot()

for elem in tree.findall(‘varlist[@name=”abc”]’): 这个是可以的,因为有属性值就是abc。
print(elem)

如何列出所有以a开头的属性的varlist标签?尝试了如下的方式,都不行:
for elem in tree.findall(‘varlist[@name=”a”]’):
for elem in tree.findall(‘varlist[startswith(@name=”a”)]’):
for elem in tree.findall(‘varlist[start-with(@name=”a”)]’):
for elem in tree.findall(‘varlist[contains(@name=”a”)]’):

Jason990420
最佳答案

In xml.etree.ElementTree — The ElementTree XML API:

XPath support

This module provides limited support for XPath expressions for locating elements in a tree. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the module.

Supported XPath syntax


My Suggestion

from lxml import etree

xml = """
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <varlist name="ABC" direction="E"/>
        <varlist name="ACB" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <varlist name="ABC" direction="N"/>
        <varlist name="BCA" direction="S"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <varlist name="CAB" direction="N"/>
        <varlist name="ABA" direction="S"/>
    </country>
</data>
""".strip()

root = etree.fromstring(xml)

for elem in root.xpath('.//varlist[starts-with(@name, "A")]'):
    print(elem.attrib)
{'name': 'ABC', 'direction': 'E'}
{'name': 'ACB', 'direction': 'W'}
{'name': 'ABC', 'direction': 'N'}
{'name': 'ABA', 'direction': 'S'}
2年前 评论
讨论数量: 2
Jason990420

In xml.etree.ElementTree — The ElementTree XML API:

XPath support

This module provides limited support for XPath expressions for locating elements in a tree. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the module.

Supported XPath syntax


My Suggestion

from lxml import etree

xml = """
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <varlist name="ABC" direction="E"/>
        <varlist name="ACB" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <varlist name="ABC" direction="N"/>
        <varlist name="BCA" direction="S"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <varlist name="CAB" direction="N"/>
        <varlist name="ABA" direction="S"/>
    </country>
</data>
""".strip()

root = etree.fromstring(xml)

for elem in root.xpath('.//varlist[starts-with(@name, "A")]'):
    print(elem.attrib)
{'name': 'ABC', 'direction': 'E'}
{'name': 'ACB', 'direction': 'W'}
{'name': 'ABC', 'direction': 'N'}
{'name': 'ABA', 'direction': 'S'}
2年前 评论

非常感谢,原来xpath需要fromstring格式化! :+1: :+1: :+1:

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!