Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixed the wrong sample for xpath and added a new sample for the $header syntax

...

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

Code Block
from("queue:foo").
  filter().xpath("//foo")).
  to("queue:bar")
Code Block

from("queue:foo").
  choice().xpath("//foo")).to("queue:bar").
  otherwise().to("queue:others");

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.

Code Block
import static org.apache.camel.builder.xpath.XPathBuilder.*;
...
from("queue:foo").filter(xpath("//foo")).to("queue:bar")
Code Block

import static org.apache.camel.builder.xpath.XPathBuilder.*;
...
from("queue:foo").choice(xpath("//foo")).to("queue:bar").
otherwise().to("queue:others");

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

Namespaces

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

...

Wiki Markup
{snippet:id=example|lang=java|url=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java}

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:

Wiki Markup
{snippet:id=e1|lang=java|url=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathHeaderTest.java}

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.

...