Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>The code below selects the table but starting from most outer table. I need to select most inner table which contains that given string</p> </blockquote> <p><strong>Use</strong>:</p> <pre><code>//table [not(descendant::table) and .//text()[contains(., ' KDV')] ] </code></pre> <p>This selects any <code>table</code> in the XML document that doesn't have a <code>table</code> descendant, and that has a text node descendant that contains the string <code>" KDV"</code> .</p> <p>In general the above expression could select many such <code>table</code> elements.</p> <p><strong>If you want only one of them selected (say the first), use this XPath expression -- do notice the brackets</strong>:</p> <pre><code> (//table [not(descendant::table) and .//text()[contains(., ' KDV')] ] )[1] </code></pre> <p><strong>Remember</strong>: If you want to select the first <code>someName</code> element in the document, using this (as in the currently accepted answer) is wrong:</p> <pre><code>//someName[1] </code></pre> <p>This is the second most FAQ in XPath (after the one how to select elements with unprefixed names in an XML document with a default namespace).</p> <p>The expression above actually selects any <code>someName</code> element in the document, that is the first child of its parent -- try it.</p> <p>The reason for this unintuitive behavior is because the XPath <code>[]</code> operator has a higher precedence (priority) that the <code>//</code> pseudo-operator.</p> <p>The correct expression that really selects only the first <code>someName</code> element (in any XML document), if such exists is:</p> <pre><code>(//someName)[1] </code></pre> <p>Here the brackets are used to explicitly override the default XPath operator precedence.</p>
 

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