【问题标题】:XSLT display data using string dateXSLT 使用字符串日期显示数据
【发布时间】:2010-11-09 00:30:06
【问题描述】:

大家好,

我有以下 XML 数据:

<?xml version="1.0" encoding="utf-8" ?>
<Publicity>
    <pub display="left" type="image" url="http://www.link.to.website" alt="Lorem ipsum" startDate="2010-09-01" endDate="2011-09-01">http://link.to.image/banner.gif</pub>
    <pub display="left" type="image" url="http://www.link.to.website" alt="Lorem ipsum" startDate="2010-09-01" endDate="2011-09-01">http://link.to.image/banner.gif</pub>
    <pub display="left" type="image" url="http://www.link.to.website" alt="Lorem ipsum" startDate="2010-09-01" endDate="2011-09-01">http://link.to.image/banner.gif</pub>
    <pub display="left" type="image" url="http://www.link.to.website" alt="Lorem ipsum" startDate="2010-09-01" endDate="2011-09-01">http://link.to.image/banner.gif</pub>
    <pub display="left" type="image" url="http://www.link.to.website" alt="Lorem ipsum" startDate="2010-09-01" endDate="2011-09-01">http://link.to.image/banner.gif</pub>
</Publicity>

我只想显示根据日期值startDateendDateXSLT 1.0 中的当前日期匹配的有效pub

仅供参考,此 XML 位于静态文件中,并通过 document() 函数包含在内。整个网站使用 PHP 的xsltProcessor() 函数在服务器端处理。

我希望很快就能看到 PHP 在本地运行 XSLT 2.0 处理器。

任何提示将不胜感激。

谢谢。

【问题讨论】:

    标签: date xslt xslt-1.0


    【解决方案1】:

    鉴于您的 startDate 和 endDate 采用“YYYY-mm-dd”格式,您只需去掉连字符并进行数字比较:

    <xsl:variable name="numericCurrentDate"
                  select="number(translate($currentDate, '-', ''))"/>
    <xsl:if test="$numericCurrentDate >= number(translate(startDate, '-', '')) and
                  number(translate(endDate, '-', '')) >= $numericCurrentDate">...
    

    (感谢http://www.dpawson.co.uk/xsl/sect2/N7218.html

    注意,translate(string, 'abcde...', '') 返回参数字符串的副本,其中删除了所有出现的字符 'abcde...'。

    或者,您可以使用扩展功能。 PHP 的 XSLT(使用 libxslt)应该支持像 EXSLT 的 date:difference 这样的扩展,只要文档说它们是由 libxslt 实现的......他们在这种情况下就是这样做的。

    date:difference 函数返回 第一个日期之间的持续时间 和第二次约会。如果第一次约会 发生在第二个日期之前,那么 结果是正的持续时间;如果 它发生在第二个日期之后, 结果是负的持续时间。

    因此你可以使用:

    <xsl:variable name="currentDate" select="date:date()" />
    <xsl:for-each select="...">
        <xsl:if test="starts-with(date:difference($currentDate, startDate), '-') and
                      starts-with(date:difference(endDate, $currentDate), '-')">
           Do something with publication...
    

    请注意,EXSLT 为我们提供了一个获取当前日期的函数,因此我们不必将它作为参数传递,还提供了一种比较两个日期的方法。

    有关如何使用 EXSLT 的日期模块的信息,包括如何声明命名空间,请参阅EXSLT docs。但请注意,并非所有 XSLT 处理器都支持 EXSLT 函数,因此您将牺牲一些可移植性。

    【讨论】:

      【解决方案2】:

      您最好的选择可能是在as a parameter 中传递当前日期。不幸的是,XSLT 1.0 不提供任何日期函数。

      【讨论】:

      • 是的,该死的 XSLT 1.0!通过互联网阅读后,诀窍是做一些substring 比较。
      • 嗯,你将如何使用子字符串?如果日期都采用相同的格式,则不需要。
      • 我想我找到了办法。伪代码:&lt;xsl:variable name="currentDate" select="2010-11-08" /&gt;&lt;xsl:for-each select="Publicity/pub"&gt;&lt;xsl:if test="substring(@startDate, 0, 4) = substring($currentDate, 0, 4) etc..."&gt; do something with matched pub &lt;/xsl:for-each&gt;
      猜你喜欢
      • 2016-07-02
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      相关资源
      最近更新 更多