本文介绍了使用Xpath基于有效负载中节点存在的Mule 3.4.0 Choice路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个Soap输入,并且想根据净荷中元素的存在来决定我的M子流的动作过程.

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>

在上述有效负载中,如果存在"myFirst",则我想走一条路线,如果存在"mySecond",则要走另一条路线.

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

我尝试了以下

<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>

但是由于"myFirst"为空,因此从未被标识.我尝试了在此位置提到的内容使用XPath进行检查如果Soap Envelope包含子节点,但无济于事.我目前的尝试基于如何使用XPath判断元素是否存在且为非空?.

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? .

有想法吗?

推荐答案

在您的XPath中,您正在child text node上进行测试,因此如果样本有效负载中有一个空节点,它就永远不会在首选元素上进行.如果目的是即使节点为空也要执行任务,请执行以下操作:

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>

这篇关于使用Xpath基于有效负载中节点存在的Mule 3.4.0 Choice路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:06