I'm trying to use arrays in XSLT Extensions using java .
I'm getting the below error :
Caused by: java.lang.ClassCastException: org.apache.xpath.objects.XObject
cannot be cast to org.apache.xpath.objects.XNodeSet.
The way I used arrays is. An extension class method
public static String[] getEvents(String contractIdStr,String tradeIdStr) {
return new String[]{"MacroType","Type","SubType"};
}
Inside XSL ,
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:partyrefrule="com.converter.Rules"
exclude-result-prefixes="partyrefrule">
<xsl:variable name="vLastNegoTradeEvents">
<xsl:value-of select="partyrefrule:getEvents($cVal,$tVal)"/>
</xsl:variable>
<xsl:message terminate="no">
<xsl:value-of select="$vLastNegoTradeEvents[0]"/>
</xsl:message>
</xsl:stylesheet>
I'm using XALAN Parse for this.
I don't think you can have an XPath function returning an array of string. What you can create however is an XPath XALAN extention that returns a NodeSet. In your case, you probably want a node set containing text nodes. Then, all you have to do is loop on all the nodes of your node set to retrieve all the strings you have generated in your extension node-set.
I have refactored a little bit you example to illustrate what I believe a solution as close as possible to what you want should look like. The first class is your extension. As I said, it creates a node set rather than array of strings. The second class has the XSL. It's a little bit cryptic because I needed to have everything (compact) in java in order to use eclipse java debugger (mixing eclipse XSLT and java debuggers is a non starter).
So here you go: first the extension:
Then the test class (with the embedded XSL).
With these 2 classes, the output is
Again, as the other XSLT VIPs of this thread have pointed out, this solution will tie you up to Xalan Java version 2.6+... But the truth of the matter is that:
1. XSLT 1.0 is often useless without extensions.
2. each processor has its own way (javascript for MSXML, java for Xalan, declared entry points for Xalan-C...)