Andrius
Andrius

Reputation: 21098

lxml - using find method to find specific tag? (does not find)

I have an xml file that I need to update some values from some specific tags. In header tag there are some tags with namespaces. Using find for such tags, works, but if I try to search for some other tags that do not have name spaces, it does not find it.

I tried relative, absolute path, but it does not find. The code is like this:

from lxml import etree

tree = etree.parse('test.xml')
root = tree.getroot()

# get its namespace map, excluding default namespace
nsmap = {k:v for k,v in root.nsmap.iteritems() if k}

# Replace values in tags

identity = tree.find('.//env:identity', nsmap)
identity.text = 'Placeholder' # works fine

e01_0017 = tree.find('.//e01_0017') # does not find
e01_0017.text = 'Placeholder' # and then it throws this ofcourse: AttributeError: 'NoneType' object has no attribute 'text'

# Also tried like this, but still not working
e01_0017 = tree.find('Envelope/Body/IVOIC/UNB/cmp04/e01_0017')

I even tried finding for example body tag, but it does not find it too.

This is how xml structure looks like:

    <?xml version="1.0" encoding="ISO-8859-1"?><Envelope xmlns="http://www.someurl.com/TTT"  xmlns:env="http://www.someurl.com/TTT_Envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.someurl.com/TTT TTT_INVOIC.xsd"><Header>
    <env:delivery>
        <env:to>
            <env:address>Test</env:address>
        </env:to>
        <env:from>
            <env:address>Test2</env:address>
        </env:from>
        <env:reliability>
            <env:sendReceiptTo/>
            <env:receiptRequiredBy/>
        </env:reliability>
    </env:delivery>
    <env:properties>
        <env:identity>some code</env:identity>
        <env:sentAt>2006-03-17T00:38:04+01:00</env:sentAt>
        <env:expiresAt/>
        <env:topic>http://www.someurl.com/TTT/</env:topic>
    </env:properties>
    <env:manifest>
        <env:reference uri="#INVOIC@D00A">
            <env:description>Doc Name Descr</env:description>
        </env:reference>
    </env:manifest>
    <env:process>
        <env:type></env:type>
        <env:instance/>
        <env:handle></env:handle>
    </env:process>
</Header>
    <Body>     
    <INVOIC>
                <UNB>
                    <cmp01>
                        <e01_0001>1</e01_0001>
                        <e02_0002>1</e02_0002>
                    </cmp01>
                    <cmp02>
                        <e01_0004>from</e01_0004>
                    </cmp02>
                    <cmp03>
                        <e01_0010>to</e01_0010>
                    </cmp03>
                    <cmp04>
                        <e01_0017>060334</e01_0017>
                        <e02_0019>1652</e02_0019>
                    </cmp04>
                    <e01_0020>1</e01_0020>
                    <cmp05>
                        <e01_0022>1</e01_0022>
                    </cmp05>
                </UNB>
    </INVOIC>
    </Body>
    </Envelope>

Update It seems something is wrong with header or envelope tags. If I for example use xml without that header and envelope info, then tags are found just fine. If I include envelope attributes and header, it stops finding tags. Updated xml sample with header info

Upvotes: 1

Views: 1154

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90859

The thing is that your elements like e01_0017 also has a namespace, it inherits its namespace from the namespace of its parent, in this case it goes all the way back to - <Envelope> . The namespace for your elements are - "http://www.someurl.com/TTT" .

You have two options ,

  1. Either directly specify the namespace in the XPATH , Example -

    e01_0017 = tree.find('.//{http://www.someurl.com/TTT}e01_0017')
    

    Demo (for your xml) -

    In [39]: e01_0017 = tree.find('.//{http://www.someurl.com/TTT}e01_0017')
    
    In [40]: e01_0017
    Out[40]: <Element {http://www.someurl.com/TTT}e01_0017 at 0x2fe78c8>
    
  2. Another option is to add it to the nsmap with some default value for the key and then use it in the xpath. Example -

    nsmap = {(k or 'def'):v for k,v in root.nsmap.items()}
    e01_0017 = tree.find('.//def:e01_0017',nsmap)
    

Upvotes: 4

Related Questions