用于在Xml中检索属性的Mule Dataweave脚本

用于在Xml中检索属性的Mule Dataweave脚本,mule,mule-component,dataweave,Mule,Mule Component,Dataweave,我的输入Xml如下所示 <?xml version="1.0" encoding="UTF-8"?> <elements> <element> <id>123</id> <items> <item> <attributes> <attribute attribute-id="test1">attr1</attribute>

我的输入Xml如下所示

<?xml version="1.0" encoding="UTF-8"?>
<elements>
<element>
  <id>123</id>
  <items>
     <item>
        <attributes>
        <attribute attribute-id="test1">attr1</attribute>
         <attribute attribute-id="test2">attr2</attribute>                      
        </attributes>
     </item>
  </items>
</element>
</elements>
预期产出:

<?xml version='1.0' encoding='windows-1252'?>
<elements>
  <element>
    <test1>attr1</test1>
    <test2>attr2</test2>
  </element>
</elements>

属性1
属性2
实际产量:

<?xml version='1.0' encoding='windows-1252'?>
<elements>
  <element>
    <test1>attr1</test1>
    <test2></test2>
  </element>
</elements>

属性1
我的data weave脚本需要做哪些更改才能获得预期的输出?

试试这个

%dw 1.0
%output application/xml
---
{
    elements: {  (payload.elements.*element map {
        element: {($.items.item.attributes.*attribute map {
            ($.@attribute-id) : $
        })}
        }
     )
    }
由于存在多个项,您的项无效。项。属性。属性

希望这有帮助。

试试这个

%dw 1.0
%output application/xml
---
{
    elements: {  (payload.elements.*element map {
        element: {($.items.item.attributes.*attribute map {
            ($.@attribute-id) : $
        })}
        }
     )
    }
由于存在多个项,您的项无效。项。属性。属性

希望这有帮助。

检查下面的脚本

%dw 1.0
%output application/xml
---
{
    elements: {
        (payload.elements.*element map {
            element: {
                ($.items.item.attributes.*attribute map ((element, index) -> {
                    (element.@attribute-id) : element when element.@attribute-id == "test1" or element.@attribute-id == "test2"
                    otherwise ''
                }))
            }
        }
     )
    }
}
它检查属性id是否为“test1和test2”,如果不同,则将值设置为空。

检查下面的脚本

%dw 1.0
%output application/xml
---
{
    elements: {
        (payload.elements.*element map {
            element: {
                ($.items.item.attributes.*attribute map ((element, index) -> {
                    (element.@attribute-id) : element when element.@attribute-id == "test1" or element.@attribute-id == "test2"
                    otherwise ''
                }))
            }
        }
     )
    }
}

如果不同,它会检查“test1和test2”的
属性id
,将值设置为空。

谢谢!如何获取下面的输出属性1 attr2使用上述代码中的
($):$
而不是
($.@attribute id):$
。希望这有帮助。谢谢。我想我举了一个不好的例子。我的输出应该如下attr1 attr2..第一个应该是studentName,第二个应该是address。我该怎么做?当$.@attribute id==“test1”时使用此
(studentName:$),当$.@attribute id==“test2”
时使用此
(address:$)而不是
($.@attribute id):$
。希望这对汉克斯有帮助!如何获取下面的输出属性1 attr2使用上述代码中的
($):$
而不是
($.@attribute id):$
。希望这有帮助。谢谢。我想我举了一个不好的例子。我的输出应该如下attr1 attr2..第一个应该是studentName,第二个应该是address。我该怎么做?当$.@attribute id==“test1”时使用此
(studentName:$),当$.@attribute id==“test2”
时使用此
(address:$)而不是
($.@attribute id):$
。希望这有帮助