Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

xml - XSLT 1.0: find the maximum value from given date and time

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>

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

eventname 4/29/2015 6:55:58 PM

Any suggestions on how to approach ?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. Convert your dates to a sortable string in the form of YYYMMDDhhmmss. This is further complicated by the necessity to convert 12-hour time to 24-hour format.
  2. Sort the resulting nodes and output the first (or last, depending on the sort order) one of these.

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>

Result

?xml version="1.0" encoding="utf-8"?>
<output>
  <eventname>Test2</eventname>
  <dateevent>4/29/2015 6:55:58 PM</dateevent>
</output>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...