Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
The Latest Development Code
-
None
-
None
Description
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
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++)
return sb.toString();
} catch (Exception e)
}
}
----------------------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>