XPATH expression that Matches on the attribute value "true"
Asked Answered
H

3

8

I have some 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 on all engine elements that have a child node property with a name equal to "bar" and and value equal to "true". I'm finding the fact that "true" appears in my XML is causing my condition to always evaluate to true in an XPath expression. Is there a way around? I'm using Python and lxml.

EDIT:

My xpath expression is (that isn't working) is: //engine[(property/@name='bar' and property/@value="true")]

Thanks,

Hassiehassin answered 1/3, 2012 at 17:21 Comment(0)
C
20

I want to match on all engine elements

This is:

//engine

that have a child node property

Now this becomes:

//engine[property]

with a name equal to "bar"

Still more specific:

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

and and value equal to "true".

Finally:

//engine[property[@name = 'bar' and @value = 'true']] 
Chronicle answered 1/3, 2012 at 19:0 Comment(1)
I realised my xpath did work all along, I was doing something stupid. Nonetheless, Dimitre had the answer I was after. Thanks!Hassiehassin
P
6

So you're saying

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

gives you too many results? Because for me it gives just one.

Photoactive answered 1/3, 2012 at 17:24 Comment(0)
C
2

What XPath expression did you try?

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

Remember that you need to encase your parameter test values in quotes.

Colet answered 1/3, 2012 at 17:28 Comment(1)
Personally, I have had issues with using double quotes. I have to use single quotes.Stepdaughter

© 2022 - 2024 — McMap. All rights reserved.