0
votes

I see the below error while I write the data weave expression for the below payload. How to fix this(All XML format)?

XML file:

<Interactions
    xmlns="urn:astrazeneca:na:Activity:domain:3"
    xmlns:ns0="urn:astrazeneca:na:Activity:domain:3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
SchemaVersion="3.1">
    <ns0:Interaction>
        <InteractionDetails CreatedOnDate="2020-01-07T00:40:38"
RecordCompanyCode="AZN"
RestrictionGroup="NONE"
UpdatedOnDate="2020-01-07T00:40:39">
            <StartDate>2020-01-07T00:40:18</StartDate>
            <EndDate>2020-01-07T00:40:18</EndDate>
            <Location xsi:type="LocationAddress">
                <AddressLine LineNo="1">6089 N 1ST ST STE 102</AddressLine>
                <CityName>FRESNO</CityName>
            </Location>
            <RelatedInteraction>
                <RelationshipType>is_a_child_of</RelationshipType>
            </RelatedInteraction>
        </InteractionDetails>
    </ns0:Interaction>
</Interactions>

Expresion:

%dw 2.0
output application/xml
---
"Interactions" : payload.Interactions mapObject {
  "Interaction" : $ mapObject {
    "InteractionDetails" : $ mapObject {
      "Location" :  $ - "AddressLine"
    }
  }
}

Error:

You called the function '-' with these arguments: 
1: String ("2020-01-07T00:40:18")
2: String ("AddressLine")
1
You're misunderstanding the purpose of mapOjbect. I would suggest you read the docs: docs.mulesoft.com/mule-runtime/4.3/dw-core-functions-mapobject - Michael Jones
What's the expected output for the input provided? - aled

1 Answers

0
votes

Assuming that you want to remove all occurrences of a given element, you could base your final solution on the following DataWeave experssion:

%dw 2.0
fun removeElements(element,elements) =
  element mapObject (value, key) -> {
    ((key): 
        if (value is Object) 
            removeElements(
                if (elements is Array) value -- elements //if elements is an array
                else value -- (elements splitBy ','), elements) //if elements is a string with comma separated elements
        else value)
  }
output application/xml
---
// All three options will be accepted by the removeElements function
removeElements(payload, "AddressLine")
//removeElements(payload, ["AddressLine", "CityName"])
//removeElements(payload, "AddressLine,CityName")

The following input:

<Interactions
    xmlns="urn:astrazeneca:na:Activity:domain:3"
    xmlns:ns0="urn:astrazeneca:na:Activity:domain:3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
SchemaVersion="3.1">
    <ns0:Interaction>
        <InteractionDetails CreatedOnDate="2020-01-07T00:40:38"
RecordCompanyCode="AZN"
RestrictionGroup="NONE"
UpdatedOnDate="2020-01-07T00:40:39">
            <StartDate>2020-01-07T00:40:18</StartDate>
            <EndDate>2020-01-07T00:40:18</EndDate>
            <Location xsi:type="LocationAddress">
                <AddressLine LineNo="1">6089 N 1ST ST STE 102</AddressLine>
                <CityName>FRESNO</CityName>
            </Location>
            <RelatedInteraction>
                <RelationshipType>is_a_child_of</RelationshipType>
            </RelatedInteraction>
        </InteractionDetails>
    </ns0:Interaction>
</Interactions>

will result in the following output:

<?xml version='1.0' encoding='UTF-8'?>
<Interactions xmlns="urn:astrazeneca:na:Activity:domain:3" SchemaVersion="3.1">
  <ns0:Interaction xmlns:ns0="urn:astrazeneca:na:Activity:domain:3">
    <ns0:InteractionDetails CreatedOnDate="2020-01-07T00:40:38" RecordCompanyCode="AZN" RestrictionGroup="NONE" UpdatedOnDate="2020-01-07T00:40:39">
      <ns0:StartDate>2020-01-07T00:40:18</ns0:StartDate>
      <ns0:EndDate>2020-01-07T00:40:18</ns0:EndDate>
      <ns0:Location xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="LocationAddress">
        <ns0:CityName>FRESNO</ns0:CityName>
      </ns0:Location>
      <ns0:RelatedInteraction>
        <ns0:RelationshipType>is_a_child_of</ns0:RelationshipType>
      </ns0:RelatedInteraction>
    </ns0:InteractionDetails>
  </ns0:Interaction>
</Interactions>