XSLT - 获取最接近当前时间戳的节点

时间:2014-04-01 06:44:20

标签: xml xslt xml-parsing xslt-1.0

我有一组日期为时间戳格式的节点,我正在用<xsl:for-each>进行迭代。如何使节点最接近$currentDate中保存的当前日期?

 <xsl:variable name="currentDate" select="1396332574" />

 <items>
   <item>
      <tmstmp>1374249600</tmstmp>
   </item>
   <item>
      <tmstmp>1374249600</tmstmp>
   </item>
   <item>
      <tmstmp>1374257700</tmstmp>
   </item>
   <item>
      <tmstmp>1374418800</tmstmp>
   </item>
   <item>
      <tmstmp>2777068800</tmstmp>
   </item>
 </items>

1 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>

    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:variable name="currentDate" select="1396332574" />

    <xsl:template match="/">
        <xsl:for-each select="items/item">
            <xsl:sort select="$currentDate - ." data-type="number" order="ascending"/>
            <xsl:if test="position() = 1">
                <xsl:copy-of select="."/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

输出:

<item>
   <tmstmp>1374418800</tmstmp>
</item>