Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt call java instance method

Tags:

xslt

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 ?

like image 245
frank Avatar asked Oct 30 '12 09:10

frank


People also ask

How do you call a Java method in XSLT?

The Xalan extension in our XSL file can call any Java method and get the value returned to it—which it can use to fill any node or attribute value. XSLT (Extensible Stylesheet Language Transformation) is an amazing language for transforming XML documents into other XML documents.

Can we use JavaScript in XSLT?

JavaScript can run XSLT transformations through the XSLTProcessor object. Once instantiated, an XSLTProcessor has an XSLTProcessor. importStylesheet() method that takes as an argument the XSLT stylesheet to be used in the transformation. The stylesheet has to be passed in as an XML document, which means that the .

What is Number () in XSLT?

Definition and Usage. The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.


1 Answers

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!

like image 157
Carles Sala Avatar answered Oct 06 '22 03:10

Carles Sala