简体   繁体   中英

Mulesoft DataWeave 2.0 - conditionally change a single nested value

A status in XML needs to change before it gets forwarded. If RESPONSE.OUTBOUND.STATUS is equal to "ERR", it needs to say "FAILURE" instead. Other messages that STATUS may contain must remain as is.

Sample XML before processing:

<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE>
   <ID>9497585</ID>
   <DATE>2020-10-01</DATE>
   <TIME>18:38:04</TIME>
   <OUTBOUND>
      <CODE>921</CODE>
      <STATUS>ERR</STATUS>
      <DESC>Manufacturing flaw</DESC>
   </OUTBOUND>
   <ORIGIN>
      <METHOD>POST</METHOD>
      <STATUS>200 OK</STATUS>
      <CLIENTID>29834</CLIENTID>
      <DIAG>330</DIAG>
      <NOTES>XRAY revealed air pockets.</NOTES>
   </ORIGIN>
</RESPONSE>

DataWeave:

%dw 2.0
output application/xml
---
payload 
   - "RESPONSE" ++
   { "RESPONSE" : 
        (payload.RESPONSE - "OUTBOUND") ++
        { "OUTBOUND" : 
            (payload.RESPONSE.OUTBOUND - "STATUS") ++ 
            {"STATUS" : 
               if (payload.RESPONSE.OUTBOUND.STATUS == "ERR") 
                  "FAILURE"  
               else 
                  payload.RESPONSE.OUTBOUND.STATUS
            } 
        }
   } 

This is the output:

<?xml version='1.0' encoding='UTF-8'?>
<RESPONSE>
  <ID>9497585</ID>
  <DATE>2020-10-01</DATE>
  <TIME>18:38:04</TIME>
  <ORIGIN>
    <METHOD>POST</METHOD>
    <STATUS>OK</STATUS>
    <CLIENTID>29834</CLIENTID>
    <DIAG>330</DIAG>
    <NOTES>XRAY revealed air pockets.</NOTES>
  </ORIGIN>
  <OUTBOUND>
    <CODE>921</CODE>
    <DESC>Manufacturing flaw</DESC>
    <STATUS>FAILURE</STATUS>
  </OUTBOUND>
</RESPONSE>

This works in a way, but to change a single value, it seems unnecessarily complex. It's like using a sledgehammer because I couldn't find a scalpel. Is there a simpler way to get to the node and change it?

Also, XML is node-order dependent. By removing and then reinserting the sub-node, I end up changing the sequence of the nodes ("OUTBOUND" now comes after "ORIGIN"). This will probably create grief down the road.

The simplest way to do this is to use the update operator

%dw 2.0
output application/xml
---
payload update {
    case status at .RESPONSE.OUTBOUND.STATUS if(status == "ERR") ->  "FAILURE"
}

Simple and clear;)

Using the update function, Mariano gave me a different approach to solve this as well.

The function expects a path to the element described in an array format.

Its raining answers for this question:).

%dw 2.0
import update from dw::util::Values
output application/xml
---
payload  update ["RESPONSE", "OUTBOUND", "STATUS"] with (value) -> if(value == "ERR") "Failure" else value 

This should help.

%dw 2.0
output application/xml
fun replaceElementValue(value:Any, nametoReplace: String, newValue: Any) = do {
    value match {
        case obj is Object -> obj mapObject ((value, key, index) -> 
            if(key ~= nametoReplace and value == "ERR")
                (key): newValue
            else    
                (key) : replaceElementValue(value, nametoReplace, newValue)
        )
        else -> value
    }
}   
---
replaceElementValue(payload,"STATUS","FAILRUE")

Slightly modified the script mentioned here to help achieve what you are looking for.

在此处输入图像描述

Extension of your attempt. Preserves ordering of the nodes Try with this script.

%dw 2.0
output application/xml
var outbound = payload.RESPONSE - "ORIGIN"
var origin  = payload.RESPONSE - "OUTBOUND" - "ID" - "DATE" - "TIME"
---
{
 a: outbound - "OUTBOUND" ++ { "OUTBOUND" : 
            (payload.RESPONSE.OUTBOUND - "STATUS") ++ 
            {"STATUS" : 
               if (payload.RESPONSE.OUTBOUND.STATUS == "ERR") 
                  "FAILURE"  
               else 
                  payload.RESPONSE.OUTBOUND.STATUS
            } 
        } ++
        origin
}

If you don't expect any other tag to have the ERR value, I would use one of the following two functions:

%dw 2.0
output application/xml

var data = read(
'<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE>
   <ID>9497585</ID>
   <DATE>2020-10-01</DATE>
   <TIME>18:38:04</TIME>
   <OUTBOUND>
      <CODE>921</CODE>
      <STATUS>ERR</STATUS>
      <DESC>Manufacturing flaw</DESC>
   </OUTBOUND>
   <ORIGIN>
      <METHOD>POST</METHOD>
      <STATUS>200 OK</STATUS>
      <CLIENTID>29834</CLIENTID>
      <DIAG>330</DIAG>
      <NOTES>XRAY revealed air pockets.</NOTES>
   </ORIGIN>
</RESPONSE>',
"application/xml"
)

// Traverse the XML and replace the String ERR with FAILURE
fun traverse(o: Object) = o mapObject (
    if ($$ ~= "OUTBOUND") {($$): traverse($)} else {($$):$}
)
fun traverse(s: String) = (
    if (s == "ERR") "FAILURE" else s
)

// Decouple the replacement from the traversal.  This is a more
// flexible solution
fun traverseFn(o: Object,fn, tag: String = "OUTBOUND") = o mapObject (
    if ($$ ~= tag) {($$): ($ traverseFn fn)} else {($$): $}
)
fun traverseFn(s: String, fn) = fn(s)

---
//traverse(data)

data traverseFn (
    (s) -> s match {
        case "ERR" -> "FAILURE"
        else -> $
    }
)

Try with this -

Using update function - documented here

%dw 2.0
import * from dw::util::Values
output application/xml
var resp = payload.RESPONSE - "OUTBOUND" - "ORIGIN"
var outbound = if(payload.RESPONSE.OUTBOUND.STATUS == "ERR") (payload.RESPONSE.OUTBOUND update "STATUS" with "FAILURE") else (payload.RESPONSE.OUTBOUND)
var origin  = payload.RESPONSE - "OUTBOUND" - "ID" - "DATE" - "TIME"
---
{
    RESPONSE: resp ++ {OUTBOUND: outbound}  ++ origin
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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