且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在camel-context.xml中使用xpath检查特定节点是否存在

更新时间:2023-11-25 23:44:04

在使用xpath并且xml具有命名空间(例如SOAP消息)时,则还必须在xpath表达式中使用命名空间.

骆驼文档中有一些详细信息: http://camel.apache.org/xpath

通过在Camel中使用XML DSL,您可以直接在XML标签中声明名称空间,例如在camelContext等中.

<camelContext xmlns="http://camel.apache.org/schema/spring" e2d="http://www.abc.com/Abc11WS">
   ...
   <when>
     <xpath>/e2d:getOrderDetailRequest</xpath>
       ...

但是请注意,XPath可能很难正常工作.这就是为什么我们在Camel 2.10中添加了logNamespaces的原因.请参阅此页面底部的详细信息: http://camel.apache.org/xpath >

I am trying to develop a content-based routing camel application. This application will look at the folder src/data to see if there is a SOAP request file that has node <e2d:getOrderDetaiRequest>, then that file will be copy into target/message, otherwise the file will be copy to target/other.

Do you know how to use xpath(or any other tools ) to check that condition (i prefer using camel-context.xml file)?

Here is my camel-context

<route>
        <from uri="file://c:/src/data?noop=true"/>
        <choice>
           <when>
            <xpath>**???????????????????????????**</xpath>
                <to uri="file://c:/target/message"/>
           </when>
           <otherwise>
            <to uri="file://c:/target/other"/>
           </otherwise>
        </choice>
    </route>

And here is the sample of 2 different SOAP requests

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:e2d="http://www.abc.com/Abc11WS">
   <soapenv:Header/>
   <soapenv:Body>
      <e2d:getOrderDetailRequest>
         <actionCode>1234</actionCode>
      </..>
   </...></...>

And

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lsr="http://www.abc.com/Abc22WS">
   <soapenv:Header/>
   <soapenv:Body>
      <lsr:getProductDetailsRequest>
           <productId>12345</...>
     </...></...></...>

When using xpath and your xml has namespaces, such as your SOAP message, then you must use namespaces in the xpath expression as well.

There is some details at the Camel docmentation at: http://camel.apache.org/xpath

By using the XML DSL in Camel you can declare the namespace in the XML tag directly such as in the camelContext etc.

<camelContext xmlns="http://camel.apache.org/schema/spring" e2d="http://www.abc.com/Abc11WS">
   ...
   <when>
     <xpath>/e2d:getOrderDetailRequest</xpath>
       ...

But mind that XPaths can be a bit tricky to get working correctly. And that is why we added the logNamespaces in Camel 2.10. See details in the bottom of the this page: http://camel.apache.org/xpath