0

Sample

<?xml version="1.0" encoding="UTF-8"?>
<Root>
<ns2:bookStore xmlns:ns2="http://bookstore.com/schemes">
    <ns2:book id="1">
        <ns2:name>Data Structure</ns2:name>
    </ns2:book>
    <ns2:book id="2">
        <ns2:name>Java Core</ns2:name>
    </ns2:book>
</ns2:bookStore>
</Root>

Java code

XPathExpression expr = 
xpath.compile("//Root/ns2:bookStore/ns2:book/ns2:name/text()");

I get Prefix must resolve to a namespace: ns2 error with above xml with Root

tag. By removing the Root tag I'm able to parse the data. Any help would be appreciated without modifying the XML file.

Dhan Raj
  • 29
  • 7
  • 1
    Duplicate of [How does XPath deal with XML namespaces?](https://stackoverflow.com/questions/40796231/how-does-xpath-deal-with-xml-namespaces) (You have to call `xpath.setNamespaceContext()`. ) – kjhughes Dec 04 '17 at 12:44
  • I do call xpath.setNamespaceContext(new NamespaceResolver(doc)); with source document for the lookup sourceDocument.lookupNamespaceURI(prefix) – Dhan Raj Dec 04 '17 at 12:49
  • Please [edit] your question and provide a full [mcve]. Omitting key code relating to namespace prefix resolution will impede our ability to help you. Thanks. – kjhughes Dec 04 '17 at 13:11

0 Answers0