Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>ancestor</code> is for selecting nodes that are higher (closer to the root) in the XML document. <code>descendant</code> is for selecting nodes that are lower (children) in the XML document.</p> <p>In your example, <code>ancestor::criterion</code> selects nothing because the current node is <code>/</code> (meaning the root of the document - <code>&lt;file&gt;</code> in this case), as indicated by <code>match="/"</code>. The root node has no ancestors so the <code>ancestor</code> axis does nothing.</p> <p>To get every <code>&lt;criterion&gt;</code> element, you should use the <code>descendant</code> axis:</p> <pre><code>&lt;xsl:template match="/"&gt; &lt;xsl:copy-of select="descendant::criterion"/&gt; &lt;/xsl:template&gt; </code></pre> <p>Or its shortcut <code>//</code>:</p> <pre><code>&lt;xsl:template match="/"&gt; &lt;xsl:copy-of select="//criterion"/&gt; &lt;/xsl:template&gt; </code></pre> <p>That will return the following: </p> <pre><code>&lt;criterion&gt;AAA&lt;/criterion&gt; </code></pre> <p>Using a loop or another template you can get all three of them:</p> <pre><code>&lt;xsl:template match="/"&gt; &lt;file&gt; &lt;xsl:apply-templates select="//criterion"/&gt; &lt;/file&gt; &lt;/xsl:template&gt; &lt;xsl:template match="criterion"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; </code></pre> <p>This will produce the following:</p> <pre><code>&lt;file&gt; &lt;criterion&gt;AAA&lt;/criterion&gt; &lt;criterion&gt;BBB&lt;/criterion&gt; &lt;criterion&gt;CCC&lt;/criterion&gt; &lt;/file&gt; </code></pre> <p>If you want to get the <code>&lt;file&gt;</code> element, too, it's a bit more complicated. XPath specifies nodes and simple copies will not copy the elements that contain the elements you select. I can clarify this point more if you're still confused.</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