1

I am working with MuleSoft DataWeave 2.0 to process an XML payload. I need to extract the second <wd:ID> element where the attribute wd:type equals "Learning_Course_ID".

Here is a sample XML input:

<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data xmlns:wd="urn:com.test.report/test">
    <wd:Report_Entry>
        <wd:Worker_group>
            <wd:user_email>[email protected]</wd:user_email>
        </wd:Worker_group>
        <wd:course_id wd:Descriptor="Preventing bribery, fraud and money laundering">
            <wd:ID wd:type="WID">dummysdfg</wd:ID>
            <wd:ID wd:type="Learning_Course_ID">MANUAL_COURSE-1-16</wd:ID>
            <wd:ID wd:type="Learning_Course">Preventing bribery, fraud and money laundering</wd:ID>
        </wd:course_id>
    </wd:Report_Entry>
</wd:Report_Data>

I tried the following DataWeave script:

%dw 2.0
output application/json
ns wd urn:com.test.report/test

---

{
  secondLearningCourseID: 
    payload.wd#Report_Data.wd#Report_Entry.wd#course_id."wd:ID" 
      filter ((item) -> item.@"type" == "Learning_Course_ID")[1] 
}

But getting { "secondLearningCourseID": null }

1 Answer 1

1

First the sub expression "wd:ID" is wrong. wd is a namespace like in the previous parts: wd#ID. Then you need to use the multi-value selector to return an array of all the values that match with that key, since there are several: *wd#ID. After applying the filter you need to use parenthesis around the whole expression to be able to use the index selector on the result. Finally if the filtering expression returns only one value you need to use 0 as the index for the first element.

(payload.wd#Report_Data.wd#Report_Entry.wd#course_id.*wd#ID filter ((item) -> item.@"type" == "Learning_Course_ID"))[0]

Output:

"MANUAL_COURSE-1-16"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.