XSL: How to compare two dates?

2019-09-16 12:01发布

问题:

I have an xml like this

<event>
   <name>Jazz festival</name>
   <place>Rome</place>
   <date>23/06/2014</date>
</event>

I want to check if the date is before or later then october 1st 2014 through XSL. Could anyone help me?

回答1:

Try it this way:

XSLT 1.0

<xsl:template match="event">
    <xsl:variable name="date" select="10000 * substring(date, 7, 4) + 100 * substring(date, 4, 2) + substring(date, 1, 2)"/>
    <xsl:choose>
        <xsl:when test="$date > 20141001 ">
            <!-- code for dates later than 2014-10-01  -->
        </xsl:when>
        <xsl:otherwise>
            <!-- code for dates earlier than or equal to 2014-10-01 -->
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

XSLT 2.0

<xsl:template match="event">
    <xsl:variable name="date" select="xs:date(concat(substring(date, 7, 4), '-',  substring(date, 4, 2), '-', substring(date, 1, 2)))"/>
    <xsl:choose>
        <xsl:when test="$date gt xs:date('2014-10-01')">
            <!-- code for dates later than 2014-10-01  -->
        </xsl:when>
        <xsl:otherwise>
            <!-- code for dates earlier than or equal to 2014-10-01 -->
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


回答2:

Thank you. At the end, this is how I did it

<xsl:variable name="data" select="concat(substring-after(substring-after(.,'/'),'/') , format-number( substring-before(substring-after(.,'/'),'/'), '00') , format-number( number( substring-before(.,'/')), '00') )"/>
<xsl:choose>
    <xsl:when test="$data &gt;  '20151018000000'">
        ...
    </xsl:when>
    <xsl:otherwise>
          ...
    </xsl:otherwise>
</xsl:choose>


标签: xslt