Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use not contains() in XPath?

Tags:

contains

xpath

I have some XML that is structured like this:

<whatson>
  <productions>    
    <production>
      <category>Film</category>
    </production>
    <production>
      <category>Business</category>
    </production>
    <production>
      <category>Business training</category>
    </production>
  </productions>
</whatson>

And I need to select every production with a category that doesn't contain "Business" (so just the first production in this example).

Is this possible with XPath? I tried working along these lines but got nowhere:

//production[not(contains(category,'business'))]
like image 992
Jason Avatar asked Jun 13 '12 22:06

Jason


People also ask

Can we use contains in XPath?

contains() in Selenium is a function within Xpath expression which is used to search for the web elements that contain a particular text. We can extract all the elements that match the given text value using the XPath contains() function throughout the webpage.

How use contains XPath?

The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]

What is text () in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.

Why we use XPath by Contains?

The contains() function aids the user in locating elements with partial values or dynamically changing values by verifying matches with a piece of the value. contains() is a Selenium function that searches for web elements that contain a specific text within an Xpath expression.


4 Answers

XPath queries are case sensitive. Having looked at your example (which, by the way, is awesome, nobody seems to provide examples anymore!), I can get the result you want just by changing "business", to "Business"

//production[not(contains(category,'Business'))]

I have tested this by opening the XML file in Chrome, and using the Developer tools to execute that XPath queries, and it gave me just the Film category back.

like image 117
Arran Avatar answered Oct 06 '22 19:10

Arran


I need to select every production with a category that doesn't contain "Business"

Although I upvoted @Arran's answer as correct, I would also add this... Strictly interpreted, the OP's specification would be implemented as

//production[category[not(contains(., 'Business'))]]

rather than

//production[not(contains(category, 'Business'))]

The latter selects every production whose first category child doesn't contain "Business". The two XPath expressions will behave differently when a production has no category children, or more than one.

It doesn't make any difference in practice as long as every <production> has exactly one <category> child, as in your short example XML. Whether you can always count on that being true or not, depends on various factors, such as whether you have a schema that enforces that constraint. Personally, I would go for the more robust option, since it doesn't "cost" much... assuming your requirement as stated in the question is really correct (as opposed to e.g. 'select every production that doesn't have a category that contains "Business"').

like image 28
LarsH Avatar answered Oct 06 '22 19:10

LarsH


You can use not(expression) function.

not() is a function in xpath (as opposed to an operator)

Example:

//a[not(contains(@id, 'xx'))]

OR

expression != true()
like image 8
Shubham Jain Avatar answered Oct 06 '22 18:10

Shubham Jain


Should be xpath with not contains() method, //production[not(contains(category,'business'))]

like image 2
bace bogo Avatar answered Oct 06 '22 19:10

bace bogo