简体   繁体   English

在Dataweave中处理NULL xml节点

[英]Handle NULL xml nodes in Dataweave

How can I access the body and/or avoid error of NULL values in xml nodes in mule data weave. 我如何访问主体和/或避免在m子数据编织中的xml节点中出现NULL值错误。

Consider this is my node: 考虑这是我的节点:

<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>

My Dataweave Code is : 我的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"
             } 
        ) }
   }

The first Product node doesn't receive a body. 第一个“产品”节点不接收正文。 I tried using default [] but it's not working. 我尝试使用默认[]但无法正常工作。 How can I make sure it always receives a body? 我如何确保它总是收到尸体?

Your XML example isn't complete and valid, missing and upc values etc. that you are using in your DataWeave script. 您的XML示例不完整,无效,缺少和upc值等。您在DataWeave脚本中使用的示例。

Your solution is probably something like checking the sizeOf the node, and if it's 0 fill it op with a whitespace, if not execute your normal script. 您的解决方案可能类似于检查节点的sizeOf,如果它为0,则如果不执行常规脚本,则用空格填充op。

If you have your complete examples that would help. 如果您有完整的示例,将会有所帮助。

I have found out one way to handle the empty nodes as, we can use filter to skip the empty nodes, 我发现了一种处理空节点的方法,因为我们可以使用过滤器跳过空节点,

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

Using this we control the empty or non empty tags into the transformation. 使用此控件,我们可以将空或非空标签控制到转换中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM