Mule 在数组中查找子字符串

Mule 在数组中查找子字符串,mule,dataweave,Mule,Dataweave,我想计算数组中字段中子字符串出现的次数 例如: 下面的XML在字段1中出现了3次子字符串“TXT1” <Level1> <Level2> <Field1>10000TXT1</Field1> <Field1>TXT210000</Field1> <Field1>10001TXT1</Field1> <Field1>TXT30000

我想计算数组中字段中子字符串出现的次数

例如: 下面的XML在字段1中出现了3次子字符串“TXT1”

  <Level1>
    <Level2>
      <Field1>10000TXT1</Field1>
      <Field1>TXT210000</Field1>
      <Field1>10001TXT1</Field1>
      <Field1>TXT30000</Field1>
      <Field1>10TXT1000</Field1>
      <Field1>TXT20000</Field1>
    </Level2>

fun countOccurences(txtToSearchFor) =
  // Some code that can count how many times the text 'TXT1' occur in all the Field1 fields.
(二)


希望您能提供帮助:-)

您好,您可以使用模块中的功能。此函数获取一个数组和一个lambda,该lambda返回数组中每个元素要添加的编号。然后我只需要求一个字符串在另一个字符串中的重复次数。这是通过使用和实现的

%dw 2.0
输出应用程序/json
从dw::core::Array导入sumBy
fun timesOf(值:数组,txtToSearchFor:String)=
值sumBy((text)->sizeOf(text find txttosearch for))
---
有效载荷.Level1.Level2.*字段1“TXT1”的时间

您好,您可以使用模块中的功能。此函数获取一个数组和一个lambda,该lambda返回数组中每个元素要添加的编号。然后我只需要求一个字符串在另一个字符串中的重复次数。这是通过使用和实现的

%dw 2.0
输出应用程序/json
从dw::core::Array导入sumBy
fun timesOf(值:数组,txtToSearchFor:String)=
值sumBy((text)->sizeOf(text find txttosearch for))
---
有效载荷.Level1.Level2.*字段1“TXT1”的时间
我找到了答案::-)

我找到了答案::-)

trim(upper(Field1)) contains "TXT1"
(((Field1) find 'TXT1') joinBy '')
%dw 2.0
output application/json
import sumBy from dw::core::Arrays

fun timesOf(value: Array<String>, txtToSearchFor: String) =
    value sumBy ((text) ->  sizeOf(text find  txtToSearchFor))
---
payload.Level1.Level2.*Field1 timesOf "TXT1"
fun countOccurences(texts) = 
    sizeOf (Level1.Level2.*Field1 filter ($ contains texts))