将字符串参数传递给Xpath

时间:2015-12-11 02:54:26

标签: python xml xpath contains

我真的很茫然。我正在尝试使用xpath和contains从xml文件中搜索数据标记“MF22”的文本字段。当我直接在contains函数中包含搜索字符串时,它工作正常。但是当我尝试将其作为参数传递时,它会返回文件中的所有内容。

from lxml import etree as ElementTree
ET = ElementTree.parse('USFLMEO_USSHARE_60200.txt')

bcnIDstr = "AB"
test1 = ET.xpath("//MF22[text()[contains(.,bcnIDstr)]]")
print 'found ' + str(len(test1)) + ' packets'

test2 = ET.xpath("//MF22[text()[contains(.,'AB')]]")
print 'found ' + str(len(test2)) + ' packets'

for elem in test1:
    packet = elem.getparent()
    for elem2 in packet:
        print elem2.tag, elem2.text 

所以在上面的代码中,'test2'找到所有正确的元素,但'test1'却找不到。下面的XML数据。

<?xml version="1.0" ?>
<topMessage>
    <header dest="366Z" orig="USFLMEO" number="60200" date="2015-10-02T00:00:59.000000000Z" />
    <message>
        <packetsMessage>
            <packet>
                <MF6>324</MF6>
                <MF11>3669</MF11>
                <MF71>2</MF71>
                <MF22>9C634E2AB509240</MF22>
                <MF77>FFFE2FCE31A7155A849207E5B34027500004</MF77>
                <MF67>15 275 0000 40.147870</MF67>
                <MF68>406033830.154</MF68>
                <MF69>0.000000</MF69>
                <MF70>99999.999</MF70>
                <MF72>45.1169</MF72>
                <MF73>399.987</MF73>
                <MF74>0000</MF74>
            </packet>
            <packet>
                <MF6>318</MF6>
                <MF11>3669</MF11>
                <MF71>1</MF71>
                <MF22>9C634E2AB509240</MF22>
                <MF77>FFFE2FCE31A7155A849207E5B34027500004</MF77>
                <MF67>15 275 0000 40.147850</MF67>
                <MF68>406033830.673</MF68>
                <MF69>0.000000</MF69>
                <MF70>99999.999</MF70>
                <MF72>40.0184</MF72>
                <MF73>400.066</MF73>
                <MF74>0000</MF74>
            </packet>
            <packet>
                <MF6>324</MF6>
                <MF11>3669</MF11>
                <MF71>2</MF71>
                <MF22>9C02BE29630F0A0</MF22>
                <MF77>FFFE2FCE015F14B18785039DABCE5A4EC14F</MF77>
                <MF67>15 275 0000 42.922460</MF67>
                <MF68>406033518.783</MF68>
                <MF69>0.000000</MF69>
                <MF70>99999.999</MF70>
                <MF72>41.5108</MF72>
                <MF73>400.053</MF73>
                <MF74>0000</MF74>
            </packet>
            <packet>
                <MF6>315</MF6>
                <MF11>3669</MF11>
                <MF71>3</MF71>
                <MF22>9C02BE29630F0A0</MF22>
                <MF77>FFFE2FCE015F14B18785039DABCE5A4EC14F</MF77>
                <MF67>15 275 0000 42.924905</MF67>
                <MF68>406038122.646</MF68>
                <MF69>0.000000</MF69>
                <MF70>99999.999</MF70>
                <MF72>41.0458</MF72>
                <MF73>399.815</MF73>
                <MF74>0000</MF74>
            </packet>
        </packetsMessage>
    </message>
</topMessage>

提前致谢!

2 个答案:

答案 0 :(得分:2)

简单地说,您没有连接XPath字符串中的bcnIDstr变量

test1 = ET.xpath("//MF22[text()[contains(.,'"+bcnIDstr+"')]]")

甚至可以缩短您的XPath:

test1 = ET.xpath("//MF22[contains(.,'"+bcnIDstr+"')]")

或者,您可以使用字符串格式:

test1 = ET.xpath("//MF22[text()[contains(.,'{0}')]]".format(bcnIDstr))
test1 = ET.xpath("//MF22[contains(.,'{0}')]".format(bcnIDstr))

答案 1 :(得分:0)

从XPath引用python变量有一个lxml特定功能:

bcnIDstr = "AB"
test1 = ET.xpath("//MF22[text()[contains(.,$foo)]]", foo=bcnIDstr)

文档:http://lxml.de/xpathxslt.html#the-xpath-method

当你想引用包含字符串以外的python变量即XML元素时,这可能很有用,在这种情况下你不能对python变量使用字符串操作。这是此类案例场景的一个示例:No nested nodes. How to get one piece of information and then to get additional info respectively?