Mulesoft 3 DataWeave-按任意长度分割字符串 [英] Mulesoft 3 DataWeave - split a string by an arbitrary length

查看:154
本文介绍了Mulesoft 3 DataWeave-按任意长度分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mule 3 DataWeave中,如何将长字符串按设置的长度分成多行?

In Mule 3 DataWeave, how do I split a long string into multiple lines by a set length?

例如,我有以下JSON输入:

For instance, I have the following JSON input:

{
   "id" : "123",
   "text" : "There is no strife, no prejudice, no national conflict in outer space as yet. Its hazards are hostile to us all. Its conquest deserves the best of all mankind, and its opportunity for peaceful cooperation many never come again."
}

输入系统只能接受40个字符或更少的字符串,因此输出XML需要符合以下要求:

The input system can only accept strings of 40 characters or less, so the output XML needs to conform as follows:

<data>
   <id>123</id>
   <text>
      <line>There is no strife, no prejudice, no nat</line>
      <line>ional conflict in outer space as yet. It</line>
      <line>s hazards are hostile to us all. Its con</line>
      <line>quest deserves the best of all mankind, </line>
      <line>and its opportunity for peaceful coopera</line>
      <line>tion many never come again.</line>
   </text>
</data>

我知道 splitBy 可以使用定界符,但是我想使用任意长度.

I know splitBy can use a delimiter, but I want to use an arbitrary length.

推荐答案

试试这个Tony:

%dw 1.0
%output application/xml
%var data = {
   "id" : "123",
   "text" : "There is no strife, no prejudice, no national conflict in outer space as yet. Its hazards are hostile to us all. Its conquest deserves the best of all mankind, and its opportunity for peaceful cooperation many never come again."
}
---
data: {
    id: data.id,
    text: data.text scan /.{1,40}/ reduce (
        (e,acc={}) -> acc ++ {text: e[0]}
    ) 
}

我不知道有什么方法可以在DW 1.0中以正则表达式动态注入范围值(我尚未检查,但我敢肯定在DW 2.0中是可以的).这样,我编写了DW 1.0函数,将等分地分割了:string 值.这是知道您应该能够动态设置大小的转换:

I don't know of any way to dynamically inject range values in a regular expression in DW 1.0 (I have yet to check but I bet it is possible in DW 2.0). As such I wrote a DW 1.0 function do break in equal parts a :string value. Here's the transformation that know you should be able to dynamically set the size:

%dw 1.0
%output application/xml
%var data = {
   "id" : "123",
   "text" : "There is no strife, no prejudice, no national conflict in outer space as yet. Its hazards are hostile to us all. Its conquest deserves the best of all mankind, and its opportunity for peaceful cooperation many never come again."
}

%function equalParts(str,sz)
    [str]
    when ((sizeOf str) < sz or sz <= 0) 
    otherwise
        using (
            partCoords = (0 to (floor (sizeOf str) / sz) - 1) map [$ * sz, $ * sz + sz - 1] 
        ) (
            
            partCoords + [partCoords[-1][-1]+1,-1] map 
                str[$[0] to $[1]]
        )

---
data: {
    id: data.id,
    text: equalParts(data.text,40) reduce (
        (e, acc={}) -> acc ++ {text: e}
    )
}

这篇关于Mulesoft 3 DataWeave-按任意长度分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆