[Python] Is there simple xpath to find all parent elements within the xml structure?

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    I'm using python 3 and lxml.etree and I need to get list of all parent elements for selected element with the defined name.

    Here is example of my XML structure

    <root>
    <father id="1">
    <father id="2">
    <child>
    ....


    I do have an element object and need list of all objects of (to get their id attributes for example)

    Using

    element.xpath("parent::father)


    returns list where is only first parent object (father id = 2) but I'd need to have whole path up, regardless how deep I'm.

    Thanks

    Continue reading...

Compartilhe esta Página