I want my end result to look like this:
<messages>
<message>
<messageID>1</messageID>
<messagedatetime>2009-12-12 09:00:00</messagedatetime>
</message>
</messages>
so I basically want to filter out the old ones based on
messagedatetime
thanks in advance
First of all the standardized dateTime format is e.g.
2009-12-12T09:00:00, your format is not quite correct.
As for filtering elements out, with XSLT 2.0 there is support for the
W3C's dateTime format so you can for instance sort on that type (in
descending order) and output the first of the sorted elements:
<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="messages">
<xsl:copy>
<xsl:for-each select="message">
<xsl:sort select="xsd:dateTime(translate(messagedatetime, ' ',
'T'))" order="descending"/>
<xsl:if test="position() eq 1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
You can run XSLT 2.0 on Windows with Saxon 9
(http://saxon.sourceforge.net/) and AltovaXML tools
(http://www.altova.com/altovaxml.html).
If you want to use XSLT 1.0 then all it can do is compare numbers so you
would need to rewrite above stylesheet as follows:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="messages">
<xsl:copy>
<xsl:for-each select="message">
<xsl:sort select="translate(messagedatetime, ' -:', '')"
data-type="number" order="descending"/>
<xsl:if test="position() = 1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
--
Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/