Jump to content

Select first record after sort


grantdb
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...