Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8285229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:25:04+00:00 2026-06-08T11:25:04+00:00

I’m doing a XSL transformation on a input XML then I need to extract

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:25:05+00:00Added an answer on June 8, 2026 at 11:25 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.