Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

XPath

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.

To add an XPath expression to your routing rules its usually easiest to import the XPathBuilder classes 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")

Examples

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

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

Often the XML you are using has namespaces within it, so you can specify the namespaces to use in your XPath as follows in this example

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

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

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