Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just bringing the condition to the expression:</p> <pre><code>//div[@class='pluto'][contains(//div/ul/li[1], "apples")]/text() </code></pre> <p>Will return <code>Sucess</code> only if that contition is met. Otherwise the result will be empty.</p> <p><br> On a side note, what you really seem to search could be simplified:</p> <pre><code>//div[@class='pluto'][contains(ul/li[1], "apples")]/text() </code></pre> <p>Once again, it will only bring results if the <code>contains</code> condition is met.</p> <h2><br></h2> <p>In <strong>XPath 2.0</strong>, on the other hand, if you need a <strong>value depending on your condition</strong> and not the node value itself, you can use a <strong><code>if</code> expression</strong>:</p> <pre><code>if (//div[@class='pluto'][contains(ul/li[1], "apples")]/text()) then 'OK!' else 'NOT OK!' </code></pre> <p>Which will bring the <code>then</code> part (<code>'OK!'</code>) if it finds a match and the <code>else</code> part <code>('NOT OK!')</code> if finds nothing.</p> <h2>Update:</h2> <blockquote> <p>What if I would like to execute another xPath query if the condition is NOT met (kind of like an else-if)?</p> </blockquote> <p>You can put any XPath expression in the <code>then</code> or <code>else</code> part:</p> <pre><code>if (//div[@class='pluto'][contains(ul/li[1], "apples")]/text()) then concat('OK! :',//div/ul/li[1]) else concat('NOT OK! :',//div/ul/li[2]) </code></pre> <h3>If-then-else Using XPath 1.0:</h3> <pre><code>concat( substring(concat('OK! :', //div/ul/li[1]) , 1, number( boolean(count( //div[@class='pluto'][contains(ul/li[1], "apples")]/text() ))) * string-length( concat('OK! :', //div/ul/li[1]) )), substring(concat('NOT OK! :',//div/ul/li[2]) , 1, number(not(boolean(count( //div[@class='pluto'][contains(ul/li[1], "apples")]/text() )))) * string-length( concat('NOT OK! :',//div/ul/li[2]) )) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload