Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

How do you declare a namespace for proper XPath use within EvaluateXPath

avatar
Rising Star

I have made some progress understanding and using EvaluateXPath.

I've been able, for example, to generate a flow file with the content

<?xml version="1.0" encoding="UTF-8"?><bookstore>  <book>    <title lang="en">Harry Potter</title>    <author>J K. Rowling</author>    <year>2005</year>    <price>29.99</price>  </book></bookstore>

and match this using the following (notice //year as the XPath)

91559-xpathbug3.png

However if I have a namespace in the XML

91560-xpathbug2.png

, then I have a problem (notice //ns8:textNOTAM as the XPath)

91561-xpathbug1.png

1 ACCEPTED SOLUTION

avatar
New Contributor
hide-solution

This problem has been solved!

Want to get a detailed solution you have to login/registered on the community

Register/Login
8 REPLIES 8

avatar
Rising Star

Oh No!! It looks like there has been an issue outstanding for over 2 years...

Is there no way for me to use EvaluateXPath on XML that contains namespaces?

https://community.hortonworks.com/questions/45869/nifi-xml-namespace.html

https://issues.apache.org/jira/browse/NIFI-1023

avatar
Rising Star

Omg.. that worked!!

The XPath I had to use (within the EvaluateXPath processor configuration) was as follows:

//*[local-name()='textNOTAM']

This is stuff that should live in a NIFI user manual someplace.

avatar
Rising Star

avatar
Rising Star

Also an interesting stackoverflow link

https://stackoverflow.com/questions/6390339/how-to-query-xml-using-namespaces-in-java-with-xpath

it seems that maybe there is a workaround that i can use with evaluatexpath it might look something like the following

/*[local-name()='workbook']/*[local-name()='sheets']/*[local-name()='sheet'][1]

avatar
Rising Star

I've found several interesting stack overflow articles.. including the following:

https://stackoverflow.com/questions/3931817/xpath-expression-from-xml-with-namespace-prefix

I should be able to match the book in the following with a simple //bk:book

<bk:bookstore xmlns:bk="urn:xmlns:25hoursaday-com:bookstore">
 <bk:book> 
    <bk:title>Lord of the Rings</bk:title> 
    <bk:author>J.R.R. Tolkien</bk:author>
    <inv:inventory status="in-stock" isbn="0345340426" 
        xmlns:inv="urn:xmlns:25hoursaday-com:inventory-tracking" />
 </bk:book> 
</bk:bookstore>

Somehow NIFI needs to let me register the namespace.

I've also tried out online xpath formatters, and they have allowed me to simply add the namespace to the xpath as follows

https://www.freeformatter.com/xpath-tester.html#ad-output

matches //bk:book

91562-xpathbug4.png

avatar
New Contributor
hide-solution

This problem has been solved!

Want to get a detailed solution you have to login/registered on the community

Register/Login

avatar
Rising Star

Thanks @Nico Verwer. I'll give that a shot, and will be sure to accept your response as an answer, once I verify.

avatar
New Contributor

@nverwer wrote:

Instead of using local-name, you can also do this:

//*:textNOTAM

This has a wildcard for the namespace prefix, and the element name in the place where you would expect it.

This tends to be more readable for longer XPaths like

//*:feed//*:entry//*:title

This info was hard to find. @nverwer thanks. Saved my day!