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.
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>
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 builderfrom
message.in.header
if there is a header with the given keyfrom
exchange.properties
if there is a property with the given key
Table 16.1 lists the variables that are accessible when using XPath.
Table 16.1. 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 |
| Object | the Exchange property whose key is property |
http://camel.apache.org/xml/functions/ | functions | Object | Additional functions described in Table 16.2 |
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 |
Apache Camel adds the following XPath functions that can be used to access the exchange:
Table 16.2. 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 16.1 shows a route that uses XPath.
Example 16.1. 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>