book
Article ID: KB0086445
calendar_today
Updated On:
Description
Resolution:
Description:
============
When the xpath expression evaluate is used, it returns an empty nodelist when the same works fine (returns nodelist) when it was executed outside of BW.
Environment:
===========
BW 5.9.3
Symptoms:
========
The following Java code fails to return nodelist for valid XML input data. It will behave the same even if the code is converted into a Java method activity.
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(false);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();;
Document inDoc = docBuilder.parse(new ByteArrayInputStream(inXML.getBytes()));;
Document outDoc = docBuilder.newDocument();
rootElement = outDoc.createElement("root");
outDoc.appendChild(rootElement);
System.out.println("Root Node="+inDoc.getDocumentElement().getNodeName());
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new SingleNamespaceContext("mx", "http://www.ibm.com/maximo"));
NodeList nodes = (NodeList) xpath.evaluate(documentNode, inDoc, XPathConstants.NODESET);
System.out.println("******its in-a"+nodes.getLength()+".."+documentNode);
System.out.println("**Length ** = "+nodes.getLength());
if the nodelist length is not checked, it will throw the following exception
java.lang.IndexOutOfBoundsException
Cause:
=====
The issue is caused by net.sf.saxon.xpath.XPathFactoryImpl becoming the default XPath Factory since BW 5.7.1. From BW 5.7.1 onwards, the JAR named "saxon9-xpath.jar" includes a new implementation of the XPathFactory class and it uses a "META-INF/services" entry to force the use of the "net.sf.saxon.xpath.XPathFactoryImpl" implementation class.
The problem is that the saxon9-B package is not "schema-aware." This means that XML data types (from schemas) in the XML instance will not be detected. It will fail to find any nodes.
Resolution:
==========
1). Change the XML default namespace to a prefixed one.
2). Change the Java code as follows:
System.setProperty("javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom", "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(false);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();;
Document inDoc = docBuilder.parse(new ByteArrayInputStream(inXML.getBytes()));;
Document outDoc = docBuilder.newDocument();
rootElement = outDoc.createElement("root");
outDoc.appendChild(rootElement);
System.out.println("Root Node="+inDoc.getDocumentElement().getNodeName());
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new SingleNamespaceContext("mx", "http://www.ibm.com/maximo"));
NodeList nodes = (NodeList) xpath.evaluate(documentNode, inDoc, XPathConstants.NODESET);
System.out.println("******its in-a"+nodes.getLength()+".."+documentNode);
System.out.println("**Length ** = "+nodes.getLength());
Issue/Introduction
Issue in Java code Activity for Xpath expression.