【问题标题】:XSLT2.0 Transformation error while converting input date to xs:dateTime and formatting the date, Also increment the date将输入日期转换为 xs:dateTime 并格式化日期时出现 XSLT2.0 转换错误,还会增加日期
【发布时间】:2014-02-06 20:11:41
【问题描述】:

我正在尝试将日期转换为有效的dateTime 格式,然后格式化日期。但是,在包含 xs:dateTime 之后,我遇到了转换错误。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <title>doublebell</title>
    <timestamp>02/06/2014 13:51:09</timestamp>
</book>

XSLT:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">

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

    <xsl:template match="/">
        <myentity>
            <label>
                <xsl:value-of select="book/title" />
            </label>
            <date>
                <xsl:apply-templates select="book/timestamp" />
            </date>
        </myentity>
    </xsl:template>

    <xsl:template match="book/timestamp">
        <xsl:variable name="datestr">
            <xsl:value-of
                select="concat(substring(., 7, 4), '-', substring(., 4, 2), '-', substring(., 1, 2), 'T', substring(., 12))" />
        </xsl:variable>

        <!--<xsl:value-of select="$datestr" /> --> <!-- prints 2014-06-02T13:51:09 -->

        <xsl:variable name="date1" as="xs:dateTime" select="xs:dateTime($datestr)" />
        <xsl:variable name="formatteddate1">
            <xsl:value-of select="format-dateTime($date1, '[Y0001]-[M01]-[D01]')" />
        </xsl:variable>
        <xsl:value-of select="$formatteddate1" />

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

我在使用“xs:dateTime”时遇到 XSLT 转换错误。我不知道我在哪里做错了 我正在尝试使用该工具:http://www.freeformatter.com/xsl-transformer.html

PS:我可以使用substring()concat() 函数来格式化日期。但我喜欢以dateTime 格式获取date,这样我就可以将日期增加一天。因此使用xs:dateTime() 函数来获取有效dateTime 格式的数据。 编辑:我所说的有效dateTime 格式是指数据类型“日期”[如数据类型字符串、数字等]

请指出我做错了什么

更新:

即使我尝试这个简单的 XSLT [基于 SO - https://stackoverflow.com/questions/3885292/xslt-subtracting-days]

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/xpath-functions">
    <xsl:template match="/">
        <xsl:value-of select="xs:date('2010-01-01')" />
    </xsl:template>
</xsl:stylesheet>

我收到错误消息。这次我也尝试了我的 Apache Camel XSLT 处理器

错误详情:

The first argument to the non-static Java function 'date' is not a valid object reference. javax.xml.transform.TransformerException: The first argument to the non-static Java function 'date' is not a valid object reference.

更新 1:

看起来这与 XSLT 转换有关 processor

http://www.freeformatter.com/xsl-transformer.html 不是 XSLT2.0 处理器,

而我的 Apache Camel 默认处理器是 1.0,要处理 XSLT2.0,我需要明确提及转换器作为一些 XSLT2.0 处理器,例如 Saxon 9.4.0.1

主要问题已解决

更新 2 和更新 3 是格式化日期增量日期

更新 2: [我没有在其他线程中发帖,因为这是相互关联的]

最后我需要在输入日期中添加一天,这就是我选择xs:dateTime的原因。

我正在放置我尝试过的代码,以防将来对其他人有用。这次我要&lt;xsl:call-template /&gt;

XML:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <title>doublebell</title>
    <timestamp>20131217-13:04:59-UTC</timestamp>
</book>

XSLT:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="text" />

    <xsl:template match="/">
        <date>
            <xsl:call-template name="formatdate">
                <xsl:with-param name="timestampstring" select="book/timestamp" />
            </xsl:call-template>
        </date>
    </xsl:template>


    <xsl:template name="formatdate">
        <xsl:param name="timestampstring" /> <!-- date is 20131217-13:04:59-UTC -->
        <xsl:variable name="datestr"> 
            <xsl:value-of
                select="concat(substring($timestampstring, 1, 4), '-', substring($timestampstring, 5, 2), '-', substring($timestampstring, 7, 2), 'T', substring($timestampstring,10, 8))" />
        </xsl:variable>
        date: <xsl:value-of select="$datestr" /><!-- date is 2013-12-17T13:04:59 -->
        date in custom format: <xsl:value-of select="format-dateTime($datestr, '[Y0001]-[M01]-[D01]')" />
        date+1: <xsl:value-of select="xs:dateTime($datestr) + xs:dayTimeDuration('P1D')" /> <!-- add one day -->
        date+3: <xsl:value-of select="xs:dateTime($datestr) + xs:dayTimeDuration('P3D')" /> <!-- add three days -->
    </xsl:template>

</xsl:stylesheet>

输出

date: 2013-12-17T13:04:59
        date in custom format: 2013-12-17
        date+1: 2013-12-18T13:04:59 
        date+3: 2013-12-20T13:04:59

更新 3

如何使添加天数,更多generic? 我是这样做的,

<!-- Made generic, increment interval can be passed as parameter -->
<xsl:template name="formatdateafterincrement">
    <xsl:param name="timestampstring" />
    <xsl:param name="incr" />
    <!-- input format: 'YYYYMMDD-HH:MM:SS-UTC' -->
    <!-- output format after date increment: YYYY-MM-DD -->
    <xsl:variable name="datestr">
        <xsl:value-of
            select="concat(substring($timestampstring, 1, 4), '-', substring($timestampstring, 5, 2), '-', substring($timestampstring, 7, 2), 'T', substring($timestampstring,10, 8))" />
    </xsl:variable>
    <xsl:variable name="datestrincr">
        <xsl:value-of select="xs:dateTime($datestr) + xs:dayTimeDuration($incr)" />
    </xsl:variable>
    <xsl:value-of select="format-dateTime($datestrincr, '[Y0001]-[M01]-[D01]')" />
</xsl:template>

<!-- how to call the template -->
<newdate>
    <xsl:call-template name="formatdateafterincrement">
        <xsl:with-param name="timestampstring" select="book/timestamp" />
        <xsl:with-param name="incr" select="'P1D'" /> <!-- one day -->
    </xsl:call-template>
</newdate>

http://xsltransform.net/ 是一个很好的 XSLT 2.0 处理器。

这很好用。如果有人有任何建议,请分享。我没有具体的问题。问题得到解答。

【问题讨论】:

  • 有人能告诉我为什么上面的代码没有以代码格式显示吗?我做了和以前一样的格式化。选择代码块并应用CNTRL+K

标签: apache-camel xslt-2.0


【解决方案1】:

原因是我用来转换的两个处理器都是 XSLT1.0 处理器。

我提到的在线工具:http://www.freeformatter.com/xsl-transformer.html 不是 XSLT2.0 处理器。此外,我使用 Apache Camel 的应用程序具有默认的 XSLT 处理器 1.0。使用XSLT 2.0 processor 可以解决问题。所以这个错误与使用 XSLT 1.0 处理器有关

另外,仅仅声明&lt;xsl:stylesheet version="2.0" 并不意味着您正在使用 XSLT 2.0 处理器。这是我的另一个错误 [不合逻辑] 假设!

要运行 xs:datexs:dateTime ,我应该使用 XSLT 2.0 处理器

我在我的 Apache Camel 配置中做了什么:

添加Saxon 9.4.0.1作为依赖[作为jar或pom.xml依赖], 在我的骆驼路线中,我进行了更改:

<route..
<from uri=
<to uri="xslt:stylesheets/test.xslt?transformerFactoryClass=net.sf.saxon.TransformerFactoryImpl" />

【讨论】:

  • xsltransform.net 可能对您有用,因为它还具有 XSLT 2.0 处理器
  • @JoelM.Lamsen 这个工具非常有用,我玩过 xs:dateTime 和其他格式功能。但我看到一个场景的一些奇怪的输出行为。我在这里发布了一个 SO 问题 - stackoverflow.com/questions/21640457/…
【解决方案2】:

运行您的代码并得到&lt;date&gt;2014-06-02&lt;/date&gt;

当您说“有效日期时间”时,您是什么意思? Python日期时间? php?

【讨论】:

  • 我所说的有效dateTime 格式是数据类型“日期时间”的东西。 XSL 转换器可以理解的日期时间数据类型,以便我可以增加或减少日期 [如数据类型字符串、数字等)。我在基于 java 的应用程序中使用 XSLT。您在哪里运行此代码?
  • 当我尝试使用`xs:date`或`xs:dateTime`时触发错误
  • 感谢您确认您获得了相同代码的输出,这意味着您使用不同的处理器运行
  • 我在 Linux 上用 OxygenXML 运行它。
  • 感谢您在 OxygenXML 中试用
猜你喜欢
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
  • 2019-08-03
  • 2023-03-30
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
相关资源
最近更新 更多