4

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

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Ibsen. I declared e2d="abc.com/Abc11WS" in CamelContext tag but the xml file got error: cvc-complex-type.3.2.2: Attribute 'e2d' is not allowed to appear in element 'camelContext'.
You need to add a declaration to this namespace in the very top, eg there is some for Camel etc. You need the same for your namespace as well. eg its standard xml/namespace stuff.
so in the beans tag: <beans xmlns=".." xmlns:camel=".." xmlns:cxf=".." xmlns:xsi=".." xmlns:e2d="abc.com/Abc11WS" xsi:schemaLocation="... abc.com/Abc11WS file://c:/.../etc/entertainment/abc11ws_schema1.xsd">. But the error is still there. Sorry, i am not good in those xml/qualified name.
See the xpath documentation at Camel, the section titled Using XML configuration. Notice the foo namespace.
Thanks Ibsen. I did followed the document. The XML is fine now. But when i ran the application. It didn't work as i expect. It looks like the condition is always get false value. here is the log file that i got: "org.apache.camel.camel-core - 2.6.0 | #0 - XPath: /e2d:getOrderDetailRequest matches: false for: Exchange[Message: [Body is instance of java.io.InputStream]] Endpoint[file://c:/target] Exchange[Message: [Body is instance of java.io.InputStream]] Wrote [c:\target\other\xxxxxxxx] to [Endpoint[file://c:/target/other]]"
|

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.