XSLT - Java Transformation (Call|Extension)

Card Puncher Data Processing

About

How to call java method from an Xsl stylesheet

Xalan is the default processor of the JDK. It implements XSL Transformations (XSLT) Version 1.0

Syntax

XSLT is not an object oriented language. Static methods fit much more neatly into its structures than do objects and instance methods.

Static Method

<xsl:value-of select="myPrefix:getMyStaticMethod(number(myNodeName))" xmlns:myPrefix="xalan://myPackage.myClass"/>

Dynamic

To create an instance of an object:

prefix:new (args)

where prefix is the extension namespace prefix.

A new instance is to be created with the args constructor arguments (if any). All constructor methods are qualified for method selection. Example:

<xsl:variable name="myType" select="my-class:new()">

To invoke an instance method on a specified object:

prefix:methodName (object, args)

where:

  • prefix is the extension namespace prefix
  • methodName is the name of the method to invoke on object with the args arguments.
  • object must be an object of the class indicated by the namespace declaration.

Example:

<xsl:variable name="new-pop" select="my-class:valueOf($myType, string(@myAttribute))">

Support

The first argument to the non-static Java function is not a valid object reference

ERROR:  'The first argument to the non-static Java function 'getWorkHierarchyType' is not a valid object reference.'
FATAL ERROR:  'The first argument to the non-static Java function 'getWorkHierarchyType' is not a valid object reference.'

Just verify that the path that refers to the static method is the good one.

Cannot convert argument/return type

WARNING:  'org.apache.xerces.jaxp.SAXParserImpl: http://javax.xml.XMLConstants/property/accessExternalDTD'
ERROR:  'Cannot convert argument/return type in call to method 'myPackage.myClass.getMyStatiscMethod(node-set)''
FATAL ERROR:  'Cannot convert argument/return type in call to method 'myPackage.myClass.getMyStatiscMethod(node-set)''
Exception in thread "main" javax.xml.transform.TransformerConfigurationException: Cannot convert argument/return type in call to method 'myPackage.myClass.getMyStatiscMethod(node-set)'
	at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:994)
	at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:766)

It seems that the return value has a node-set datatype. By applying a conversion function, the problem was resolved.

Documentation / Reference

See http://xml.apache.org/xalan-j/extensions.html







Share this page:
Follow us:
Task Runner