Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>I. A good XSLT 1.0 solution</strong> (brief, efficient and understandable):</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="text"/&gt; &lt;xsl:template match="/*"&gt; &lt;xsl:for-each select="group"&gt; &lt;xsl:sort select="sum(val)" data-type="number" order="descending"/&gt; &lt;xsl:if test="position()=1"&gt; &lt;xsl:value-of select="sum(val)"/&gt; &lt;/xsl:if&gt; &lt;/xsl:for-each&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;t&gt; &lt;group&gt; &lt;val&gt;1&lt;/val&gt; &lt;val&gt;3&lt;/val&gt; &lt;val&gt;1&lt;/val&gt; &lt;/group&gt; &lt;group&gt; &lt;val&gt;3&lt;/val&gt; &lt;val&gt;2&lt;/val&gt; &lt;val&gt;2&lt;/val&gt; &lt;/group&gt; &lt;/t&gt; </code></pre> <p><strong>the wanted, correct result is produced</strong>:</p> <pre><code>7 </code></pre> <p><strong>To get the desired variable definition</strong>, simply put the <code>&lt;xsl:for-each&gt;</code> instruvtion from the above code in the body of the variable.</p> <p><strong>II. An even better XSLT 2.0 (and actually XPath 2.0 one-liner) solution</strong>:</p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="text"/&gt; &lt;xsl:template match="/*"&gt; &lt;xsl:sequence select="max(group/sum(val))"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when this transformation is applied on the same XML document, the same correct answer is produced</strong>:</p> <pre><code>7 </code></pre> <p><strong>And the wanted variable definition is simply</strong>:</p> <pre><code> &lt;xsl:variable name="vHighestSum" select="max(group/sum(val))"/&gt; </code></pre> <p><strong>Finally</strong>, the same Xpath expression can be used in XQuery to define the required variable:</p> <pre><code>let $vHighestSum := max(/*/group/sum(val)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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