Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>In SQL, what I would be doing is something like this:</p> <pre><code>SELECT log.*, COUNT(1) numHits FROM Application log GROUP BY Source, Task, Level, Description ORDER BY numHits DESC LIMIT 10 </code></pre> <p>Is it possible to do such a thing using xpath?</p> </blockquote> <p><strong>In case no sorting is necessary, one can get the first <code>$n</code> nodes selected by any XPath expression by</strong>:</p> <pre><code>(ExpressionSelectingNodeSet)[not(position() &gt; $n)] </code></pre> <p>where <code>$n</code> can be substituted by a specific number</p> <p><strong>If there is a requirement that the nodes be sorted on one or more sort-keys, then this is not possible pure XPath, but one can easily perform such tasks with XSLT, using the <a href="http://www.w3.org/TR/1999/REC-xslt-19991116#element-sort" rel="nofollow"><code>&lt;xsl:sort&gt;</code></a> instruction and the XPath <a href="http://www.w3.org/TR/1999/REC-xpath-19991116/#function-position" rel="nofollow"><code>position()</code></a> function</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:template match="/*"&gt; &lt;nums&gt; &lt;xsl:for-each select="num"&gt; &lt;xsl:sort data-type="number" order="descending"/&gt; &lt;xsl:if test="not(position() &gt; 5)"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:if&gt; &lt;/xsl:for-each&gt; &lt;/nums&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;010&lt;/num&gt; &lt;/nums&gt; </code></pre> <p><strong>the correct result, containing only the top 5 numbers is produced</strong>:</p> <pre><code>&lt;nums&gt; &lt;num&gt;010&lt;/num&gt; &lt;num&gt;09&lt;/num&gt; &lt;num&gt;08&lt;/num&gt; &lt;num&gt;07&lt;/num&gt; &lt;num&gt;06&lt;/num&gt; &lt;/nums&gt; </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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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