Select 使用 python 2.7 和 XML 元素树在 XML 中具有特定元素值的节点

Select a node with a particular element value in XML using python 2.7 and XML element tree

我整个早上都在为这个问题苦苦挣扎,但一直无法让它发挥作用。

我有一个这样的XML(简化的匿名版本):

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <First_Level_Node>
    <Element_Name>
      <attribute1>1</attribute1>
      <attribute2>2</attribute2>
      <attribute3>3</attribute3>
      <attribute4>4</attribute4>
      <attribute5>5</attribute5>
      <attribute6>6</attribute6>
    </Element_Name>
    <Element_Name>
      <attribute1>42</attribute1>
      <attribute2></attribute2>
      <attribute3>NO</attribute3>
      <attribute4>42</attribute4>
      <attribute5>random value</attribute5>
      <attribute6>18th Jun 2014  07:09:18 GMT</attribute6>
    </Element_Name>
    <Element_Name>
      <attribute1>42</attribute1>
      <attribute2></attribute2>
      <attribute3>NO</attribute3>
      <attribute4>42</attribute4>
      <attribute5>random</attribute5>
      <attribute6>23rd Jul 2014  02:47:10 GMT</attribute6>
    </Element_Name>
    <Element_Name>
      <attribute1>42</attribute1>
      <attribute2></attribute2>
      <attribute3>NO</attribute3>
      <attribute4>42</attribute4>
      <attribute5>random</attribute5>
      <attribute6>08th Nov 2014  23:53:31 GMT</attribute6>
    </Element_Name>
  </First_Level_Node>
</Root>

现在我已经从所有元素中获取了一些值并使用了它们。

但现在我只想 select 具有特定属性值对的元素。

例如,在我粘贴的 xml 中,我只需要使用 attribute4 = 42

获取元素

我目前的代码如下:

tree=ET.parse('xmlname.xml')
root=tree.getroot()
for slot in input_data:
        for child in root[0]:
            for ch in child.findall('First Level Node/*/[@attribute4="' + str(sys.argv[1]) + '"]'):
                print ch
                if ch.tag == slot:
                    if ch.text == 'UNCOMPUTED' or ch.text == None:
                        slot_text.append("Undefined")
                    else:
                        slot_text.append(ch.text)
        data[slot]=Counter(slot_text).most_common()

但是我在 ch 中没有得到任何值。我已经尝试了相同的多种变体以及我所知道的所有 Xpath,仍然没有结果。

任何帮助将不胜感激。

注意:Element_Name 是动态的,可以更改。

编辑:试过这个但是我得到错误的信息作为输出。

for slot in input_data:
        for child in root[0]:
            for ch in child:
                if ch.text == '42' and ch.tag == "attribute4":
                    flag=1
                if ch.tag == slot and flag == 1:
                    flag=0
                    if ch.text == 'UNCOMPUTED' or ch.text == None:
                        slot_text.append("Undefined")
                    else:
                        slot_text.append(ch.text)
        data[slot]=Counter(slot_text).most_common()

<attribute4> 是一个 XML 元素,而不是 XML 属性。因此,我首先要尝试的是以下 XPath:

.....
xpath = 'First Level Node/*[attribute4="' + str(sys.argv[1]) + '"]'
for ch in child.findall(xpath):
......

*) 旁注:"First Level Node" 不是有效的 XML 元素示例,因为它包含空格

更新:

在您的 XML 示例的上下文中,child 变量已经指向 <First_Level_Node>,它是 <Root> 的子项:

for child in root[0]:

因此,您需要从 XPath 中删除 First_Level_Node :

.....
xpath = '*[attribute4="' + str(sys.argv[1]) + '"]'
for ch in child.findall(xpath):
......

试试这个:

tree=ET.parse('xmlname.xml')
root=tree.getroot()

for first_level_node in root:
    for element_name in first_level_node:
        for attribute in element_name:
            if attribute.tag == "attribute4" and attribute.text == "42":
            # do something