lxml - 使用find方法查找特定标签? (找不到)

时间:2015-10-21 06:17:13

标签: python xml lxml

我有一个xml文件,我需要更新某些特定标签中的某些值。在header标记中,有一些带有名称空间的标记。使用find来获取这样的标签,但是如果我尝试搜索一些没有名称空间的其他标签,它就找不到它。

我尝试了相对的,绝对的路径,但它找不到。代码是这样的:

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')

我甚至试图找到例如body标签,但它也找不到它。

这就是xml结构的样子:

    <?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>

更新标题或信封标记似乎有问题。例如,如果我使用没有该标题和信封信息的xml,那么标签就可以了。如果我包含信封属性和标题,它将停止查找标记。使用标题信息更新了xml示例

1 个答案:

答案 0 :(得分:1)

问题是像e01_0017这样的元素也有一个命名空间,它从其父命名空间继承它的命名空间,在这种情况下它会一直回到 - <Envelope>。元素的命名空间是 - "http://www.someurl.com/TTT"

您有两种选择,

  1. 直接在XPATH中指定命名空间,例如 -

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

    演示(适用于您的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. 另一种选择是将其添加到nsmap,并为该键添加一些默认值,然后在xpath中使用它。示例 -

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