Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>This XSLT 2.0 transformation illustrates how multi-pass (in this case 2-pass) processing can be done</strong>:</p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="node()|@*" mode="#all"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*" mode="#current"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/"&gt; &lt;xsl:variable name="vPass1"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:variable&gt; &lt;xsl:apply-templates select="$vPass1/*" mode="non-empty"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="text()[xs:integer(.) mod 2 eq 0]"/&gt; &lt;xsl:template match="*[not(node())]" mode="non-empty"/&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on this XML document</strong>:</p> <pre><code>&lt;nums&gt; &lt;num&gt;01&lt;/num&gt; &lt;num&gt;02&lt;/num&gt; &lt;num&gt;03&lt;/num&gt; &lt;num&gt;04&lt;/num&gt; &lt;num&gt;05&lt;/num&gt; &lt;num&gt;06&lt;/num&gt; &lt;num&gt;07&lt;/num&gt; &lt;num&gt;08&lt;/num&gt; &lt;num&gt;09&lt;/num&gt; &lt;num&gt;10&lt;/num&gt; &lt;/nums&gt; </code></pre> <p>It creates a result document in the first pass (which is captured in the <code>$vPass1</code> variable), in which all <code>&lt;num&gt;</code> elements with contents even integer are stripped off their content and are empty. Then, in the second pass, applied in a specific mode, all empty elements are removed. </p> <p><strong>The result of the transformation is</strong>:</p> <pre><code>&lt;nums&gt; &lt;num&gt;01&lt;/num&gt; &lt;num&gt;03&lt;/num&gt; &lt;num&gt;05&lt;/num&gt; &lt;num&gt;07&lt;/num&gt; &lt;num&gt;09&lt;/num&gt; &lt;/nums&gt; </code></pre> <p><strong>Do note the use of modes</strong>, and the special modes <code>#all</code> and <code>#current</code>.</p> <p><strong>Update:</strong> The OP now wants in a comment to delete "recursively" "all nodes that have no non-empty descendant".</p> <p>This can be implemented simpler using no explicit recursion. Just change:</p> <pre><code> &lt;xsl:template match="*[not(node())]" mode="non-empty"/&gt; </code></pre> <p>to:</p> <pre><code> &lt;xsl:template match="*[not(descendant::text())]" mode="non-empty"/&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