[Python] Iterate xml using namespace prefix

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have an xml file with a default namespace, like this:

    <?xml version="1.0" encoding="utf-8"?>
    <root xmlns="somelongnamespace">
    <child>...</child>
    </root>


    I starting using lxml to iterate and query this file, but I would like to use a namespace prefix, like this:

    from lxml import etree
    xml = etree.parse("myfile.xml")
    root = xml.getroot()
    c = root.findall('ns:child')


    What do I need to do for this to work? I cannot change the file, but I could change the xml object after loading.

    I read the relevant lxml documentation, searched and tried all kinds of suggestions, but got none of them to work unfortunately. This does sound like a very common question...?

    Continue reading...

Compartilhe esta Página