Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get the earliest and latest dates in a node set?

3,379 views
Skip to first unread message

Chris Gannon

unread,
Nov 25, 2009, 7:57:16 PM11/25/09
to
I'm trying to determine the earliest and latest StartDate in a record
set. I can compare a value in one node to it's preceding sibling by
converting the dates to numbers and comparing them but I'm limited to
2 rows. I can't figure out how to compare the StartDates of all nodes
and extracting the earliest or latest.

Can someone point me in the right direction?

Happy Thanksgiving!

Sample dataset
<dsQueryResponse>
<Rows>
<Row ProjectName="Elvis" StartDate="2009-10-01T04:00:00Z"
TargetDate="2009-12-21T04:00:00Z" Overall_x0020_Status="On Track"
_x0025__x0020_Complete=".8" />
<Row ProjectName="Dolly" StartDate="2009-09-11T04:00:00Z"
TargetDate="2009-12-01T04:00:00Z" Overall_x0020_Status="On Track"
_x0025__x0020_Complete=".66" />
<Row ProjectName="CCR" StartDate="2009-09-21T04:00:00Z"
TargetDate="2009-11-21T04:00:00Z" Overall_x0020_Status="Manageable
Issues" _x0025__x0020_Complete=".90" />
<Row ProjectName="Stones" StartDate="2009-09-15T04:00:00Z"
TargetDate="2009-12-28T04:00:00Z" Overall_x0020_Status="Project
Concern" _x0025__x0020_Complete=".50" />
</Rows>
</dsQueryResponse>

Martin Honnen

unread,
Nov 26, 2009, 7:39:44 AM11/26/09
to

With XSLT 2.0 it is as easy as taking the min and max e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xsd"
version="2.0">

<xsl:template match="/">
<xsl:variable name="dates" as="xsd:dateTime*"
select="dsQueryResponse/Rows/Row/@StartDate/xsd:dateTime(.)"/>
<xsl:text>Minimum date: </xsl:text>
<xsl:value-of select="min($dates)"/>
<xsl:text>; maximum date: </xsl:text>
<xsl:value-of select="max($dates)"/>
</xsl:template>

</xsl:stylesheet>

You can run XSLT 2.0 in the COM world (e.g. JScript/VBScript, VB(A))
with AltovaXML tools http://www.altova.com/altovaxml.html and in the
.NET world with Saxon 9 (http://saxon.sourceforge.net/).


If you want to solve it with XSLT 1.0 then you need to sort and take the
first and last item in the sorted sequence:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="/">
<xsl:for-each select="dsQueryResponse/Rows/Row/@StartDate">
<xsl:sort select="translate(., '-T:Z', '')" data-type="number"/>
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:text>Minimum date: </xsl:text>
<xsl:value-of select="."/>
</xsl:when>
<xsl:when test="position() = last()">
<xsl:text>; maximum date: </xsl:text>
<xsl:value-of select="."/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

That approach with converting the xsd:dateTime to a number and
compare/sort on that number will only work if all items are in the same
time zone (as in your sample data Z) so that you can strip that off.

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

Chris Gannon

unread,
Nov 30, 2009, 9:00:49 AM11/30/09
to

Brilliant! Thanks Martin! btw- I am relegated to xslt 1.0. sorting
NEVER even occured to me! Either I need a lot more coffee or a lot
less.

You rock!

CG

Message has been deleted
0 new messages