Jeet
Jeet

Reputation: 321

Mule 3.4.0 Choice router based on presence of a node in payload using Xpath

I have a couple of Soap inputs and would like to decide course of action in my Mule Flow based on existence of an Element in Payload.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xyz="http://xyz.abc.com/">
  <soapenv:Header/>
  <soapenv:Body>
    <xyz:myFirst/>
  </soapenv:Body>
</soapenv:Envelope>


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xyz="http://xyz.abc.com/">
  <soapenv:Header/>
  <soapenv:Body>
    <xyz:mySecond>
        Something here
    </xyz:mySecond>
  </soapenv:Body>
</soapenv:Envelope>

In the above payloads, if "myFirst" is present, I would like to go one route and if "mySecond" is present, another route.

I tried the following

<choice>
  <when expression="#[xpath('fn:count(//soapenv:Envelope/soapenv:Body/xyz:myFirst/child::text())') != 0]">
    //Do something First Here
  </when>
  <when expression="#[xpath('fn:count(//soapenv:Envelope/soapenv:Body/xyz:mySecond/child::text())') != 0]">
    //Do something Second Here
  </when>
  <otherwise>
    //Didn't match any!
  </otherwise>
</choice>

But "myFirst" is never identified since it is empty. I tried what is mentioned at this location Using XPath to Check if Soap Envelope Contains Child Node but to no avail. My current attempt is based on How to tell using XPath if an element is present and non empty? .

Thoughts?

Upvotes: 1

Views: 2334

Answers (1)

Charu Khurana
Charu Khurana

Reputation: 4551

In your XPath you are testing on child text node, that's why it never goes on first choice element if you have an empty node in sample payload. If the intent is to perform a task even if the node is empty, do this:

<choice>
  <when expression="#[xpath('fn:count(//soapenv:Envelope/soapenv:Body/xyz:myFirst)') != 0]">
    //Do something First Here
  </when>
  <when expression="#[xpath('fn:count(//soapenv:Envelope/soapenv:Body/xyz:mySecond') != 0]">
    //Do something Second Here
  </when>
  <otherwise>
    //Didn't match any!
  </otherwise>
</choice>

Upvotes: 1

Related Questions