XML-XSLT:如何比较字符串中的两个日期
XML-XSLT : How to compare two dates which are in String
我知道这个问题可能会重复出现,我也看过类似的文章和问题,但我还没有找到确切的解决方案。
现在的问题
我正在使用 XSLT 或 XPATH 来转换 xml。 XML 中有两个字符串变量。一个是 OldDate
,第二个是 CurrentDate
。
Ex : $oldDate = '29.05.2015 15:25:06'
$currentDate ='27.07.2015 14:28:02'.
现在我想比较这两个日期。
If $oldDate > $currentDate then 'OK' else 'Not Ok'.
因为我是 XSLT 和 XPATH 的新手,所以我不知道如何从其他文章中给出的答案着手。
能为此提供一个完美的解决方案就太好了。
谢谢。
鉴于此输入 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>
和这个样式表:
<?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>
它产生:
OK
XSLT (2.0) 仅识别 YYYY-MM-DD
格式的日期和 YYYY-MM-DDThh:mm:ss
格式的日期时间。为了比较字符串 作为日期,(或者,在这种情况下,日期时间),您必须首先将它们转换为有效的日期-次。由于你需要多次这样做,为此构造一个函数会很方便:
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>
结果
<?xml version="1.0" encoding="utf-8"?>
<result>OK</result>
请注意,这假定您的字符串采用 DD.MM.YYYY hh:mm:ss
格式 - 即日期被填充为两位数 - 否则还有更多工作要做。
我知道这个问题可能会重复出现,我也看过类似的文章和问题,但我还没有找到确切的解决方案。
现在的问题
我正在使用 XSLT 或 XPATH 来转换 xml。 XML 中有两个字符串变量。一个是 OldDate
,第二个是 CurrentDate
。
Ex : $oldDate = '29.05.2015 15:25:06'
$currentDate ='27.07.2015 14:28:02'.
现在我想比较这两个日期。
If $oldDate > $currentDate then 'OK' else 'Not Ok'.
因为我是 XSLT 和 XPATH 的新手,所以我不知道如何从其他文章中给出的答案着手。
能为此提供一个完美的解决方案就太好了。
谢谢。
鉴于此输入 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>
和这个样式表:
<?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>
它产生:
OK
XSLT (2.0) 仅识别 YYYY-MM-DD
格式的日期和 YYYY-MM-DDThh:mm:ss
格式的日期时间。为了比较字符串 作为日期,(或者,在这种情况下,日期时间),您必须首先将它们转换为有效的日期-次。由于你需要多次这样做,为此构造一个函数会很方便:
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>
结果
<?xml version="1.0" encoding="utf-8"?>
<result>OK</result>
请注意,这假定您的字符串采用 DD.MM.YYYY hh:mm:ss
格式 - 即日期被填充为两位数 - 否则还有更多工作要做。