XPATH expression that matches the value of the true attribute

I have an XML like this:

<engine-set>
  <engine host-ref="blah1.com">
  <property name="foo" value="true"/>
  <property name="bar" value="true"/>
 </engine>
 <engine host-ref="blah2.com">
  <property name="foo" value="true"/>
  <property name="bar" value="false"/>
 </engine>
</engine-set>

I want to match all engine elements that have a child node property with a name equal to "bar" and a value equal to "true". I find the fact that "true" appears in my XML, so my condition is always evaluated as true in the XPath expression. Is there any way? I am using Python and lxml.

EDIT:

My xpath expression (this does not work): // engine [(property / @ name = "bar" and property / @ value = "true")]

Thank,

+5
source share
3 answers

I want to match all the elements of the engine

It:

//engine

which have a child property of node

Now it will be:

//engine[property]

with a name equal to "bar"

More specifically:

//engine[property[@name = 'bar']] 

"true".

:

//engine[property[@name = 'bar' and @value = 'true']] 
+17

,

//engine[property[@name='bar' and @value='true']]

? .

+5

What XPath expression have you tried?

The following seems to work well on getting "blah1.com" but not "blah2.com": //engine[property[@value="true"][@name="bar"]]

Remember that you need to enclose the parameter values ​​in quotation marks in the test values.

+2
source

All Articles