Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:template match="/"&gt; &lt;Target Sum="{sum(FOO/BAR)}" /&gt; &lt;/xsl:template&gt; </code></pre> <p></p> <p><strong>EDIT:</strong></p> <p>This is a bit verbose, and there is probably a more elegant way to do it, but essentially I have a named template here that recursively calls itself to calculate the sum after the hard-coded mapping occurs:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:template match="/"&gt; &lt;Target&gt; &lt;xsl:attribute name="Sum"&gt; &lt;xsl:call-template name="GetSum"&gt; &lt;xsl:with-param name="CurrentNode" select="FOO/BAR[position()=1]"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:attribute&gt; &lt;/Target&gt; &lt;/xsl:template&gt; &lt;xsl:template name="GetSum"&gt; &lt;xsl:param name="CurrentNode"/&gt; &lt;xsl:param name="Number" select="0"/&gt; &lt;xsl:variable name="Recursive_Result"&gt; &lt;xsl:variable name="MappedNumber"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$CurrentNode/. = 'x'"&gt;1&lt;/xsl:when&gt; &lt;xsl:when test="$CurrentNode/. = 'y'"&gt;2&lt;/xsl:when&gt; &lt;xsl:when test="$CurrentNode/. = 'z'"&gt;3&lt;/xsl:when&gt; &lt;xsl:otherwise&gt;0&lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:variable&gt; &lt;!-- if there is a next sibling, recursively call GetSum --&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$CurrentNode/following-sibling::BAR[1]"&gt; &lt;xsl:call-template name="GetSum"&gt; &lt;xsl:with-param name="CurrentNode" select="$CurrentNode/following-sibling::BAR[1]"/&gt; &lt;xsl:with-param name="Number"&gt; &lt;xsl:value-of select="$MappedNumber"/&gt; &lt;/xsl:with-param&gt; &lt;/xsl:call-template&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:value-of select="$MappedNumber"/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:variable&gt; &lt;!-- add the recursive_result to the number passed into the template. this will eventually build a sum --&gt; &lt;xsl:value-of select="$Recursive_Result + $Number"/&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