1
votes

I'm doing a XSL transformation on a input XML then I need to extract some values using XPath on the resulting document. But may XPath expression always return null when using the XSL result Node. But If I store the XSL resulting document in a file then reload it. the XPath expression returns the corresponding Node.

Here is my code (utility fonctions have been removed for lisibility) :

public class XmlTest {
@Test
public void testWithNativeJavaApi() throws Exception {
    InputStream instream = resolveClasspathFile("xslt/xslt-test-transform-2.xsl");
    StreamSource xsltSource = new StreamSource(instream);
    DOMSource domSource = loadXmlFromClasspathFile("xslt/xslt-test-input-2.xml");
    prettyPrint(domSource.getNode());

    Transformer transformer = TransformerFactory.newInstance().newTransformer(xsltSource);
    DOMResult domResult = new DOMResult();
    transformer.transform(domSource, domResult);
    Node node = domResult.getNode();

    // Store then reload the file
    // Uncommenting those 3 lines will make the test pass
    // File xslOutputfile = new File("target", "xsl-ouput.xml");
    // prettyPrint(node, new FileOutputStream(xslOutputfile));
    // node = loadXmlFromInputStream(new FileInputStream(xslOutputfile)).getNode();

    XPath xPathProcessor = XPathFactory.newInstance().newXPath();
    XPathExpression xpathExpression = xPathProcessor.compile("/Message/Out/Personne/CodeCivilite");
    System.out.println();
    Node resultNode = (Node) xpathExpression.evaluate(node, XPathConstants.NODE);

    if (resultNode != null) {
        System.out.println(resultNode.getNodeName() + "=" + resultNode.getTextContent());
    } else {
        System.out.println("Node is null");
    }

    assertNotNull("XPath expression returned null node", resultNode);
    assertEquals("CodeCivilite", resultNode.getNodeName());
    assertEquals("M.", resultNode.getTextContent());

}
}

Juste comment or remove the 3 lines below "// Store then reload the file" and the test won't pass anymore.

I'm completely stuck, any help is welcome.

3

3 Answers

1
votes

DocumentBuilderFactory does not use namespace by default unless dbf.setNamespaceAware(true) is used. On the other hand, XSL and TransformerFactory plently use namespace so the resulting document of the transformation is qualified with namespace.

So the XPathExpression should use namespaces when being called next to a XSL transformation.

XPath xPathProcessor = XPathFactory.newInstance().newXPath();
xPathProcessor.setNamespaceContext(new NamespaceContext() {
  @Override
  public String getNamespaceURI(String prefix) {
    if(prefix.equals("t")) {
      return "http://www.example.org/test";
    } else {
      return null;  
    }
  }
  @Override public String getPrefix(String namespaceURI) {
    return null; // not used
  }
  @Override
  public Iterator<?> getPrefixes(String namespaceURI) {
    return null; // not used
  }
});
XPathExpression xpathExpression = xPathProcessor.compile("/t:Message/t:Out/t:Personne/t:CodeCivilite");
System.out.println();
Node resultNode = (Node) xpathExpression.evaluate(node, XPathConstants.NODE);

Introducing a temporary file, reloaded with a default DocumentBuilderFactory (not aware of namespaces) make the non-qualified XPATH expression (with no namespace) work.

Hope this was clear :)

0
votes

I wonder if parsing the file is giving you a shorter tree. Try using the xpath expression

//Message/Out/Personne/CodeCivilite

(double slash in front) without the file i/o to see if there's an extra layer of nodes without the file i/o.

0
votes

node = loadXmlFromInputStream(new FileInputStream(xslOutputfile)).getDocumentElement();

is it getDocumentElement()?

Can you print the reloaded node and see if you have what you are expecting?