XSL:
<xsl:template match="int"
xmlns:fib="java:FibonacciNumber">
<int>
<xsl:value-of select="fib:calculate(number(.))"/>
</int>
</xsl:template>
Groovy:
import java.math.BigInteger
import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource
import javax.xml.transform.Templates
class FibonacciNumber {
def calculate(int n) {
if (n <= 0) {
throw new IllegalArgumentException(
"Fibonacci numbers are only defined for positive integers"
)
}
BigInteger low = BigInteger.ONE
BigInteger high = BigInteger.ONE
for (int i = 3; i <= n; i++) {
BigInteger temp = high
high = high.add(low)
low = temp
}
return high
}
}
def fibo = new FibonacciNumber()
def factory = TransformerFactory.newInstance()
def StreamSource xsource = new StreamSource(new File("validPathToXSL.xsl"))
def Templates template = factory.newTemplates(xsource)
def transformer = template.newTransformer()
transformer.setParameter("fib",fibo)
transformer.transform(
new StreamSource(
new File("validPathToXmlFile.xml")),
new StreamResult(System.out)
)
everytime i run a groovy-based transofrmation (from the groovyConsole) Groovy is complaining about not finding the class FibonacciNumber i tried to print the print this.class.getName() and print this.class.getPackage() and i only get the Names and null for each getPackage.
how would you reference the FibonacciNumber groovy class in your xslt to use its methods within the xsl Transformation ?
thanks