简体   繁体   English

使用 Xpath 获取 XML 的节点,这些节点在属性之一中包含特定单词

[英]get the nodes of XML with Xpath that contain a particular word in one of the attributes

The following example comes from a very good answer to an existing question.以下示例来自对现有问题的一个非常好的答案。 I would like to refine the question further: selecting attribute values from lxml我想进一步细化这个问题: 从 lxml 中选择属性值

having this XML, I would like to have the nodes where a particula node attribute contains a string有了这个 XML,我想要一个节点属性包含字符串的节点

<?xml version ="1.0" encoding="UTF-8"?>
    <level1>
      <level2 first_att='att1.fff.tre' second_att='foo'><name>A</name><age>8</age></level2>
      <level2 first_att='att2.ert.wer' second_att='bar'><name>B</name><age>9</age></level2>
      <level2 first_att='att2.fff.wer' second_att='bar'><name>C</name><age>10</age></level2>
      <level2 first_att='att2.ert.wer' second_att='bar'><name>D</name><age>11</age></level2>
    </level1>

One can access the attribute 'bar' with:可以通过以下方式访问属性“bar”:

import lxml.etree as etree
tree = etree.parse("test_file.xml")
print tree.xpath("//level1/level2[@first_att='att1.fff.tre']/@second_att")[0]

What If I would like to get the nodes where first_att CONTAINS 'fff' anywhere?如果我想在任何地方获取 first_att CONTAINS 'fff' 的节点怎么办? (first and third node in the example. (示例中的第一个和第三个节点。

The ultimate purpose is to get a dictionary to populate a pandas dataframe.最终目的是获取字典来填充 pandas dataframe。

[{'name':'A','age':8},{'name':'B','age':10}] [{'name':'A','age':8},{'name':'B','age':10}]

thanks谢谢

If you want to examine not the whole attribute value, but just a part, try to replace predicate如果您想检查的不是整个属性值,而只是一部分,请尝试替换谓词

[@first_att='att1.fff.tre']

with

[contains(@first_att, 'fff')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM