如何迭代具有与当前元素相同名称的XML子元素并在迭代中避免当前元素?

huangapple go评论59阅读模式
英文:

How to iterate over XML children with same name as current element and avoid current element in iteration?

问题

我有

一个给定的XML(不能更改命名),其中一个节点和其直接子节点具有相同的名称,这里是 items

我想

只迭代子节点,具有 description 字段的 items

我的问题

类型为 items 的父节点出现在迭代中,即使我理解正确,iter 在自身上调用

from xml.etree import ElementTree
content = """<?xml version="1.0" encoding="utf-8"?>
<root>
    <items>
        <items>
            <description>foo1</description>
        </items>
        <items>
            <description>foo2</description>
        </items>
    </items>
</root>
"""
tree = ElementTree.fromstring(content)
print(">>", tree.find("items&quot,))
for item in tree.find("items").iter("items"):
    print(item, item.find("description"))

当前输出

>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8720> None
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>

预期输出

>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>
英文:

I have

A given XML (can't change naming) that have same name for a node and its direct children, here items

I want

To iterate on the children only, the items that have a description field

My issue

The parent node of type items appears in the iteration, even the iter is called on itself if I understand well

from xml.etree import ElementTree
content = """<?xml version="1.0" encoding="utf-8"?>
<root>
    <items>
        <items>
            <description>foo1</description>
        </items>
        <items>
            <description>foo2</description>
        </items>
    </items>
</root>
"""
tree = ElementTree.fromstring(content)
print(">>", tree.find("items"))
for item in tree.find("items").iter("items"):
    print(item, item.find("description"))

Current output

>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8720> None
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>

Expected output

>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>

答案1

得分: 1

tree.findall('items/items')
英文:

Use XPath with findall().

tree.findall('items/items')

huangapple
  • 本文由 发表于 2023年2月19日 17:10:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定