XPath

Overview

XPath expressions implicitly acts on the message content and returns a node set as its result. Depending on the context, the return value is interpreted either as a predicate (where an empty node set is interpreted as false) or as an expression.

Namespaces

When processing documents whose elements belong to one or more XML schemas, it is typically necessary to associate namespace URIs with prefixes, so that you can identify element names unambiguously in your XPath expressions. Because Spring DSL is itself written in XML, it is possible to use the standard XML mechanism for associating prefixes with namespace URIs. That is, you can set an attribute as follows:

xmlns:prefix="namespaceURI"

For example, to associate the prefix, cust, with the namespace, http://acme.com/customer/record, and then extract the contents of the element, /cust:person/cust:name, you could define a route like the following:

<beans ...
  xmlns:cust="http://acme.com/customer/record"
  ...>

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
      <from uri="queue:foo"/>
      <setHeader headerName="user">
        <xpath>/cust:person/cust:name/text()</xpath>
      </setHeader>
      <to uri="direct:tie"/>
    </route>
  </camelContext>
  ...
</beans>

Namespace resolution

When Fuse IDE encounters an element that is in a namespace that is not explicitly declared it uses Apache Camel's default namespace resolution scheme. Apache 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

Variables

Table 14, “XPath variables” lists the variables that are accessible when using XPath.

Table 14. XPath variables

NamespaceLocal PartTypeDescription
http://camel.apache.org/xml/in/inMessageThe IN message
http://camel.apache.org/xml/out/outMessageThe OUT message
http://camel.apache.org/xml/variables/exchange-propertypropertyObjectthe Exchange property whose key is property
http://camel.apache.org/xml/functions/functionsObjectAdditional functions described in Table 15, “Aditional XPath functions”
http://camel.apache.org/xml/variables/environment-variablesenvObjectOS environment variables
http://camel.apache.org/xml/variables/system-propertiessystemObjectJava System properties

Functions

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

Table 15. Aditional XPath functions

FunctionArgumentTypeDescription
in:body ObjectReturns the in message body.
in:headerheaderNameObjectReturn the specified in message header.
out:body ObjectReturns the out message body.
out:headerheaderNameObjectReturn the specified out message header.
function:propertiespropertyKeyStringLooks up a property using the Properties component.
function:simpleexpressionObjectEvaluates a Simple expression.

Example

Example 23, “Route using XPath” shows a route that uses XPath.

Example 23. Route using XPath

<camelContext>
  <route>
    <from uri="direct:in"/>
    <choice>
      <when>
        <language langauge="xpath">$type = 'Camel'</language>
        <to uri="mock:camel"/>
      </when>
      <when>
        <language langauge="xpath">//name = 'Kong'</language>
        <to uri="mock:donkey"/>
      </when>
      <otherwise>
        <to uri="mock:other"/>
      </otherwise>
    </choice>
  </route>
</camelContext>