Can't use XPath api in xalan extension functions ------------------------------------------------
Key: XALANJ-2433 URL: https://issues.apache.org/jira/browse/XALANJ-2433 Project: XalanJ2 Issue Type: Bug Components: Xalan-extensions Affects Versions: The Latest Development Code Reporter: Dave Brosius If you use XPathFactory to create a new XPath inside an extension function, the xpath evaluation fails because the context node is a DTMNodeProxy, and the wrong XPathContext is referred to, and a NullPtrException occurs. Exception stack trace is > AxesWalker.setRoot(int) line: 221 > WalkingIterator.setRoot(int, Object) line: 157 > XNodeSet(NodeSequence).setRoot(int, Object) line: 265 > WalkingIterator(LocPathIterator).execute(XPathContext) line: 212 > XPath.execute(XPathContext, int, PrefixResolver) line: 337 > XPath.execute(XPathContext, Node, PrefixResolver) line: 303 > XPathImpl.eval(String, Object) line: 216 > XPathImpl.evaluate(String, Object, QName) line: 281 > ExtensionsWithXPath.exfunc(ExpressionContext) line: 28 because the dtm is null, HERE public void setRoot(int root) { // %OPT% Get this directly from the lpi. XPathContext xctxt = wi().getXPathContext(); m_dtm = xctxt.getDTM(root); m_traverser = m_dtm.getAxisTraverser(m_axis); /* HERE m_dtm is null */ m_isFresh = true; m_foundLast = false; m_root = root; m_currentNode = root; if (DTM.NULL == root) { throw new RuntimeException( XSLMessages.createXPATHMessage(XPATHErrorResources.ER_SETTING_WALKER_ROOT_TO_NULL, null)); //"\n !!!! Error! Setting the root of a walker to null!!!"); } resetProximityPositions(); } ------------------- sample code ----------------------------- public class ExtensionsWithXPath { public static void main(String[] args) throws Exception { System.setProperty("javax.xml.xpath.XPathFactory:" +XPathConstants.DOM_OBJECT_MODEL, "org.apache.xpath.jaxp.XPathFactoryImpl"); 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(); } } } -----------------------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> -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]