You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Dave Brosius <db...@mebigfatguy.com> on 2008/02/09 00:15:18 UTC

XFormers in extension functions 'Could not resolve the node to a handle'

Greetings.

   I am trying to use xpath in an extension function, and it is failing with message 
'Could not resolve the node to a handle'

Any ideas what is wrong? Here is the shortest example i could come up with
----
xsl
----
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
							 xmlns:xalan="http://xml.apache.org/xalan"
							 xmlns:exxp="xalan://ExtensionsWithXPath"
							 extension-element-prefixes="exxp">
	
	<xsl:template match="/sample">
		<xsl:value-of select="exxp:exfunc()"/>
	</xsl:template>
</xsl:transform>
----
xml
----
<sample>
	<test>
		<target value="42"/>
	</test>
	<test>
		<target value="43"/>
	</test>
</sample>
----
code
----
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.xalan.extensions.ExpressionContext;
import org.w3c.dom.Attr;
import org.w3c.dom.NodeList;


public class ExtensionsWithXPath
{
    public static void main(String[] args) throws Exception {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer(new StreamSource(ExtensionsWithXPath.class.getResourceAsStream("/exxp.xsl")));
        t.transform(new StreamSource(ExtensionsWithXPath.class.getResourceAsStream("/exxp.xml")), new StreamResult(System.out));
    }
    
    public String exfunc(ExpressionContext context) {
        try {
            XPathFactory xpath = XPathFactory.newInstance();
            XPath xp = xpath.newXPath();
            NodeList nodes = (NodeList)xp.evaluate("target/@value", context.getContextNode(), XPathConstants.NODESET);
            StringBuilder sb = new StringBuilder();
            int len = nodes.getLength();
            for (int i = 0; i < len; i++) {
                sb.append(((Attr)nodes.item(i)).getValue());
            }
            
            return sb.toString();
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}