xslt调用java实例方法

时间:2012-10-30 09:41:12

标签: xslt

我的xsl如下所示:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
xmlns:SimpleDateFormat="java.text.SimpleDateFormat"
xmlns:Date="java.util.Date" exclude-result-prefixes="SimpleDateFormat Date">

<xsl:variable name="s" select="SimpleDateFormat:new(MMM/dd/yyyy-HH/mm/ss/SSS)"/>
<xsl:variable name="date" select="Date:new(number($beginTime))"/>

那么现在如何调用实例's'的方法格式(日期日期)?

如果我使用<xsl:value-of select="s:format($date)" />,那么错误是:前缀必须解析为命名空间:s。

但是如果我添加这样的命名空间:xmlns:s="java.text.SimpleDateFormat"<xsl:value-of select="s:format($date)" />将返回默认格式,而不是指定的格式。

那么如何获得指定的格式,如MM / dd / yyyy-HH / mm / ss / SSS?

1 个答案:

答案 0 :(得分:5)

您需要使用的名称空间是引用对象类型的名称空间,并将变量本身作为您调用中的第一个参数传递:

顺便说一句:您需要将格式参数放在撇号之间:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:SimpleDateFormat="java.text.SimpleDateFormat" xmlns:Date="java.util.Date" exclude-result-prefixes="SimpleDateFormat Date">
    <xsl:variable name="s" select="SimpleDateFormat:new('MMM/dd/yyyy-HH/mm/ss/SSS')"/>
    <xsl:variable name="date" select="Date:new()"/>
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
    <xsl:template match="*">
        <Test>
            <xsl:value-of select="SimpleDateFormat:format($s,$date)" />
        </Test>
    </xsl:template>
</xsl:stylesheet>

我希望这有帮助!