Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>And I have the following XSL:</p> <pre><code>&lt;xsl:template match="/"&gt; &lt;xsl:copy-of select="ancestor::criterion/&gt; &lt;/xsl:template&gt; </code></pre> <p>which produces nothing!</p> </blockquote> <p><strong>As it should!</strong></p> <pre><code>ancestor::criterion </code></pre> <p>is a relative expression, which means that it is evaluated off the current node (matched by the template). But the current node is the document node <code>/</code>.</p> <p>So, the above is equivalent to:</p> <pre><code>/ancestor::criterion </code></pre> <p>However, by definition the document node <code>/</code> has no parents (and that means no ancestors), so this XPath expression doesn't select any node.</p> <blockquote> <p>I expected it to produce:</p> <pre><code>&lt;file&gt; &lt;criteria&gt; &lt;/criteria&gt; &lt;/file&gt; </code></pre> </blockquote> <p>What you probably wanted was:</p> <pre><code>//criterion/ancestor::* </code></pre> <p>or</p> <pre><code>//*[descendant::criterion] </code></pre> <p>The last two XPath expressions are equivalent and select all elements that have a <code>criterion</code> descendant.</p> <p><strong>Finally, to produce the output you wanted, here is one possible solution:</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:strip-space elements="*"/&gt; &lt;xsl:template match="node()|@*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="root | criterion | format"/&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the provided XML document, the wanted output is produced</strong>:</p> <pre><code>&lt;file&gt; &lt;criteria&gt; &lt;/criteria&gt; &lt;/file&gt; </code></pre>
 

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