Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>In XPath 1.0 one way to do this is by using the Kayessian method for node-set intersection</strong>:</p> <pre><code>$ns1[count(.|$ns2) = count($ns2)] </code></pre> <p>The above expression selects exactly the nodes that are part both of the node-set <code>$ns1</code> and the node-set <code>$ns2</code>.</p> <p><strong>To apply this to the specific question</strong> -- let's say we need to select all nodes between the 2nd and 3rd <code>h3</code> element in the following XML document:</p> <pre><code>&lt;html&gt; &lt;h3&gt;Title T31&lt;/h3&gt; &lt;a31/&gt; &lt;b31/&gt; &lt;h3&gt;Title T32&lt;/h3&gt; &lt;a32/&gt; &lt;b32/&gt; &lt;h3&gt;Title T33&lt;/h3&gt; &lt;a33/&gt; &lt;b33/&gt; &lt;h3&gt;Title T34&lt;/h3&gt; &lt;a34/&gt; &lt;b34/&gt; &lt;h3&gt;Title T35&lt;/h3&gt; &lt;/html&gt; </code></pre> <p><strong>We have to substitute <code>$ns1</code> with</strong>:</p> <pre><code>/*/h3[2]/following-sibling::node() </code></pre> <p><strong>and to substitute <code>$ns2</code> with</strong>:</p> <pre><code>/*/h3[3]/preceding-sibling::node() </code></pre> <p><strong>Thus, the complete XPath expression is</strong>:</p> <pre><code>/*/h3[2]/following-sibling::node() [count(.|/*/h3[3]/preceding-sibling::node()) = count(/*/h3[3]/preceding-sibling::node()) ] </code></pre> <p><strong>We can verify that this is the correct XPath expression:</strong></p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:copy-of select= "/*/h3[2]/following-sibling::node() [count(.|/*/h3[3]/preceding-sibling::node()) = count(/*/h3[3]/preceding-sibling::node()) ] "/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the XML document presented above, the wanted, correct result is produced</strong>:</p> <pre><code>&lt;a32/&gt; &lt;b32/&gt; </code></pre> <hr> <p><strong>II. XPath 2.0 solution</strong>:</p> <p><strong>Use the <code>intersect</code> operator</strong>:</p> <pre><code> /*/h3[2]/following-sibling::node() intersect /*/h3[3]/preceding-sibling::node() </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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