Created on 09-26-2018 05:52 PM - edited 08-17-2019 10:12 PM
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)
However if I have a namespace in the XML
, then I have a problem (notice //ns8:textNOTAM as the XPath)
Created 07-16-2019 12:56 PM
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
Created 09-26-2018 06:13 PM
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
Created 09-26-2018 06:43 PM
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.
Created 09-28-2018 03:19 AM
Since I've gotten past this, but i'd like others to understand it as well, i've dug a tiny bit more to understand this "local-name" magic.
Several links of interest
https://examples.javacodegeeks.com/core-java/xml/xpath/xpath-local-name-example/
https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/local-name
https://docs.oracle.com/cd/E35413_01/doc.722/e35419/dev_xpath_functions.htm#autoId6
http://www.codesimplify.com/java/java-xpath-ignore-namespace-example/
Enjoy
Created 09-26-2018 09:52 PM
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]
Created on 09-28-2018 03:11 AM - edited 08-17-2019 10:12 PM
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
Created 07-16-2019 12:56 PM
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
Created 07-16-2019 04:23 PM
Thanks @Nico Verwer. I'll give that a shot, and will be sure to accept your response as an answer, once I verify.
Created on 09-30-2022 02:35 PM - edited 09-30-2022 02:35 PM
@nverwer wrote:Instead of using local-name, you can also do this:
//*:textNOTAMThis 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!