Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Here is a solution using Muenchian grouping</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:key name="kBookByNums" match="Book" use="number(current_inventory)"/&gt; &lt;xsl:template match="/*"&gt; &lt;xsl:for-each select= "Book [generate-id() = generate-id(key('kBookByNums', number(current_inventory) )[1] ) ] "&gt; &lt;xsl:sort select="current_inventory" data-type="number" order="descending"/&gt; &lt;xsl:if test="position()=1"&gt; &lt;xsl:copy-of select= "key('kBookByNums', number(current_inventory) )"/&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 provided XML document, the correct result is produced</strong>:</p> <pre><code>&lt;Book&gt; &lt;sku&gt;12345&lt;/sku&gt; &lt;title&gt;Beauty Secrets&lt;/title&gt; &lt;condition&gt;New&lt;/condition&gt; &lt;current_inventory&gt;20&lt;/current_inventory&gt; &lt;price&gt;99.99&lt;/price&gt; &lt;/Book&gt; &lt;Book&gt; &lt;sku&gt;54321&lt;/sku&gt; &lt;title&gt;Picturescapes&lt;/title&gt; &lt;current_inventory&gt;20&lt;/current_inventory&gt; &lt;condition&gt;New&lt;/condition&gt; &lt;price&gt;50.00&lt;/price&gt; &lt;/Book&gt; </code></pre> <p><strong>Update</strong>:</p> <p>It may seem that this xsl-key's solution "plays its 'efficiency card' for large input documents only (i.e. thousands of books). " as someone put it. In fact, such a statement is quite imprecise.</p> <p><strong>The improvement in eficiency can be large even when there are not so many items</strong>, <em>if the number of different values is very small, compared to the total number of values</em>. So, even for fairly small sets of items (one-two hundred), there is significant effect if the number of different values is not more than 10-20 -- something that happens in a lot of real-world cases.</p> <p><strong>What matters is the small ratio of the number of different values to the total size</strong>.</p>
 

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