【发布时间】: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的原因。
我正在放置我尝试过的代码,以防将来对其他人有用。这次我要<xsl:call-template />
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