ElementTree XPath weird behavior

Hi
I use ElementTree(1.3) with Python 2.7 and enjoy XPath functionality,
however one of the search results surprised me.

My XML example:

<OTF>
  <FECS state="disabled" version="2.2.0.0">
    <BackEndCompatibility major="2.2" state="disabled">
        <BackEnd state="disabled" version="2.2.0.0"/>
    </BackEndCompatibility>
  </FECS>
</OTF>

Question 1:
When do I use findallto get the first item found

version = "2.2.0.0"
found = list(txml.findall(".//BackEnd[@version='%s']" % version))
return found and found[0] or None

finds nothing.

However, when I modify the XML file so that the element BackEndcontains subelements,

        <BackEnd state="disabled" version="2.2.0.0">
           <any_dummy_element/> 
        </BackEnd>

The search item found was found correctly.

Have you encountered this behavior?
Am I mistaken, or is this a mistake in implementation ElementTree?

Question 2:
Another problem that I have: xmlns.
Suppose I modify the first line of XML to contain xmlns:

<OTF xmlns="http://si-wiki/OTFCompatibility">
</OTF>

In this case, I need to change the search bar to:

".//{http://si-wiki/OTFCompatibility}BackEnd[@version='%s']"

ElementTree xmlns ( root), , ?

,

+3
1

№1:

    found = list(txml.findall(".//BackEnd[@version='%s']" % version))
    return found and found[0] or None

    found = txml.findall(".//BackEnd[@version='%s']" % version)
    if found:
        return found[0]
    return None

.

+1

Source: https://habr.com/ru/post/1792574/


All Articles