2015-10-05 36 views
1

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

考虑这是我的节点:

<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:[email protected] 
    }, 
    ProdDetails:{ 
       product-id:[email protected], 
       mode:[email protected], 
       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 ([email protected]) == "sellByUnit" , 
       (VOLUMEVal: $) when ([email protected]) == "VOLUME", 
       (UnitMeasureVal: $) when ([email protected]) == "UnitMeasure" 
      } 
     ) } 
    } 

的第一款产品节点没有收到一个机构。我尝试使用默认[],但它不工作。我怎样才能确保它总是收到一个身体?

+1

它不是第三个产品是空的吗?你能告诉我们你用来读取xml节点的代码吗? – StillLearnin

+0

我分享了我的示例代码..请看看 –

回答

0

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

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

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

+0

xml是正确的..一些节点可能会进入xml或不可能。所以我也必须为这些元素创建映射。产品节点在输入xml中重复。为了演示目的,我只添加了2个产品 –

0

我已经找到了处理,因为,我们可以使用过滤器跳过空节点空节点的一种方式,

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

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