我正在尝试使用XSL对日期进行一些操作。我在这个脚本上找到了IBM developerWorks的教程。
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
version="1.0"
>
<xsl:output method="html"/>
<!-- B -->
<xsl:variable name="now" select="date:date-time()"/>
<xsl:template match="/">
<!-- The rest of the Web site HTML material would go here -->
<xsl:call-template name="date-section"/>
</xsl:template>
<xsl:template name="date-section">
<p>This page was loaded at <xsl:text/>
<!-- C -->
<xsl:value-of select="concat(date:hour-in-day($now), ':',
date:minute-in-hour($now), ':',
date:second-in-minute($now))"/>
<xsl:text> on </xsl:text>
<xsl:value-of select="concat(date:day-in-month($now), ' ',
date:month-name($now), ' ',
date:year($now))"/>
</p>
<p>
<!-- D -->
<xsl:variable name="days-elapsed"
select="concat('-P',date:day-in-month($now),'D')"/>
<xsl:variable name="one-month-hence"
select="date:add($now, 'P1M')"/>
<xsl:variable name="next-month-start"
select="date:add($one-month-hence, $days-elapsed)"/>
<xsl:variable name="seconds"
select="date:seconds(
date:difference($now, $next-month-start)
)"/>
<xsl:text>The next month starts in </xsl:text>
<xsl:value-of select="$seconds div (3600*24)"/>
<xsl:text> days</xsl:text>
</p>
</xsl:template>
我收到以下错误:
[ERROR]: Cannot find external method 'com.sun.org.apache.xalan.internal.lib.ExsltDatetime.add' (must be public).
[ERROR]: Cannot find external method 'com.sun.org.apache.xalan.internal.lib.ExsltDatetime.difference' (must be public).
[ERROR]: Cannot find external method 'com.sun.org.apache.xalan.internal.lib.ExsltDatetime.seconds' (must be public).
[ERROR]: Cannot convert data-type 'void' to 'real'.
[FATAL]: Could not compile stylesheet
任何想法如何解决它。我正在使用IntelliJ来运行xsl。
最终,我希望能够为日期添加日期。出于某种原因,当我尝试使用xsl 2日期函数时,它只是告诉我它们不存在(并且是的,我确实将标题更改为版本2 :))。所以我试图让它与第三方库一起工作。来自 http://exslt.org/dates-and-times 命名空间的 date-time()函数正在运行,但我无法接通以便能够调用其他函数构成了该命名空间。
答案 0 :(得分:3)
这个问题有点陈旧,但答案是,恕我直言,不太正确,我的回答可能会帮助其他人绊倒这个线程(正如我刚才所做的那样):
xsltproc --dumpextensions | sort
进行概述)。希望这有帮助, 埃里克
答案 1 :(得分:1)
我不知道Xalan,所以我不知道为什么它找不到这些方法。
但是如果你想使用XSLT 2.0日期/时间函数,你需要调用一个XSLT 2.0处理器,Java环境中显而易见的是Saxon(当前版本是9.4)。切换处理器需要的不仅仅是更改版本号:例如,如果从命令行运行它,则需要使用其他命令。