sissaschool / elementpath

XPath 1.0/2.0/3.0/3.1 parsers and selectors for ElementTree and lxml
MIT License
72 stars 20 forks source link

select() always calls root ElementTree instead of child one #47

Closed kkuriata closed 2 years ago

kkuriata commented 2 years ago

I use elementpath with defusedxml.ElementTree and it basically works fine until I want queries to be relative.

For example:

<elements>
  <element id="1">
    <list>
      <item>
        <p name="someParam1">1</p>
        <p name="someParam2">2</p>
      </item>
    </list>
  </element>
  <element id="2">
    <list>
      <item>
        <p name="someParam1">3</p>
        <p name="someParam2">4</p>
      </item>
      <item>
        <p name="someParam1">5</p>
        <p name="someParam2">6</p>
      </item>
    </list>
  </element>
</elements>
from elementpath import select

root = defusedxml.ElementTree.parse("./my_file.xml")
chosen_elements = select(root, "//elements/element[number(@id) < 3]")  # getting a list of ElementTree instances

for el in chosen_elements:
  # I want it to search inside el (not inside root)
  params = select(el, "//list/item/p[@name = 'someParam1']/text()")

I expect this code to return ["1"] for element@id = 1 and ["3", "5"] for element@id = 2. Unfortunately, it returns ["1", "3", "5"] for each element. It looks like select(el, ...) behaves the same as select(root, ...). Of course we can change our query to precise which element we want, but the problem is when we have e.g. 50k of elements, then this query takes forever to finish. That's why I wanted to search through each element separately.

kkuriata commented 2 years ago

Ok, I have just found out that it doesn't work only with version 0.7.0 - on 0.7.1 everything is fine.

brunato commented 2 years ago

Ok, I have just found out that it doesn't work only with version 0.7.0 - on 0.7.1 everything is fine.

Hi, to which package versions 0.7.0 - 0.7.1 refer?