I know this question might get repeated and also I have went through similar articles and question but I have not found the exact solution.
Now the question
I am using XSLT or XPATH to transform the xml. Here in XML two string variables are there. one is OldDate
and second is CurrentDate
.
Ex : $oldDate = '29.05.2015 15:25:06'
$currentDate ='27.07.2015 14:28:02'.
Now I want to compare those two dates.
If $oldDate > $currentDate then 'OK' else 'Not Ok'.
As I am new to use XSLT and XPATH I did not get that how to proceed from the given answers in other articles.
It will be great-full to provide a perfect solution for this.
Thank you.
XSLT (2.0) recognizes dates in YYYY-MM-DD
format only, and date-times in YYYY-MM-DDThh:mm:ss
format only. In order to compare the strings as dates, (or, in this case, date-times), you must first convert them to valid date-times. Since you need to do this more than once, it would be convenient to construct a function for this purpose:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="http://www.example.com/my"
exclude-result-prefixes="xs my">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="oldDate" select="'29.05.2015 15:25:06'" />
<xsl:variable name="currentDate" select="'27.07.2015 14:28:02'" />
<xsl:function name="my:string-to-datetime">
<xsl:param name="string"/>
<xsl:variable name="parts" select="tokenize($string,'\.|\s')"/>
<xsl:sequence select="xs:dateTime(concat($parts[3], '-', $parts[2], '-', $parts[1], 'T', $parts[4]))" />
</xsl:function>
<xsl:template match="/">
<result>
<xsl:value-of select="if (my:string-to-datetime($oldDate) gt my:string-to-datetime($currentDate)) then 'OK' else 'Not Ok'" />
</result>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="utf-8"?>
<result>OK</result>
Note that this assumes your strings come in a DD.MM.YYYY hh:mm:ss
format - i.e. that the days are padded to two digits - otherwise there's more work to be done.
Given this input XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<oldDate>29.05.2015 15:25:06</oldDate>
<currentDate>27.07.2015 14:28:02</currentDate>
</root>
and this stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:variable name="raw_oldDate" select="substring-before(root/oldDate, ' ')"/>
<xsl:variable name="raw_currentDate" select="substring-before(root/currentDate, ' ')"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="number(concat(substring($raw_oldDate, 7, 4),
substring($raw_oldDate, 4, 2),
substring($raw_oldDate, 1, 2),
translate(substring-after(root/oldDate, ' '), ':', '')))
<
number(concat(substring($raw_currentDate, 7, 4),
substring($raw_currentDate, 4, 2),
substring($raw_currentDate, 1, 2),
translate(substring-after(root/currentDate, ' '), ':', '')))">
<xsl:text>OK</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>not OK</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
it produces:
OK