XSLT 1.0:从给定的日期和时间找到最大值 [英] XSLT 1.0: find the maximum value from given date and time
问题描述
大家好,
我有以下格式的 xml 事件(月、日、年、小时、分钟、秒 AM/PM).我想提一下,我无法控制生成的 XML.现在我需要找到最大或最新的dateevent"并选择相应的eventname".XML 看起来像
Hi all,
I have the xml event in the following format (month,date,year,hour,minute,seconds AM/PM). I would like to mention that, I don't have the control over the generated XML. Now I need to find the maximum or latest "dateevent" and select the corresponding "eventname". The XML looks like
<?xml version="1.0" standalone="no"?>
<day>
<day-event>
<eventname>Test1</eventname>
<dateevent>1/30/2014 7:15:50 AM</dateevent>
</day-event>
<day-event>
<eventname>Test2</eventname>
<dateevent>4/29/2015 6:55:58 PM</dateevent>
</day-event>
<day-event>
<eventname>Test3</eventname>
<dateevent>12/29/2014 9:33:24 PM</dateevent>
</day-event>
</day>
在此 XML 中,我必须选择最新的日期事件"(即 2015 年 4 月 29 日下午 6:55:58),并选择相应的事件名称"(即Test2).关于如何做到这一点的任何建议..?应用程序使用 SAX 解析器进行转换.我尝试了排序方法,但没有成功.我的结果应该看起来像
In this XML I have to select the latest "dateevent"(ie) 4/29/2015 6:55:58 PM and also select the corresponding "eventname"(ieTest2). Any suggestions on how to do this..? Application uses SAX parser to make the transformation. I tried sorting approach but I wasn't successful. My result should look like
活动名称 2015/4/29 下午 6:55:58
eventname 4/29/2015 6:55:58 PM
关于如何接近的任何建议?
Any suggestions on how to approach ?
谢谢
推荐答案
XSLT 1.0 没有日期的概念(甚至 XSLT 2.0 也只能识别 ISO-8601 格式的日期).因此,您需要分两步执行此操作:
XSLT 1.0 has no concept of dates (and even XSLT 2.0 only recognize dates in ISO-8601 format). So you need to do this in two steps:
- 将日期转换为 YYYMMDDhhmmss 形式的可排序字符串.由于需要将 12 小时制转换为 24 小时制,这进一步复杂化.
- 对结果节点进行排序并输出其中的第一个(或最后一个,取决于排序顺序).
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/day">
<!-- first pass -->
<xsl:variable name="events">
<xsl:for-each select="day-event">
<event name="{eventname}" date="{dateevent}">
<xsl:call-template name="convert-date">
<xsl:with-param name="datestring" select="dateevent"/>
</xsl:call-template>
</event>
</xsl:for-each>
</xsl:variable>
<!-- output -->
<output>
<xsl:for-each select="exsl:node-set($events)/event">
<xsl:sort select="." order="descending"/>
<xsl:if test="position()=1">
<eventname>
<xsl:value-of select="@name" />
</eventname>
<dateevent>
<xsl:value-of select="@date" />
</dateevent>
</xsl:if>
</xsl:for-each>
</output>
</xsl:template>
<xsl:template name="convert-date">
<xsl:param name="datestring"/>
<xsl:variable name="date" select="substring-before($datestring, ' ')" />
<xsl:variable name="time" select="substring-before(substring-after($datestring, ' '), ' ')" />
<xsl:variable name="M" select="substring-before($date, '/')" />
<xsl:variable name="D" select="substring-before(substring-after($date, '/'), '/')" />
<xsl:variable name="Y" select="substring-after(substring-after($date, '/'), '/')" />
<xsl:variable name="h12" select="substring-before($time, ':')"/>
<xsl:variable name="m" select="substring-before(substring-after($time,':'), ':')"/>
<xsl:variable name="s" select="substring-after(substring-after($time,':'), ':')"/>
<xsl:variable name="pm" select="contains($datestring,'PM')"/>
<xsl:variable name="h" select="$h12 mod 12 + 12*$pm"/>
<xsl:value-of select="format-number($Y, '0000')" />
<xsl:value-of select="format-number($M, '00')" />
<xsl:value-of select="format-number($D, '00')" />
<xsl:value-of select="format-number($h, '00')" />
<xsl:value-of select="format-number($m, '00')" />
<xsl:value-of select="format-number($s, '00')" />
</xsl:template>
</xsl:stylesheet>
结果
?xml version="1.0" encoding="utf-8"?>
<output>
<eventname>Test2</eventname>
<dateevent>4/29/2015 6:55:58 PM</dateevent>
</output>
这篇关于XSLT 1.0:从给定的日期和时间找到最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!