Support Questions

Find answers, ask questions, and share your expertise

How to use EvaluateXPath to get xml root's attribute value in NiFi?

avatar
New Contributor

xpath.pngMy NiFi Flow is:

GenerateFlowFile --> UpdateAttribute --> InvokeHTTP --> EvaluateXPath --> LogAttribute

My xml data returned by InvokeHTTP Is :

<?xml version="1.0" ?> <queryResponse type="HCC" rootUrl="https://xyz/data" requestUrl="https://xyz/data/HCC" responseType="listEntityInstances" count="253561" first="0" last="999"> <entity url="https://xyz/data/HCC/790189306" type="HCC" dtoType="hccDTO"> <hccDTO id="790189306" displayName="790189306"> <count>0</count> </hccDTO> </entity> </queryResponse>

I want to get "last" attribute's value from EvaluateXPath. Expecting value "999" to be returned but I am getting entire xml in output.

I am attaching the processor details. Kindly let me know where I am going wrong.

3 REPLIES 3

avatar
Master Guru

Try the following for the XPath:

string(/queryResponse/@last)

Also ensure the Destination property is "flowfile-content" and Return is "string", this will ensure the value of the attribute is written as the contents of the outgoing flow file.

15244-evaluatexpath-settings.png

avatar
Expert Contributor

When I try this, I'm getting an empty string back. Can someone explain why? I'm using NiFi 1.5.0.

My XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Log>
    <Transaction Type="1" TrainingModeFlag="true">
 <StoreID>240041</StoreID>
 ...
    </Transaction>
</Log>

My Workflow splits the XML via SplitXML in depth 1, so after this processor I have this sub-xml:

<Transaction Type="1" TrainingModeFlag="true">
    <StoreID>240041</StoreID>
    ...
</Transaction>

I want to extract the value of the Type attribute of the Transaction tag, but it doesn't work for me. Here my EvaluateXPath processor:

71428-1.png

My new content attribute has an empty string set, instead of showing the value 1:

71429-2.png

avatar
Expert Contributor

Reading the Value with the XPath //@Type works fine.