【问题标题】:Handle NULL xml nodes in Dataweave在 Dataweave 中处理 NULL xml 节点
【发布时间】:2019-08-27 19:46:51
【问题描述】:

如何访问主体和/或避免 mule 数据编织中 xml 节点中的 NULL 值错误。

认为这是我的节点:

<catalog>
    <product product-id="D158413" mode="delete"/>
    <product product-id="556204380">
    <ean>5014414203648</ean>
    <display-name>Double duvet cover</display-name>
    <long-description>Line</long-description>
    <online-flag>true</online-flag>
    <available-flag>true</available-flag>
    <searchable-flag>true</searchable-flag>
    <tax-class-id>default</tax-class-id>
    <brand>Linea</brand>
    <manufacturer-name>Linea</manufacturer-name>
    <custom-attributes>
        <custom-attribute attribute-id="Care Instructions">Machine</custom-attribute>
        <custom-attribute attribute-id="Colour">Pink</custom-attribute>
        <custom-attribute attribute-id="Finish">Plain</custom-attribute>
        <custom-attribute attribute-id="Guarantee">N/A</custom-attribute>
    </product>  
</catalog>

我的 Dataweave 代码是:

%dw 1.0
%input payload application/xml
%output application/java 
---
(payload.catalog.*product default []) map  {
    CatalogDetails:{
            CatalogId:payload.catalog.@catalog-id
    },
    ProdDetails:{
                product-id:$.@product-id,
                mode:$.@mode,
                ean:$.ean,
                upc:$.upc,
                min-order-quantity:$.min-order-quantity,    
                display-name:$.display-name,
                short-description:$.short-description
    },
CustValues: { (
            ($.custom-attributes.*custom-attribute default []) map {      
                (sellByUnitVal: $)  when ($.@attribute-id) == "sellByUnit" ,
                (VOLUMEVal: $) when ($.@attribute-id) == "VOLUME",
                (UnitMeasureVal: $) when ($.@attribute-id) == "UnitMeasure"
             } 
        ) }
   }

第一个 Product 节点没有收到正文。我尝试使用默认的[],但它不起作用。我怎样才能确保它总是收到一个身体?

【问题讨论】:

  • 不是第三个产品是null吗?你能告诉我们你用来读取 xml 节点的代码吗?
  • 我分享了我的示例代码.. 请看看

标签: mule mule-studio mule-component mule-el dataweave


【解决方案1】:

您在 DataWeave 脚本中使用的 XML 示例不完整且不有效,缺少和 upc 值等。

您的解决方案可能类似于检查节点的 sizeOf,如果它是 0,则用空格填充它,如果不执行您的正常脚本。

如果你有完整的例子会有所帮助。

【讨论】:

  • xml 是正确的.. 某些节点可能会进入传入的 xml,也可能不会。所以我还必须为这些元素创建映射。产品节点在输入 xml 中重复。出于演示目的,我只添加了 2 个产品
【解决方案2】:

我找到了一种处理空节点的方法,我们可以使用过滤器来跳过空节点,

(payload.catalog.*product default []) filter ($ !='') map { }

使用这个我们控制空或非空标签进入转换。

【讨论】:

    猜你喜欢
    • 2016-02-28
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 2020-05-19
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多