从xslt 2.0或xslt 1.0中的给定三个日期变量中查找第一,第二和第三最高日期时间变量

时间:2014-08-19 05:56:50

标签: xslt xslt-2.0

我有三个日期时间变量,我需要找到第一,第二和第三高。

这是实际要求

第1步:获得第一个最高日期&时间(三分之一),应该小于当前日期时间+ 1个月。如果是,请考虑第一个最高的第二步。

第2步:获得第二高的日期&时间(两个),它应该小于当前日期时间+ 1个月。如果是,请考虑第二个最高的第3步。

第3步:获取第三个日期&时间,它应该小于当前日期时间+ 1个月。如果是,请考虑第三个其他返回NULL。

我确信下面的代码真的很烦人,但建议/支持表示赞赏。

    <xsl:template match="SUBSCRIBER">
        <xsl:variable name="first_date_time" as="xs:dateTime" select="xs:dateTime('2014-08-20T00:00:00')"/>
        <xsl:variable name="second_date_time" as="xs:dateTime" select="xs:dateTime('2014-08-21T00:00:00')"/>
        <xsl:variable name="third_date_time" as="xs:dateTime" select="xs:dateTime('2014-08-18T00:00:00')"/>

        <xsl:variable name="product">
            <xsl:perform-sort select="./.">
                <xsl:sort select="$first_date_time"/>
                <xsl:sort select="$second_date_time"/>
                <xsl:sort select="$third_date_time"/>
            </xsl:perform-sort>

        </xsl:variable>

        <xsl:copy-of select="$product"/>


    </xsl:template>



</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

难道你不能简单地拿走不到一个月的所有给定日期,对它们进行排序并取结果的前三个吗?


编辑:

例如,请考虑以下样式表:

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"
exclude-result-prefixes="xs">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <xsl:variable name="first_date_time" as="xs:dateTime" select="xs:dateTime('2014-08-15T00:00:00')"/>
    <xsl:variable name="second_date_time" as="xs:dateTime" select="xs:dateTime('2014-09-15T00:00:00')"/>
    <xsl:variable name="third_date_time" as="xs:dateTime" select="xs:dateTime('2014-10-15T00:00:00')"/>

    <xsl:variable name="cut-off-date" select="current-dateTime() + xs:yearMonthDuration('P1M')" as="xs:dateTime"/>

    <xsl:variable name="qualifying-dates" as="xs:dateTime*">
        <xsl:perform-sort select="($first_date_time, $second_date_time, $third_date_time)[. &lt; $cut-off-date]">
            <xsl:sort select="xs:dateTime(.)" order="descending"/>
        </xsl:perform-sort>
    </xsl:variable>

    <!-- output -->
    <output>
        <xsl:for-each select="$qualifying-dates" >
            <date><xsl:value-of select="." /></date>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

如果今天(2014-08-20)应用于任何XML输入,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <date>2014-09-15T00:00:00</date>
   <date>2014-08-15T00:00:00</date>
</output>

正如您所看到的,超过一个月之外的日期将从输出中排除,其余日期按降序排序 - 因此第一个返回日期是距离不到一个月的最新日期