Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like for each "model", you are trying to find the cheapest Item for that model.</p> <p>To get the <strong>SupplierCode</strong> for the current <strong>Price</strong> element you are on, you could just do this...</p> <pre><code>&lt;xsl:template match="Price"&gt; &lt;xsl:if test="position() = 1"&gt; &lt;xsl:value-of select="."/&gt; &lt;xsl:value-of select="../SupplierCode" /&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; </code></pre> <p>Where <code>..</code> is used to get the parent of the current node.</p> <p>Alternatively, what you could do is instead of selecting the <strong>Price</strong> elements with you template, select the <strong>Item</strong> element, ordered by <strong>Price</strong></p> <pre><code>&lt;xsl:apply-templates select="current-group()"&gt; &lt;xsl:sort select="Price" data-type="number" order="ascending"/&gt; &lt;/xsl:apply-templates&gt; </code></pre> <p>Then, you can have a template matching the <strong>Item</strong> element, and in this you be easily able to access the <strong>Price</strong> and <strong>Code</strong></p> <pre><code>&lt;xsl:template match="Item"&gt; &lt;xsl:if test="position() = 1"&gt; &lt;xsl:value-of select="Price" /&gt; &lt;xsl:value-of select="Code" /&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; </code></pre> <p>EDIT: In answer to your comment, if you only wanted the SupplierCode for the Item with the lowest price, you would change the template matching the <strong>Item</strong> element to output just the supplier code, and then wrap the <strong>xsl:apply-templates</strong> in a variable.</p> <p>Try this XSLT</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;xsl:for-each-group select="Items/Item" group-by="@model"&gt; &lt;xsl:variable name="SupplierCode"&gt; &lt;xsl:apply-templates select="current-group()"&gt; &lt;xsl:sort select="Price" data-type="number" order="ascending"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:variable&gt; &lt;xsl:value-of select="$SupplierCode" /&gt; &lt;/xsl:for-each-group&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Item"&gt; &lt;xsl:if test="position() = 1"&gt; &lt;xsl:value-of select="SupplierCode"/&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Actually, there is another way of doing this. You could make use of the <strong>min</strong> operator in XSLT 2.0</p> <pre><code> &lt;xsl:value-of select="current-group()[number(Price)=min(current-group()/Price)]/SupplierCode" /&gt; </code></pre> <p>This wouldn't necessarily be as efficient as the previous method though because you are effectively checking the "current-group()" twice. First to get the minimum value, and then again to find the Item in the current-group with the lowest price.</p>
    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.
 

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