brc.sw
brc.sw

Reputation: 91

lxml XPath match a value in Python

I don't know how to XPath the Element in XML.

I mean, XML:

<General>
    <Task>
        <Startup value="some value" />
        <Shutdown value="some value" />
    </Task>
    <Reset>
        <Startup value="some value2" />
        <Shutdown value="some value2" />
    </Reset>
</General>

I'm trying to reach Startup and Shutdown tags by one line code:

for SActStartup in nXML.xpath('//[$ActionType]/Startup',ActionType=ActionType):
    print SActStartup.get('value')

What I am missing here?

XPathEvalError: Invalid expression

Upvotes: 1

Views: 352

Answers (1)

unutbu
unutbu

Reputation: 880987

$ActionType needs to be replaced by some string -- perhaps the value of the variable ActionType?:

for val in nXML.xpath('//{ActionType}/Startup/@value'.format(ActionType=ActionType)):
    stdin, stdout, stderr = dssh.exec_command(val)

Upvotes: 2

Related Questions