Call Java methods from within the XSLT

makes use of the possibility to introduce new XPath functions

  • Define the namespace prefix (use Class Name) and bind this prefix to the Java Class object

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:Math="http://www.math.org/">
      <xsl:script implements-prefix="Math" language="java" src="java:java.lang.Math"/>
      ...
    </xsl:stylesheet>
    					
  • Define the Java method name using the local-name of the XPath function

      <xsl:template name="test">
        <value-of select="Math:min( 100, 101)"/>
      </xsl:template>
    					
  • Construct a new Java Object using new()

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:Math="http://www.date.org/">
      <xsl:script implements-prefix="Date" language="java" src="java:java.util.Date"/>
    
      <xsl:template name="test">
        <variable name="date" select="Date:new()"/>
        <value-of select="Date:toString( $date)"/>
      </xsl:template>
    </xsl:stylesheet>
    					
  • Convert the arguments to Java Objects

      <xsl:template name="test">
        <value-of select="Math:min( number( @value1), number( @value2))"/>
      </xsl:template>
    					
  • Handle the return value.