I need to have XSLT call a method on a Java instance that I pass as a parameter. So far I can only get it to work if I create the instance in the XSLT itself. If I attempt to call it on the passed instance it fails with
Exception in thread "main" javax.xml.transform.TransformerConfigurationException:
Cannot find external method 'Test.get' (must be public).
I can prove the instance is being passed ok by outputting it (it comes out as the toString). Here is my Java:
public class Test {
public static void main(String[] args) throws Exception {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(
new StreamSource(Test.class.getResourceAsStream("test.xsl")));
transformer.setParameter("test1", new Test());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
transformer.transform(new StreamSource(
new ByteArrayInputStream(
"<?xml version=\"1.0\"?><data></data>".getBytes())),
new StreamResult(outputStream));
System.out.println(outputStream.toString());
}
public String get() {
return "hello";
}
@Override
public String toString() {
return "An instance of Test";
}
}
and here is my xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:test="xalan://Test"
exclude-result-prefixes="test"
>
<xsl:param name="test1" />
<xsl:variable name="test2" select="$test1"/>
<xsl:variable name="test3" select="test:new()"/>
<xsl:template match="/">
<data>
<!-- proves that the instance is really being passed -->
<xsl:value-of select="$test1"/>
</data>
<data>
<!-- first two do not work -->
<!--<xsl:value-of select="test:get($test1)"/>-->
<!--<xsl:value-of select="test:get($test2)"/>-->
<!-- this one does work -->
<xsl:value-of select="test:get($test3)"/>
</data>
</xsl:template>
</xsl:stylesheet>
Does anyone know how I can make this work with the passed parameter? Instantiating it in the XSLT is not going to work in my actual use case. Thanks.