Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPATH expression that Matches on the attribute value "true"

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,

like image 528
G-Man Avatar asked Mar 01 '12 17:03

G-Man


People also ask

What is attribute and value in XPath?

XPath Tutorial from basic to advance level. This attribute can be easily retrieved and checked by using the @attribute-name of the element. @name − get the value of attribute "name". <td><xsl:value-of select = "@rollno"/></td> Attribute can be used to compared using operators.

Which expression used for anything in XPath is?

XPath uses a path expression to select node or a list of nodes from an XML document.

What is an attribute in XPath?

Definition of XPath attribute. For finding an XPath node in an XML document, use the XPath Attribute expression location path. We can use XPath to generate attribute expressions to locate nodes in an XML document.

What is the meaning of '/' in XPath?

0 votes. Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.


1 Answers

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']] 
like image 55
Dimitre Novatchev Avatar answered Sep 22 '22 18:09

Dimitre Novatchev