XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Nick RoperSubject: Using a template to sort and return a value
Author: Nick Roper
Date: 09 Mar 2007 04:01 AM
Hi,

I'm fairly new to xslt, but have been managing to get along OK(ish) so far. However, I'm having problems trying to get my head around a certain process.

Here is the content of an example 'myfile.xml' file:

<UserData>
<Views>
</Views>
<viewedproducts>
<product id="39" time="633085244872194905">1</product>
<product id="33" time="633085245009538655">1</product>
<product id="43" time="633085245096569905">1</product>
<product id="46" time="633085245166726155">1</product>
</viewedproducts>
</UserData>

What I want to do is to assign the id of the most recently viewed product to a variable 'lastProductId'. I will need to do this at various points in the application, so the code that carries out the assignment should be stored in a separate 'widget' file that is imported into the main xslt, for example:

<xsl:import href="widgets/mywidgets.xslt" />

Then, from within the main xslt I would make the assignment as follows:

<xsl:variable name="lastProductId">
<xsl:call-template name="get_last_viewed_product" />
</xsl:variable>

This would call the get_last_viewed_product template in mywidgets.xslt and return the value.

My code so far for the template is as follows:

<xsl:template name="get_last_viewed_product">
<xsl:variable name="viewedproducts" select="document('path/to/file/myfile.xml')/UserData/viewedproducts/product"/>

</xsl:template>

This should mean that $viewedproducts now contains:

<product id="39" time="633085244872194905">1</product>
<product id="33" time="633085245009538655">1</product>
<product id="43" time="633085245096569905">1</product>
<product id="46" time="633085245166726155">1</product>

What I want to do is to return the id of the product with the highest time attribute. Not sure if I can do this by using some kind of 'max' function, or whether I need to sort the elements and the find the first (or last depending on sort order)

I've tried various approaches but can't get anything to work - any help would be very greatly appreciated.

Many thanks,


Nick







Posttop
Tony LavinioSubject: Using a template to sort and return a value
Author: Tony Lavinio
Date: 09 Mar 2007 03:06 PM
<xsl:template match="/">
<xsl:variable name="max" select="max(UserData/viewedproducts/product/@time)"/>
<xsl:copy-of select="UserData/viewedproducts/product[@time = $max]"/>
</xsl:template>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.