Note that there are some explanatory texts on larger screens.

plurals
  1. POXPath 1.0 closest preceding and/or ancestor node with an attribute in a XML Tree
    primarykey
    data
    text
    <p>Here are three XML-trees</p> <p>(1)</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;content&gt; &lt;section id="1"&gt; &lt;section id="2"/&gt; &lt;section id="3"/&gt; &lt;section id="9"/&gt; &lt;/section&gt; &lt;section id="4"&gt; &lt;section id="5"&gt; &lt;section&gt; &lt;bookmark/&gt; &lt;!-- here's the bookmark--&gt; &lt;section id="6"&gt; &lt;section id="7"&gt; &lt;section id="8"/&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;/content&gt; </code></pre> <p>(2)</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;content&gt; &lt;section id="1"/&gt; &lt;section id="2"&gt; &lt;section id="9"&gt; &lt;section id="10"&gt; &lt;section id="11"/&gt; &lt;/section&gt; &lt;/section&gt; &lt;section&gt; &lt;section id="4"&gt; &lt;section id="5"/&gt; &lt;/section&gt; &lt;/section&gt; &lt;section/&gt; &lt;bookmark/&gt; &lt;!-- here's the bookmark--&gt; &lt;section id="6"&gt; &lt;section id="7"&gt; &lt;section id="8"/&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;/content&gt; </code></pre> <p>The desired result is in both cases the id <strong>5</strong>.</p> <p>With XSLT 1.0 and XPath 1.0 I can either get the ancestor from (1)</p> <pre><code>&lt;xsl:value-of select="//bookmark/ancestor::*[@id][1]/@id"/&gt; </code></pre> <p>or the preceding node from (2)</p> <pre><code>&lt;xsl:value-of select="//bookmark/preceding::*[@id][1]/@id"/&gt; </code></pre> <p>How do I get the <strong>nearest</strong> ancestor <strong>or</strong> preceding node with an id from my bookmark?<br> I need a single xsl:value-of which matches both cases. Thanks.</p> <p><strong>EDIT:</strong></p> <p>The solution should also cover this structure. Desired id is still <strong>5</strong>.</p> <p>(3)</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;content&gt; &lt;section id="1"&gt; &lt;section id="2"/&gt; &lt;section id="3"/&gt; &lt;section id="9"/&gt; &lt;/section&gt; &lt;section id="4"&gt; &lt;section&gt; &lt;section id="10"/&gt; &lt;section id="5"/&gt; &lt;section&gt; &lt;bookmark/&gt; &lt;!-- here's the bookmark--&gt; &lt;section id="6"&gt; &lt;section id="7"&gt; &lt;section id="8"/&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;/content&gt; </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.
 

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