Dataweave 2.0 - 如何根据属性名称获取 XML 元素值

Dataweave 2.0 - How to obtain an XML elements value, based on it's attribute name

我正在尝试找出如何从具有相同名称但属性名称不同的元素的重复列表中提取 XML 元素值。

在这种情况下,您将如何在 DataWeave 2.0 中提取具有属性名称 'xxx.UDFCHAR10' 的元素的元素值 ('ABC')?

<root>
   <UserArea>
      <PropertyList>
         <Property>
            <NameValue name="xxx.CreatedBy">Test 1</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.EnteredBy">Test 2</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.SafetyFlag">false</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.DependFlag">true</NameValue>
         </Property>
         <Property>
            <NameValue name="xxx.UDFCHAR10">ABC</NameValue>
         </Property>
      </PropertyList>
   </UserArea>
</root>

谢谢

在包含它的元素上使用 multi-valued selector to get all the repeated instances into an array, then you can filter by the value of the attribute using the attribute selector。此方法将 return 提取值的数组,但是如果您知道只有一个元素,您可以按索引 [0].

提取它

请注意,您的描述并不准确,因为该属性位于重复元素的子元素中。

%dw 2.0
output application/java
---
(payload.root.UserArea.PropertyList.*Property 
    filter ($.NameValue.@name == "xxx.UDFCHAR10")).NameValue

输出:

[
  "ABC"
]

使用多重选择器获取所有 属性 个对象,然后通过选择 NameValue 创建一个 ArrayList。然后使用 firstWith 获取符合提供条件的第一个字符串

%dw 2.0
output application/json
import firstWith from dw::core::Arrays
var data = read("<root>
   <UserArea>
      <PropertyList>
         <Property>
            <NameValue name='xxx.CreatedBy'>Test 1</NameValue>
         </Property>
         <Property>
            <NameValue name='xxx.EnteredBy'>Test 2</NameValue>
         </Property>
         <Property>
            <NameValue name='xxx.SafetyFlag'>false</NameValue>
         </Property>
         <Property>
            <NameValue name='xxx.DependFlag'>true</NameValue>
         </Property>
         <Property>
            <NameValue name='xxx.UDFCHAR10'>ABC</NameValue>
         </Property>
      </PropertyList>
   </UserArea>
</root>","application/xml")
---
payload.root.UserArea.PropertyList.*Property.NameValue firstWith ((object) -> object.@name == "xxx.UDFCHAR10")

输出:

"ABC"