Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml Mulesoft 3 DataWeave-按任意长度拆分字符串_Xml_Split_Dataweave_Mulesoft - Fatal编程技术网

Xml Mulesoft 3 DataWeave-按任意长度拆分字符串

Xml Mulesoft 3 DataWeave-按任意长度拆分字符串,xml,split,dataweave,mulesoft,Xml,Split,Dataweave,Mulesoft,在Mule 3 DataWeave中,如何按设定的长度将长字符串拆分为多行 例如,我有以下JSON输入: { "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 dese

在Mule 3 DataWeave中,如何按设定的长度将长字符串拆分为多行

例如,我有以下JSON输入:

{
   "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需要符合以下要求:

<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>

123
没有冲突,没有偏见,没有纳特
迄今为止,外层空间的冲突仍在继续。信息技术
美国的危险对我们所有人都是敌对的。它的缺点
追求是全人类最好的追求,
以及和平合作的机会
许多人再也不会来了。
我知道
splitBy
可以使用分隔符,但我想使用任意长度。

试试这个:

%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]}
    ) 
}
我不知道有什么方法可以在DW1.0中的正则表达式中动态注入范围值(我还没有检查,但我打赌在DW2.0中是可能的)。因此,我编写了一个DW 1.0函数,将一个
:string
值分成相等的部分。以下是您应该能够动态设置大小的转换:

%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}
    )
}
%dw 1.0
%输出应用程序/xml
%风险值数据={
“id”:“123”,
“文本”:“外层空间尚未出现冲突、偏见和民族冲突。其危害对我们所有人都是敌对的。它的征服理应得到全人类的最佳回报,和平合作的机会从未再来。”
}
%函数相等部分(str、sz)
[str]
当((sizeOf str)
试试这个:

%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]}
    ) 
}
我不知道有什么方法可以在DW1.0中的正则表达式中动态注入范围值(我还没有检查,但我打赌在DW2.0中是可能的)。因此,我编写了一个DW 1.0函数,将一个
:string
值分成相等的部分。以下是您应该能够动态设置大小的转换:

%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}
    )
}
%dw 1.0
%输出应用程序/xml
%风险值数据={
“id”:“123”,
“文本”:“外层空间尚未出现冲突、偏见和民族冲突。其危害对我们所有人都是敌对的。它的征服理应得到全人类的最佳回报,和平合作的机会从未再来。”
}
%函数相等部分(str、sz)
[str]
当((sizeOf str)
看起来不错,谢谢!40是可配置的正则表达式,比如属性或dw常量吗?更新了答案@TonyLooks good,谢谢!40是可配置的正则表达式,比如属性或dw常量吗?更新了答案@Tony