To evaluate an XPath expression in the XML DSL, put the XPath expression inside an
xpath
element. The XPath expression is applied to the body of the
current In message and returns an XML node (or node set).
Typically, the returned XML node is automatically converted to a string.
For example, to extract the contents of the /person/name
element from
the current In message body and use it to set a header named
user
, you could define a route like the following:
<beans ...> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="queue:foo"/> <setHeader headerName="user"> <xpath>/person/name/text()</xpath> </setHeader> <to uri="direct:tie"/> </route> </camelContext> </beans>
If you want to convert the result to a specific type, specify the result type by
setting the resultType
attribute to a Java type name (where you must
specify the fully-qualified type name). For example, to specify explicitly that the
result type is java.lang.String
(you can omit the
java.lang.
prefix here):
<xpath resultType="String">/person/name/text()</xpath>
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. It is possible to
use the standard XML mechanism for associating prefixes with namespace URIs. That
is, you can set an attribute like this:
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 ...>
<camelContext xmlns="http://camel.apache.org/schema/spring"
xmlns:cust="http://acme.com/customer/record" >
<route>
<from uri="queue:foo"/>
<setHeader headerName="user">
<xpath>/cust:person/cust:name/text()</xpath>
</setHeader>
<to uri="direct:tie"/>
</route>
</camelContext>
</beans>