* @return The sum of the evaluation value on each node
*/
public static double sum(ExpressionContext myContext, NodeList nl, String expr)
throws SAXNotSupportedException
{
XPathContext xctxt = null;
if (myContext instanceof XPathContext.XPathExpressionContext)
xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
else
throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext }));
if (expr == null || expr.length() == 0)
return Double.NaN;
NodeSetDTM contextNodes = new NodeSetDTM(nl, xctxt);
xctxt.pushContextNodeList(contextNodes);
double sum = 0;
for (int i = 0; i < nl.getLength(); i++)
{
int contextNode = contextNodes.item(i);
xctxt.pushCurrentNode(contextNode);
double result = 0;
try
{
XPath dynamicXPath = new XPath(expr, xctxt.getSAXLocator(),
xctxt.getNamespaceContext(),
XPath.SELECT);
result = dynamicXPath.execute(xctxt, contextNode, xctxt.getNamespaceContext()).num();
}
catch (TransformerException e)
{
xctxt.popCurrentNode();
xctxt.popContextNodeList();
return Double.NaN;
}
xctxt.popCurrentNode();
sum = sum + result;
}
xctxt.popContextNodeList();
return sum;
}