如何在DataWeave中有条件地填充一个XML标记?

huangapple go评论98阅读模式
英文:

How to populate an xml tag conditionally in DataWeave?

问题

<typ:Product xmlns:typ="http://xmlns.oracle.com/apps/prc/po/editDocument/order/types/">
<typ:Entry>
<pur:OrderId xmlns:pur="http://xmlns.oracle.com/apps/prc/po/editDocument/order/">247</pur:OrderId>
<pur:OrderDescription xmlns:pur="http://xmlns.oracle.com/apps/prc/po/editDocument/order/">apple</pur:OrderDescription>
<pur:EntryLine xmlns:pur="http://xmlns.oracle.com/apps/prc/po/editDocument/order/">
<pur:productNumber/>
<pur:Qty/>
<pur:unit>11.34</pur:unit>
<pur:status>Changed</pur:status>
<pur:Mode>CHANGE</pur:Mode>
<pur:Reason/>
<pur:Schedule>
<pur:StatusNumber>1</pur:StatusNumber>
<pur:RequestDate>2023-02-22</pur:RequestDate>
<pur:Action>CHANGE</pur:Action>
</pur:Schedule>
</pur:EntryLine>
<pur:EntryLine xmlns:pur="http://xmlns.oracle.com/apps/prc/po/editDocument/order/">
<pur:productNumber/>
<pur:Qty/>
<pur:unit>11.34</pur:unit>
<pur:status>Changed</pur:status>
<pur:Mode>CHANGE</pur:Mode>
<pur:Reason/>
<pur:Schedule>
<pur:StatusNumber>1</pur:StatusNumber>
<pur:RequestDate>2023-02-25</pur:RequestDate>
<pur:Action>CHANGE</pur:Action>
</pur:Schedule>
</pur:EntryLine>
<pur:EntryLine xmlns:pur="http://xmlns.oracle.com/apps/prc/po/editDocument/order/">
<pur:productNumber/>
<pur:Qty/>
<pur:unit>11.34</pur:unit>
<pur:status/>
<pur:Mode>CANCEL</pur:Mode>
<pur:Reason>Cancelled</pur:Reason>
</pur:EntryLine>
</typ:Entry>
</typ:Product>

英文:

I want to iterate over this below tag:

      &lt;pur:Schedule&gt;
        &lt;pur:StatusNumber&gt;1&lt;/pur:StatusNumber&gt;
        &lt;pur:RequestDate&gt;2023-02-22&lt;/pur:RequestDate&gt;
        &lt;pur:Action&gt;CHANGE&lt;/pur:Action&gt;
      &lt;/pur:Schedule&gt;

I want to populate the above tag when the Mode is equal to CHANGE else if the Mode is equal to CANCEL skip the entire tag.

Input Payload

{&quot;Order&quot;: {
    &quot;Reason&quot;: &quot;apple&quot;,
&quot;Line&quot;: [
    {
&quot;Quantity&quot;: 30,
&quot;Date&quot;: &quot;2023-02-22&quot;,&quot;Pricing&quot;: {
    &quot;Amount&quot;: &quot;11.34&quot;},&quot;Details&quot;: {
        &quot;Number&quot;: &quot;247&quot;}},
         {
&quot;Quantity&quot;: 12,
&quot;Date&quot;: &quot;2023-02-25&quot;,&quot;Pricing&quot;: {
    &quot;Amount&quot;: &quot;11.34&quot;},&quot;Details&quot;: {
        &quot;Number&quot;: &quot;247&quot;}},
         {
&quot;Quantity&quot;: 0,
&quot;Date&quot;: &quot;2023-02-21&quot;,&quot;Pricing&quot;: {
    &quot;Amount&quot;: &quot;11.34&quot;},&quot;Details&quot;: {
        &quot;Number&quot;: &quot;247&quot;}}
        
        ]}}

Used Dataweave Script

%dw 2.0
output text/xml writeDeclaration=false
ns typ http://xmlns.oracle.com/apps/prc/po/editDocument/order/types/
ns pur http://xmlns.oracle.com/apps/prc/po/editDocument/order/
---
{typ#Product: {
    typ#Entry: {
        pur#OrderId: payload.Order.Line.Details.Number[0],
pur#OrderDescription: payload.Order.Reason,pur#EntryLine: payload.Order.Line map {
    pur#productNumber: $.Details.Line_Number,
pur#Qty: $.Ordered_Quantity,
pur#unit: $.Pricing.Amount ,
pur#status: if($.Quantity != 0) &quot;Changed&quot; else &quot;&quot;,
pur#Mode: if($.Quantity == 0) &quot;CANCEL&quot; else &quot;CHANGE&quot;,pur#Reason: if($.Quantity == 0) &quot;Cancelled&quot; else &quot;&quot;,
(pur#Schedule: {
    pur#StatusNumber: &quot;1&quot;,
    pur#RequestDate: $.Date,
    pur#Action: &quot;CHANGE&quot;})
    }}}}

Expected Output

&lt;typ:Product xmlns:typ=&quot;http://xmlns.oracle.com/apps/prc/po/editDocument/order/types/&quot;&gt;
  &lt;typ:Entry&gt;
    &lt;pur:OrderId xmlns:pur=&quot;http://xmlns.oracle.com/apps/prc/po/editDocument/order/&quot;&gt;247&lt;/pur:OrderId&gt;
    &lt;pur:OrderDescription xmlns:pur=&quot;http://xmlns.oracle.com/apps/prc/po/editDocument/order/&quot;&gt;apple&lt;/pur:OrderDescription&gt;
    &lt;pur:EntryLine xmlns:pur=&quot;http://xmlns.oracle.com/apps/prc/po/editDocument/order/&quot;&gt;
      &lt;pur:productNumber/&gt;
      &lt;pur:Qty/&gt;
      &lt;pur:unit&gt;11.34&lt;/pur:unit&gt;
      &lt;pur:status&gt;Changed&lt;/pur:status&gt;
      &lt;pur:Mode&gt;CHANGE&lt;/pur:Mode&gt;
      &lt;pur:Reason/&gt;
      &lt;pur:Schedule&gt;
        &lt;pur:StatusNumber&gt;1&lt;/pur:StatusNumber&gt;
        &lt;pur:RequestDate&gt;2023-02-22&lt;/pur:RequestDate&gt;
        &lt;pur:Action&gt;CHANGE&lt;/pur:Action&gt;
      &lt;/pur:Schedule&gt;
    &lt;/pur:EntryLine&gt;
    &lt;pur:EntryLine xmlns:pur=&quot;http://xmlns.oracle.com/apps/prc/po/editDocument/order/&quot;&gt;
      &lt;pur:productNumber/&gt;
      &lt;pur:Qty/&gt;
      &lt;pur:unit&gt;11.34&lt;/pur:unit&gt;
      &lt;pur:status&gt;Changed&lt;/pur:status&gt;
      &lt;pur:Mode&gt;CHANGE&lt;/pur:Mode&gt;
      &lt;pur:Reason/&gt;
      &lt;pur:Schedule&gt;
        &lt;pur:StatusNumber&gt;1&lt;/pur:StatusNumber&gt;
        &lt;pur:RequestDate&gt;2023-02-25&lt;/pur:RequestDate&gt;
        &lt;pur:Action&gt;CHANGE&lt;/pur:Action&gt;
      &lt;/pur:Schedule&gt;
    &lt;/pur:EntryLine&gt;
    &lt;pur:EntryLine xmlns:pur=&quot;http://xmlns.oracle.com/apps/prc/po/editDocument/order/&quot;&gt;
      &lt;pur:productNumber/&gt;
      &lt;pur:Qty/&gt;
      &lt;pur:unit&gt;11.34&lt;/pur:unit&gt;
      &lt;pur:status/&gt;
      &lt;pur:Mode&gt;CANCEL&lt;/pur:Mode&gt;
      &lt;pur:Reason&gt;Cancelled&lt;/pur:Reason&gt;
    &lt;/pur:EntryLine&gt;
  &lt;/typ:Entry&gt;
&lt;/typ:Product&gt;

答案1

得分: 2

你可以在生成该密钥部分的脚本中添加条件。请注意,脚本的输出不能用作脚本的一部分。避免以这种方式表达问题。相反,考虑输入如何生成输出。我重复使用生成元素Mode的条件。

(
  pur#Schedule: {
    pur#StatusNumber: &quot;1&quot;,
    pur#RequestDate: $.Date,
    pur#Action: &quot;CHANGE&quot;
  }
) if ($.Quantity != 0)
英文:

You can add a conditional to the section of the script that generates that key. Note that the output of the script can not be used as part of the script. Avoid that way of expressing the problem. Instead think on terms of how the input generates the output. I reused the condition that generates the element Mode.

        (
          pur#Schedule: {
            pur#StatusNumber: &quot;1&quot;,
            pur#RequestDate: $.Date,
            pur#Action: &quot;CHANGE&quot;
          }
        ) if ($.Quantity != 0)

huangapple
  • 本文由 发表于 2023年2月24日 06:35:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定