我试图通过分组product-id
来提取primary = true时的category-id%input payload application/xml
%output application/java
---
using (c=payload.catalog.*category-assignment default [])
((c default []) filter ($.@mode != "delete") groupBy $.@product-id map ({
(($ default []) filter ($.primary == 'true') map ({
field3:$.@category-id
}) when $.primary != null otherwise field3:"" ),
cat-id: $.@category-id joinBy "||" ,
primary-flag:$.primary
// (($ default []) filter ($.primary matches /true/) map ({
// //ur code here when equals to shipping charges
// field3:$.@category-id
// }) when $.primary != null otherwise [] ) ,
}))
我尝试了多种组合和过滤器。过滤器通常使用xml属性,但这里给出异常
cast to com.mulesoft.weave.model.structure.ObjectSeq (java.lang.ClassCastException). Message payload is of type: ReceiverFileInputStream
示例输入 -
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://www.example.com/xml/impex/catalog/2006-10-31">
<category-assignment product-id="D711069" category-id="4160">
<primary>true</primary>
</category-assignment>
<category-assignment product-id="D711069" category-id="DANIEL_4160"/>
</catalog>
这里有什么建议吗?
我尝试了另一组转换仅用于演示,而不是工作 -
%input payload application/xml
%output application/xml
---
Test:{((payload.catalog.*category-assignment default []) groupBy $.@product-id pluck {
product-id:$$,
cat-id: $.@category-id joinBy "||" ,
primary-flag:$[0].primary,
field3:$.@category-id[?($.primary == "true")]
})}
答案 0 :(得分:0)
试试这个表达式。你能详细说明你的预期产量吗?
using (c=payload.catalog.*category-assignment default [])
(c[?(($.primary == "true") and ($.@mode != "delete"))] groupBy $.@product-id map {
field3:$.@category-id,
primary-flag:$.primary
})
根据您的评论更新了dataweave:
%dw 1.0
%output application/csv
---
payload.catalog.*category-assignment[?(($.primary == "true") and ($.@mode != "delete"))] groupBy $.@product-id map {
productid:$.@product-id[0],
categoryid: arrayToString($.@category-id, sizeOf $.@category-id)
添加全局配置元素,如下所示:
<configuration doc:name="Configuration">
<expression-language>
<global-functions>
def arrayToString(arrayObj,length){
String r=null;
if(arrayObj==null){
return "null";
}else if(arrayObj==""){
return "Empty";
}else{
for (String s:arrayObj) {
if(r != null)
r = r + '||' + s;
else {
r = s;
}
}
return r;
}
}
</global-functions>
</expression-language>
</configuration>
答案 1 :(得分:0)
尝试使用此表达式,它将根据您声明的要求生成CSV。
%dw 1.0
%output application/csv header=true
---
using (ca = payload.catalog.*category-assignment)
(
payload.catalog map (catVal, idx) -> ({
(
ca groupBy $.@product-id pluck {
product-id:$$,
cat-id: $.@category-id joinBy "||"
,primary-flag:$.primary[0] default "false"
,(field3:$.@category-id[0]) when ($.primary[0] contains "true")
}
)
}) distinctBy $
)
它产生的输出为:
product-id,cat-id,primary,field3
D711069,DANIEL_4160||4160||DANIEL_4160,true,DANIEL_4160
使用的输入:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<category-assignment product-id="D711069" category-id="DANIEL_4160"/>
<category-assignment product-id="D711069" category-id="4160">
<primary>true</primary>
</category-assignment>
<category-assignment product-id="D711069" category-id="DANIEL_4160"/>
</catalog>
HTH