NiFi EvaluateXPath处理器未返回所有结果。

huangapple go评论72阅读模式
英文:

NiFi EvaluateXPath Processor doesn't return all results

问题

我试图从示例XML中提取ID列表,并最好将它们写入流文件属性。

XML:

<Data>
<Buckets>
<Data_Item>
<Data_Info ID="1234">
<Value>123</Value>
</Data_Info>
</Data_Item>
<Data_Item>
<Data_Info ID="5678">
<Value>456</Value>
</Data_Info>
</Data_Item>
<Data_Item>
<Data_Info ID="9999">
<Value>789</Value>
</Data_Info>
</Data_Item>
</Buckets>
</Data>


XPath:
`/Data/Buckets/Data_Item/Data_Info/@ID`

我在在线编辑器中测试了XPath,并返回了所有3个ID,但当我将其输入到NiFi EvaluateXPath处理器中时,它要么抛出错误,要么只返回第一个ID。

如果我将返回类型设置为`nodeset`或`auto-detect`,就会发生错误。
错误消息:`XPath evaluation on ... produced unexpected results [3]`

如果我将返回类型设置为`string`,我只会得到第一个ID(1234)作为返回值。

如果我将目的地从`flowfile-attribute`更改为`flowfile-content`,也会出现相同的问题。

如何从XML中提取ID列表?
英文:

I'm trying to extract a list of IDs from an example XML, and preferably write them into a flowfile attribute.

XML:

&lt;Data&gt;
  &lt;Buckets&gt;
    &lt;Data_Item&gt;
      &lt;Data_Info ID=&quot;1234&quot;&gt;
        &lt;Value&gt;123&lt;/Value&gt;
      &lt;/Data_Info&gt;
    &lt;/Data_Item&gt;
    &lt;Data_Item&gt;
      &lt;Data_Info ID=&quot;5678&quot;&gt;
        &lt;Value&gt;456&lt;/Value&gt;
      &lt;/Data_Info&gt;
    &lt;/Data_Item&gt;
    &lt;Data_Item&gt;
      &lt;Data_Info ID=&quot;9999&quot;&gt;
        &lt;Value&gt;789&lt;/Value&gt;
      &lt;/Data_Info&gt;
    &lt;/Data_Item&gt;
  &lt;/Buckets&gt;
&lt;/Data&gt;

XPath:
/Data/Buckets/Data_Item/Data_Info/@ID

I tested the XPath with an online editor and it returned all 3 IDs, but when I enter it into the NiFi EvaluateXPath processor it either throws an error or only returns the first ID.

The error happens if I use nodeset or auto-detect for the return type.
Error Message: XPath evaluation on ... produced unexpected results [3]

If I use string as the return type I only get the first ID (1234) as a return value.

The same problems occur if I change the destination from flowfile-attribute to flowfile-content.

How can I extract a list of IDs from the XML?

答案1

得分: 1

1234,5678,9999

英文:

Use a this XPath Expression (using string-join):

string-join(/Data/Buckets/Data_Item/Data_Info/@ID, &#39;,&#39;)

input

&lt;Data&gt;
  &lt;Buckets&gt;
    &lt;Data_Item&gt;
      &lt;Data_Info ID=&quot;1234&quot;&gt;
        &lt;Value&gt;123&lt;/Value&gt;
      &lt;/Data_Info&gt;
    &lt;/Data_Item&gt;
    &lt;Data_Item&gt;
      &lt;Data_Info ID=&quot;5678&quot;&gt;
        &lt;Value&gt;456&lt;/Value&gt;
      &lt;/Data_Info&gt;
    &lt;/Data_Item&gt;
    &lt;Data_Item&gt;
      &lt;Data_Info ID=&quot;9999&quot;&gt;
        &lt;Value&gt;789&lt;/Value&gt;
      &lt;/Data_Info&gt;
    &lt;/Data_Item&gt;
  &lt;/Buckets&gt;
&lt;/Data&gt;

output

1234,5678,9999

huangapple
  • 本文由 发表于 2023年6月29日 19:46:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定