xslt call java instance method

xslt

Solution

The namespace you need to use is the one which refers to the object type, and pass the variable itself as the first argument in your call:

BTW: You need to put the format argument between apostrophes:

<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>

I hope this helps!

Problem

my xsl looks like below : ``` <?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))"/> ``` So now how to call the method format(Date date) of instance 's'? If I use `<xsl:value-of select="s:format($date)" />`, then the error is : prefix must resolve to a namespace : s. But if I add the namespace like this : `xmlns:s="java.text.SimpleDateFormat"`, the `<xsl:value-of select="s:format($date)" />` will return default format, not the specified format. So how can I get the specified format, like MM/dd/yyyy-HH/mm/ss/SSS ?

Original source