Search code examples
pythonxmlattributeselementtreefindall

Python 3 XML findall empty output


I'm trying to figure out the following problem:

I want to get the 'type' attribute of the tag (cpe-23 tags are optional and of ANY type in XML schema)

<cpe-23:deprecated-by ... type="NAME_CORRECTION"/>

Reading other questions in stackoverflow has lead me to the following code:

import xml.etree.ElementTree as ET
tree = ET.parse(".../official-cpe-dictionary_v2.3.xml")
root = tree.getroot()

for child in root.findall('cpe-item/cpe-23:cpe23-item/cpe-23:deprecation/cpe-23:deprecated-by',
                      namespaces={'cpe-23': 'http://scap.nist.gov/schema/cpe-extension/2.3'}):

    print(child.attrib['name'], child.attrib['type'])

My problem now is that I just get an empty output and my program stops with code:0

Any ideas on how to solve this?

Many thanks in advance!

Source (XML-file):

<cpe-list ...>
    <generator>
       ...
    </generator>
    <cpe-item name="cpe:/a:3com:tippingpoint_ips_tos:2.2.3" deprecated="true" deprecation_date="2010-12-28T17:36:02.240Z">
        <title xml:lang="en-US">3Com TippingPoint IPS TOS 2.2.3</title>
        <cpe-23:cpe23-item name="cpe:2.3:a:3com:tippingpoint_ips_tos:2.2.3:*:*:*:*:*:*:*">
          <cpe-23:deprecation date="2010-12-28T12:36:02.240-05:00">
            <cpe-23:deprecated-by name="cpe:2.3:o:3com:tippingpoint_ips_tos:2.2.3:*:*:*:*:*:*:*" type="NAME_CORRECTION"/>
          </cpe-23:deprecation>
        </cpe-23:cpe23-item>
    </cpe-item>
    <cpe-item ...>
        ...
    </cpe-item>
    ...
</cpe-list>

Solution

  • I was able to work it out myself reading the doc of the library. The reason why there was not output was that the Element.findall() function only looks at the direkt children. The "cpe-23:cp23-item" was the grandchild of the root node.