java-xmlbuilder icon indicating copy to clipboard operation
java-xmlbuilder copied to clipboard

xpathFind method is not working as expected.

Open arjunlodhe opened this issue 1 year ago • 1 comments

Hi Team,

I upgraded java-xmlbuilder library from v0.4 to 0.6 or 1.2 and I start seeing failures related to "XPathExpressionException", Please see below the complete error message:

javax.xml.xpath.XPathExpressionException: XPath expressi on "TABLE_INFO/field[tag='aggregate-count']" does not resolve to an Element in context [aggregate-information: null]: null at com.jamesmurty.utils.BaseXMLBuilder.xpathFindImpl(BaseXMLBuilder.java:505) at com.jamesmurty.utils.XMLBuilder.xpathFind(XMLBuilder.java:372) at com.jamesmurty.utils.XMLBuilder.xpathFind(XMLBuilder.java:378)

If I revert back to 0.4 it starts working, could someone please help me with this?

Regards, Arjun

arjunlodhe avatar Aug 01 '24 13:08 arjunlodhe

Hi @arjunlodhe I expect the problem you're experiencing is due to the improved namespace support that was added in version 0.5:

Namespace support when building document, and when performing xpath queries

Since versions after 0.4 are aware of namespaces and their prefix names, you must either:

  • include the prefix in your XPath queries (e.g. xpathFind("//my-ns-prefix:TABLE_INFO")) or if you only have a single default namespace you can leave out its name (e.g. xpathFind("//:TABLE_INFO"))
  • create with an XMLBuilder/2 object that has namespaces disabled via the isNamespaceAware optional param, e.g. XMLBuilder.parse(xmlFile, false, false) (with enableExternalEntities and isNamespaceAware boolean flags)

See this section of the unit tests for examples of how to handle namespaces in XPath queries: https://github.com/jmurty/java-xmlbuilder/blob/master/src/test/java/com/jamesmurty/utils/BaseXMLBuilderTests.java#L403-L485

jmurty avatar Sep 01 '24 15:09 jmurty