【问题标题】:Date dependant XSL transformation日期相关的 XSL 转换
【发布时间】:2015-04-24 13:23:09
【问题描述】:

目前我将大量产品代码和其中包含的各种属性输出到 txt 文件。 “项目结束日期”是输出的属性之一。

有没有办法查询这个属性值,如果项目结束日期在今天之后才执行 XSL 函数?

如果有帮助,我正在使用 Altova。

XML:

 <Agility>
<group>
    <product id="295826" subtype="CMS Product" created="20/04/12" modified="03/03/15">
        <attribute definition_id="26" modified="20/04/12" is_name="true" is_identifier="true" scope="global" type="text">
            <data language="English" label="CMS Product Name">012345</data>
        </attribute>
        <attribute definition_id="278" modified="28/08/08" scope="global" type="datetime">
            <data language="English" label="Item End Date">01/01/99</data>
        </attribute>
    </product>
</group>

XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>


<xsl:template match="product[@subtype = 'CMS Product']">

<xsl:for-each select="attribute">
  <xsl:if test="@definition_id = 278"> 
    <xsl:value-of select="data" />
  </xsl:if>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • XSLT 1.0 本身无法提供获取当前日期的方法。您可以在运行时将当前日期作为参数传递给样式表,或者 - 如果您的特定 XSLT 处理器支持它 - 使用扩展函数。 Altova 不是 XSLT 处理器。获取您正在使用的实际处理器的名称,请参见此处:stackoverflow.com/questions/25244370/…
  • 感谢 Michael 的回复 - 确认 Altova GmbH 版本 2.0
  • 在 XSLT 2.0 中,您可以使用 current-date() 函数。但是,在将其与 Item End Date 值进行比较之前,您必须将它们转换为有效的 xs:date 格式(例如“2015-04-24”)。如果看起来您的数据有 2 位数的年份并且可以追溯到 1999 年或更早,那将需要一些工作。
  • 我们可以假设没有结束日期早于 2000 年,因此 /99 将始终为 2099 年。每当我将输出的文件导入 Excel 时,这都会给我带来一个问题!
  • 你当前的日期格式是? “01/01/99”是模棱两可的。

标签: xml xslt altova


【解决方案1】:

试试这个方法?

<xsl:template match="product">
    <xsl:variable name="end" select="attribute/data[@label='Item End Date']" />
    <xsl:variable name="end-date" select="xs:date(concat('20', substring($end, 7, 2), '-', substring($end, 4, 2), '-',substring($end, 1, 2)))" />
    <xsl:if test="$end-date gt current-date()">
        <!-- do your processing here -->
    </xsl:if>
</xsl:template>

【讨论】:

  • 迈克尔太棒了,非常感谢!这将每天为我节省很多时间!
猜你喜欢
  • 2018-06-24
  • 2013-03-02
  • 1970-01-01
  • 2021-03-09
  • 2013-02-23
  • 2013-06-07
  • 2017-09-01
  • 1970-01-01
  • 2012-05-24
相关资源
最近更新 更多