# 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 Red Hat 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 13, “XPath variables” lists the variables that are accessible when using XPath.

Table 13. XPath variables

Namespace | Local Part | Type | Description
--- | --- | --- | ---
`http://camel.apache.org/xml/in/` | `in` | `Message` | The IN message
`http://camel.apache.org/xml/out/` | `out` | `Message` | The OUT message
`http://camel.apache.org/xml/variables/exchange-property` | ``property`` | `Object` | the Exchange property whose key is `property`
`http://camel.apache.org/xml/functions/` | `functions` | `Object` | Additional functions described in Table 14, “Aditional XPath functions”
`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

## Functions

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

Table 14. Aditional XPath functions

Function | Argument | Type | Description
--- | --- | --- | ---
`in:body` |  | `Object` | Returns the in message body.
`in:header` | `headerName` | `Object` | Return the specified in message header.
`out:body` |  | `Object` | Returns the out message body.
`out:header` | `headerName` | `Object` | Return the specified out message header.
`function:properties` | `propertyKey` | `String` | Looks up a property using the Properties component.
`function:simple` | `expression` | `Object` | Evaluates a Simple expression.

## Example

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

Example 21. 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>