Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

xml - Tokenize and compare dates in xslt 1.0

I have a variable in xslt1.0 which contains dates with a separator like ";Aug 11, 2015 11:16;Aug 07, 2015 08:27;Aug12, 2015 15:14" I want to tokenize this variable value and get the latest date and store it in a variable. Can someone please help me.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Xalan supports the EXSLT str:tokenize() function, so that will take care of that. After that, you just need to sort the tokens by the individual date & time components, and grab the last one.

<xsl:for-each select="str:tokenize($dates, ';')">
    <!-- sort by year -->
    <xsl:sort select="substring(., 9, 4)"/>
    <!-- sort by month -->
    <xsl:sort select="string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec', substring(., 1, 3)))" data-type="number"/>
    <!-- sort by day -->
    <xsl:sort select="substring(., 5, 2)"/>
    <!-- sort by time -->
    <xsl:sort select="substring(., 14, 4)"/>
    <xsl:if test="position()=last()">
        <xsl:value-of select="."/>
    </xsl:if>
</xsl:for-each>

Note that this will not work if your dates are not all in the same format (in your input, the last date does not have a space between month and day).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...