Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Performing a chain of transformations is used quite often in XSLT applications</strong>, though doing this entirely in XSLT 1.0 requires the use of the vendor-specific <code>xxx:node-set()</code> function. In XSLT 2.0 no such extension is needed as the infamous RTF datatype is eliminated there.</p> <p><strong>Here is an example</strong> (too-simple to be meaningful, but illustrating completely how this is done):</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:variable name="vrtfPass1"&gt; &lt;xsl:apply-templates select="/*/*"/&gt; &lt;/xsl:variable&gt; &lt;xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/&gt; &lt;xsl:apply-templates mode="pass2" select="$vPass1/*"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="num[. mod 2 = 1]"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="num" mode="pass2"&gt; &lt;xsl:copy&gt; &lt;xsl:value-of select=". *2"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when this transformation is applied on the following 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><strong>the wanted, correct result is produced</strong>:</p> <pre><code>&lt;num&gt;2&lt;/num&gt; &lt;num&gt;6&lt;/num&gt; &lt;num&gt;10&lt;/num&gt; &lt;num&gt;14&lt;/num&gt; &lt;num&gt;18&lt;/num&gt; </code></pre> <p><strong>Explanation</strong>:</p> <ol> <li><p><strong>In the first step the XML document is transformed</strong> and the result is defined as the value of the variable <code>$vrtfPass1</code>. This copies only the <code>num</code> elements that have odd value (not even).</p></li> <li><p><strong>The <code>$vrtfPass1</code> variable, being of type RTF, is not directly usable for XPath expressions</strong> so we convert it to a normal tree, using the EXSLT (implemented by most XSLT 1.0 processors) function <code>ext:node-set</code> and defining another variable -- <code>$vPass1</code> whose value is this tree.</p></li> <li><p><strong>We now perform the second transformation in our chain of transformations</strong> -- on the result of the first transformation, that is kept as the value of the variable <code>$vPass1</code>. Not to mess with the first-pass template, we specify that the new processing should be in a named mode, called "pass2". In this mode the value of any <code>num</code> element is multiplied by two.</p></li> </ol> <p><strong>XSLT 2.0 solution</strong> (no RTFs):</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:template match="/"&gt; &lt;xsl:variable name="vPass1" &gt; &lt;xsl:apply-templates select="/*/*"/&gt; &lt;/xsl:variable&gt; &lt;xsl:apply-templates mode="pass2" select="$vPass1/*"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="num[. mod 2 = 1]"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="num" mode="pass2"&gt; &lt;xsl:copy&gt; &lt;xsl:value-of select=". *2"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&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