Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 30th, 2005, 12:06 PM
Authorized User
Join Date: Aug 2005
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Min / Max followup

I've reviewed a previous thread about determining the minmum and maximum values in a node-set. I have an interesting related question:

How do you retrieve the minimum or maximum values ONLY ONCE assuming that there are multiple equl values?

For instance, in the follwoing xml file, there are several equal date values:

<?xml version="1.0"?>
<?xml-stylesheet href="sortdates.xsl" type="text/xsl"?>
<user name="employee name" id="001" />
<project name="Project 1" number="23" interval="minutes">
<entry date="01/01/2005" duration="20000"/>
<entry date="01/01/2005" duration="3000"/>
<entry date="01/02/2005" duration="36"/>
<project name="Project 2" number="36" interval="hours">
<entry date="01/01/2005" duration="20000" />
<entry date="01/01/2005" duration="3000"/>
<entry date="01/02/2005" duration="30"/>
<project name="Project 3" number="45" interval="minutes">
<entry date="01/02/2005" duration="20000"/>
<entry date="01/01/2005" duration="1000"/>
<entry date="01/03/2005" duration="2"/>


And the stylesheet to go with it:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output method="html"/>

    <xsl:variable name="entries" select=".//entry"/>

    <xsl:template match="/">
    <xsl:for-each select="$entries">

        <xsl:if test="not(//entry/@date &lt; ./@date)">
                lowdate = <xsl:value-of select="./@date"/><br/>

        <xsl:if test="not(//entry/@date &gt; ./@date)">
                highdate = <xsl:value-of select="./@date"/><br/>


                <style type="text/css" media="all">@import "timesheet.css";</style>




which basically prints out the low and high dates in the node set, except that the matching equal values are repeated as follows:

lowdate = 01/01/2005
lowdate = 01/01/2005
lowdate = 01/01/2005
lowdate = 01/01/2005
lowdate = 01/01/2005
highdate = 01/03/2005


Can anyone suggest something? Thanks...

  #2 (permalink)  
Old August 31st, 2005, 09:11 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

You could use the standard Munchian methods to remove duplicates. However, this is an inefficient way of getting min and max anyway. A better approach is to do a sort and take the first/last in the sorted sequence - this won't give you any duplicates.

<xsl:for-each select="xxx">
  <xsl:sort .../>
  <xsl:if test="position()=1">

  <xsl:if test="position()=last()">

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old August 31st, 2005, 12:02 PM
Authorized User
Join Date: Aug 2005
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks Michael, it works like a charm...

Similar Threads
Thread Thread Starter Forum Replies Last Post
How does one get the Max or Min Filename? crabjoe ASP.NET 1.0 and 1.1 Basics 4 January 15th, 2008 04:48 PM
MAX/MIN Value Help slbibs SQL Server 2005 4 November 6th, 2007 04:00 AM
MIN( MAX( UPDATE Stuart Stalker SQL Language 3 February 21st, 2007 07:10 AM
MAX and MIN ON TD cleytonjordan XSLT 7 July 27th, 2005 12:39 PM
How to use Min,Max,Abs functions braindog_43 XSLT 0 February 7th, 2005 06:04 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.