Jump to content

Select first record after sort


grantdb

Recommended Posts

Hi There, I currently have something like this. This sorts a select and then uses the first item. I am only interested in the first item. This doesn't seem a good way to do this to me. Is there a better way?ThanksGrant<xsl:for-each select="/forrendering/RESULTS/DOCUMENTS/DOCUMENT[*[name() = $VARselectfieldMatrix] = $VARselectvalueMatrix]"> <xsl:sort select="*[name()=$VARsortbyMatrix]" order="ascending"/> <xsl:if test="position()=1"> <fo:external-graphic fo:content-width="257mm" content-height="160mm" scaling="uniform" src="{$basepathcoverimages}{COVERASSET_BASEPATH}{/forrendering/sections/section[@sectionname=$VARsectionnameMatrix]/bookimageextensionthumb/text()}"/> </xsl:if></xsl:for-each>

Link to comment
Share on other sites

I think the code is fine if you want to access the first node after sorting the nodes.Sorting in ascending order and taking the first one is like accessing the node with the minimum value so with XSLT and XPath 2.0 there might be an alternative using the XPath 2.0 min function http://www.w3.org/TR/xpath-functions/#func-min maybe along the lines of (completely untested!)

<xsl:variable name="elements" select="/forrendering/RESULTS/DOCUMENTS/DOCUMENT[*[name() = $VARselectfieldMatrix] = $VARselectvalueMatrix]"/><xsl:variable name="min-value" select="min($elements/*[name()=$VARsortbyMatrix])"/><xsl:variable name="min-element" select="$elements[$min-value = *[name()=$VARsortbyMatrix]]"/><fo:external-graphic fo:content-width="257mm" content-height="160mm" scaling="uniform" src="{$basepathcoverimages}{$min-element/COVERASSET_BASEPATH}{/forrendering/sections/section[@sectionname=$VARsectionnameMatrix]/bookimageextensionthumb/text()}"/>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...