【问题标题】:Dataweave 2.0 - How to obtain an XML elements value, based on it's attribute nameDataweave 2.0 - 如何根据属性名称获取 XML 元素值
【发布时间】:2021-12-03 21:13:58
【问题描述】:

我正在尝试研究如何从具有相同名称但属性名称不同的元素的重复列表中提取 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>

谢谢

【问题讨论】:

    标签: xml dataweave mule4


    【解决方案1】:

    使用multi-valued selector 将所有重复的实例放入一个数组中,然后您可以使用具有它的元素上的attribute selector 按属性的值进行过滤。此方法将返回一个包含提取值的数组,但是如果您知道只有一个元素,您可以通过索引 [0] 提取它。

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

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

    输出:

    [
      "ABC"
    ]
    

    【讨论】:

      【解决方案2】:

      使用多重选择器获取所有属性对象,然后通过选择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"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        • 2021-03-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多