[CONF] Apache Camel: XPath (page edited)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[CONF] Apache Camel: XPath (page edited)

Dhiraj Bokde (Confluence)

XPath has been edited by Martin Gilday (Sep 24, 2008).

(View changes)



Camel supports XPath to allow an Expression or Predicate to be used in the DSL or Xml Configuration. For example you could use XPath to create an Predicate in a Message Filter or as an Expression for a Recipient List.

From 1.3 of Camel onwards you can use XPath expressions directly using smart completion in your IDE as follows:


Notice: The xpath is outside the filter node; after the ( ) braces.

In earlier versions of Camel you had to use the XPathBuilder methods then you can use the xpath() function inside your rules.

import static org.apache.camel.builder.xml.XPathBuilder.*;
import static org.apache.camel.builder.xml.XPathBuilder.*;

Notice: The xpath is inside the filter node; within the ( ) braces.


In 1.3 onwards you can easily use namespaces with XPath expressions using the Namespaces helper class.

Namespaces ns = new Namespaces("c", "http://acme.com/cheese");

    xpath("/c:person[@name='James']", ns).


Variables in XPath is defined in different namespaces. The default namespace is http://activemq.apache.org/camel/schema/spring.

Namespace URI Local part Type Description
http://camel.apache.org/xml/in/ in Message the exchange.in message
http://camel.apache.org/xml/out/ out Message the exchange.out message
http://camel.apache.org/xml/variables/environment-variables env Object OS environment variables
http://camel.apache.org/xml/variables/system-properties system Object Java System properties
http://camel.apache.org/xml/variables/exchange-property   Object the exchange property

Camel will resolve variables according to either:

  • namespace given
  • no namespace given

Namespace given

If the namespace is given then Camel is instructed exactly what to return. However when resolving either in or out Camel will try to resolve a header with the given local part first, and return it. If the local part has the value body then the body is returned instead.

No namespace given

If there is no namespace given then Camel resolves only based on the local part. Camel will try to resolve a variable in the following steps:

  • from variables that has been set using the variable(name, value) fluent builder
  • from message.in.header if there is a header with the given key
  • from exchange.properties if there is a property with the given key


Camel adds the following XPath functions that can be used to access the exchange:

Function Argument Type Description
$body none Object The message body. Will return the in message body if using no/default or the in namespace. Will return the out body if using the out namespace.
$header the key Object The message header. Will return the in message header if using no/default or the in namespace. Will return the out message if using the out namespace.
$out-body none Object The message.out.body
$out-header the key Object The message.out.header

Using XML configuration

If you prefer to configure your routes in your Spring XML file then you can use XPath expressions as follows

<beans xmlns="http://www.springframework.org/schema/beans"
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">

  <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
      <from uri="activemq:MyQueue"/>
        <to uri="mqseries:SomeOtherQueue"/>

Notice how we can reuse the namespace prefixes, foo in this case, in the XPath expression for easier namespace based XPath expressions!


Here is a simple example using an XPath expression as a predicate in a Message Filter


If you have a standard set of namespaces you wish to work with and wish to share them across many different XPath expressions you can use the NamespaceBuilder as shown in this example

// lets define the namespaces we'll need in our filters
Namespaces ns = new Namespaces("c", "http://acme.com/cheese")
        .add("xsd", "http://www.w3.org/2001/XMLSchema");

// now lets create an xpath based Message Filter

In this sample we have a choice construct. The first choice evaulates if the message has a header key type that has the value Camel.
The 2nd choice evaluates if the message body has a name tag <name> which values is Kong.
If neither is true the message is routed in the otherwise block:

    // using $headerName is special notation in Camel to get the header key
    .when().xpath("$type = 'Camel'")
    // here we test for the body name tag
    .when().xpath("//name = 'Kong'")

And the spring XML equivalent of the route:

<camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
        <from uri="direct:in"/>
                <xpath>$type = 'Camel'</xpath>
                <to uri="mock:camel"/>
                <xpath>//name = 'Kong'</xpath>
                <to uri="mock:donkey"/>
                <to uri="mock:other"/>

XPath injection

You can use Bean Integration to invoke a method on a bean and use various languages such as XPath to extract a value from the message and bind it to a method parameter.

The default XPath annotation has SOAP and XML namespaces available. If you want to use your own namespace URIs in an XPath expression you can use your own copy of the XPath annotation to create whatever namespace prefixes you want to use.

i.e. cut and paste the XPath annotation code to your own project in a different package and/or annotation name then add whatever namespace prefix/uris you want in scope when you use your annotation on a method parameter. Then when you use your annotation on a method parameter all the namespaces you want will be available for use in your XPath expression.

For example

public class Foo {
    @MessageDriven(uri = "activemq:my.queue")
    public void doSomething(@Path("/foo/bar/text()") String correlationID, @Body String body) {
		// process the inbound message here


The XPath language is part of camel-core.