`

Apahe camel: XPath

阅读更多

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.

from("queue:foo").
  filter().xpath("//foo")).
  to("queue:bar")
from("queue:foo").
  choice().xpath("//foo")).to("queue:bar").
  otherwise().to("queue:others");

Namespaces

You can easily use namespaces with XPath expressions using the Namespaces helper class.

Namespaces ns = new Namespaces("c", "http://acme.com/cheese");

from("direct:start").filter().
        xpath("/c:person[@name='James']", ns).
        to("mock:result");

Variables

Variables in XPath is defined in different namespaces. The default namespace is http://camel.apache.org/schema/spring.

Namespace URI Local part Type Description
http://camel.apache.org/xml/in/ in Message the exchange.in message
http://camel.apache.org/xml/out/ out Message the exchange.out message
http://camel.apache.org/xml/function/ functions Object Camel 2.5: Additional 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
http://camel.apache.org/xml/variables/exchange-property   Object the exchange property

Camel will resolve variables according to either:

  • namespace given
  • no namespace given

Namespace given

If the namespace is given then Camel is instructed exactly what to return. However when resolving either in or out Camel will try to resolve a header with the given local part first, and return it. If the local part has the value body then the body is returned instead.

No namespace given

If there is no namespace given then Camel resolves only based on the local part. 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

Functions

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

Function Argument Type Description
in:body none Object Will return the in message body.
in:header the header name Object Will return the in message header.
out:body none Object Will return the out message body.
out:header the header name Object Will return the out message header.
function:properties key for property String Camel 2.5: To lookup a property using the Properties component (property placeholders).
function:simple simple expression Object Camel 2.5: To evaluate a Simple expression.

Notice: function:properties and function:simple is not supported when the return type is a NodeSet, such as when using with a Splitter EIP.

Here's an example showing some of these functions in use.

from("direct:start").choice()
  .when().xpath("in:header('foo') = 'bar'").to("mock:x")
  .when().xpath("in:body() = '<two/>'").to("mock:y")
  .otherwise().to("mock:z");

And the new functions introduced in Camel 2.5:

// setup properties component
PropertiesComponent properties = new PropertiesComponent();
properties.setLocation("classpath:org/apache/camel/builder/xml/myprop.properties");
context.addComponent("properties", properties);

// myprop.properties contains the following properties
// foo=Camel
// bar=Kong

from("direct:in").choice()
    // $type is a variable for the header with key type
    // here we use the properties function to lookup foo from the properties files
    // which at runtime will be evaluted to 'Camel'
    .when().xpath("$type = function:properties('foo')")
        .to("mock:camel")
    // here we use the simple language to evaluate the expression
    // which at runtime will be evaluated to 'Donkey Kong'
    .when().xpath("//name = function:simple('Donkey ${properties:bar}')")
        .to("mock:donkey")
    .otherwise()
        .to("mock:other")
    .end();

Using XML configuration

If you prefer to configure your routes in your Spring XML file then you can use XPath expressions as follows

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring" xmlns:foo="http://example.com/person">
    <route>
      <from uri="activemq:MyQueue"/>
      <filter>
        <xpath>/foo:person[@name='James']</xpath>
        <to uri="mqseries:SomeOtherQueue"/>
      </filter>
    </route>
  </camelContext>
</beans>

Notice how we can reuse the namespace prefixes, foo in this case, in the XPath expression for easier namespace based XPath expressions!

See also this discussion on the mailinglist about using your own namespaces with xpath

Setting result type

The XPath expression will return a result type using native XML objects such as org.w3c.dom.NodeList. But many times you want a result type to be a String. To do this you have to instruct the XPath which result type to use.

In Java DSL:

xpath("/foo:person/@id", String.class)

In Spring DSL you use the resultType attribute to provide a fully qualified classname:

<xpath resultType="java.lang.String">/foo:person/@id</xpath>

In @XPath:
Available as of Camel 2.1

@XPath(value = "concat('foo-',//order/name/)", resultType = String.class) String name)

Where we use the xpath function concat to prefix the order name with foo-. In this case we have to specify that we want a String as result type so the concat function works.

Using XPath on Headers

Available as of Camel 2.11

Some users may have XML stored in a header. To apply an XPath to a header's value you can do this by defining the 'headerName' attribute.

In XML DSL:

<camelContext id="xpathHeaderNameTest" xmlns="http://camel.apache.org/schema/blueprint">
  <route>
    <from uri="direct:in"/>
    <choice>
      <when>
        <!-- use headerName attribute to refer to a header -->
        <xpath headerName="invoiceDetails">/invoice/@orderType = 'premium'</xpath>
        <to uri="mock:premium"/>
      </when>
      <when>
        <!-- use headerName attribute to refer to a header -->
        <xpath headerName="invoiceDetails">/invoice/@orderType = 'standard'</xpath>
        <to uri="mock:standard"/>
      </when>
      <otherwise>
        <to uri="mock:unknown"/>
      </otherwise>
    </choice>
  </route>
</camelContext>

Examples

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

from("direct:start").
        filter().xpath("/person[@name='James']").
        to("mock:result");

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

// lets define the namespaces we'll need in our filters
Namespaces ns = new Namespaces("c", "http://acme.com/cheese")
        .add("xsd", "http://www.w3.org/2001/XMLSchema");

// now lets create an xpath based Message Filter
from("direct:start").
        filter(ns.xpath("/c:person[@name='James']")).
        to("mock:result");

In this sample we have a choice construct. The first choice evaulates if the message has a header key type that has the value Camel.
The 2nd choice evaluates if the message body has a name tag <name> which values is Kong.
If neither is true the message is routed in the otherwise block:

from("direct:in").choice()
    // using $headerName is special notation in Camel to get the header key
    .when().xpath("$type = 'Camel'")
        .to("mock:camel")
    // here we test for the body name tag
    .when().xpath("//name = 'Kong'")
        .to("mock:donkey")
    .otherwise()
        .to("mock:other")
    .end();

And the spring XML equivalent of the route:

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:in"/>
        <choice>
            <when>
                <xpath>$type = 'Camel'</xpath>
                <to uri="mock:camel"/>
            </when>
            <when>
                <xpath>//name = 'Kong'</xpath>
                <to uri="mock:donkey"/>
            </when>
            <otherwise>
                <to uri="mock:other"/>
            </otherwise>
        </choice>
    </route>
</camelContext>

XPath injection

You can use Bean Integration to invoke a method on a bean and use various languages such as XPath to extract a value from the message and bind it to a method parameter.

The default XPath annotation has SOAP and XML namespaces available. If you want to use your own namespace URIs in an XPath expression you can use your own copy of the XPath annotation to create whatever namespace prefixes you want to use.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.w3c.dom.NodeList;

import org.apache.camel.component.bean.XPathAnnotationExpressionFactory;
import org.apache.camel.language.LanguageAnnotation;
import org.apache.camel.language.NamespacePrefix;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@LanguageAnnotation(language = "xpath", factory = XPathAnnotationExpressionFactory.class)
public @interface MyXPath {
    String value();

    // You can add the namespaces as the default value of the annotation
    NamespacePrefix[] namespaces() default {
    @NamespacePrefix(prefix = "n1", uri = "http://example.org/ns1"),
    @NamespacePrefix(prefix = "n2", uri = "http://example.org/ns2")};

    Class<?> resultType() default NodeList.class;
}

i.e. cut and paste upper code to your own project in a different package and/or annotation name then add whatever namespace prefix/uris you want in scope when you use your annotation on a method parameter. Then when you use your annotation on a method parameter all the namespaces you want will be available for use in your XPath expression.

For example

public class Foo {
	
    @MessageDriven(uri = "activemq:my.queue")
    public void doSomething(@MyXPath("/ns1:foo/ns2:bar/text()") String correlationID, @Body String body) {
		// process the inbound message here
    }
}

Using XPathBuilder without an Exchange

Available as of Camel 2.3

You can now use the org.apache.camel.builder.XPathBuilder without the need for an Exchange. This comes handy if you want to use it as a helper to do custom xpath evaluations.

It requires that you pass in a CamelContext since a lot of the moving parts inside the XPathBuilder requires access to the Camel Type Converter and hence why CamelContext is needed.

For example you can do something like this:

boolean matches = XPathBuilder.xpath("/foo/bar/@xyz").matches(context, "<foo><bar xyz='cheese'/></foo>"));

This will match the given predicate.

You can also evaluate for example as shown in the following three examples:

    String name = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>cheese</bar></foo>", String.class);
    Integer number = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>123</bar></foo>", Integer.class);
    Boolean bool = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>true</bar></foo>", Boolean.class);

Evaluating with a String result is a common requirement and thus you can do it a bit simpler:

    String name = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>cheese</bar></foo>");

Using Saxon with XPathBuilder

Available as of Camel 2.3

You need to add camel-saxon as dependency to your project.

Its now easier to use Saxon with the XPathBuilder which can be done in several ways as shown below.
Where as the latter ones are the easiest ones.

Using a factory

// create a Saxon factory
XPathFactory fac = new net.sf.saxon.xpath.XPathFactoryImpl();

// create a builder to evaluate the xpath using the saxon factory
XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]").factory(fac);

// evaluate as a String result
String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
assertEquals("def", result);

Using ObjectModel

// create a builder to evaluate the xpath using saxon based on its object model uri
XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]").objectModel("http://saxon.sf.net/jaxp/xpath/om");

// evaluate as a String result
String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
assertEquals("def", result);

The easy one

// create a builder to evaluate the xpath using saxon
XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]").saxon();

// evaluate as a String result
String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
assertEquals("def", result);

Setting a custom XPathFactory using System Property

Available as of Camel 2.3

Camel now supports reading the JVM system property javax.xml.xpath.XPathFactory that can be used to set a custom XPathFactory to use.

This unit test shows how this can be done to use Saxon instead:

// set system property with the XPath factory to use which is Saxon 
System.setProperty(XPathFactory.DEFAULT_PROPERTY_NAME + ":" + "http://saxon.sf.net/jaxp/xpath/om", "net.sf.saxon.xpath.XPathFactoryImpl");

// create a builder to evaluate the xpath using saxon
XPathBuilder builder = XPathBuilder.xpath("tokenize(/foo/bar, '_')[2]");

// evaluate as a String result
String result = builder.evaluate(context, "<foo><bar>abc_def_ghi</bar></foo>");
assertEquals("def", result);

Camel will log at INFO level if it uses a non default XPathFactory such as:

XPathBuilder  INFO  Using system property javax.xml.xpath.XPathFactory:http://saxon.sf.net/jaxp/xpath/om with value:
                    net.sf.saxon.xpath.XPathFactoryImpl when creating XPathFactory

To use Apache Xerces you can configure the system property

-Djavax.xml.xpath.XPathFactory=org.apache.xpath.jaxp.XPathFactoryImpl

Enabling Saxon from Spring DSL

Available as of Camel 2.10

Similarly to Java DSL, to enable Saxon from Spring DSL you have three options:

Specifying the factory

<xpath factoryRef="saxonFactory" resultType="java.lang.String">current-dateTime()</xpath>

Specifying the object model

<xpath objectModel="http://saxon.sf.net/jaxp/xpath/om" resultType="java.lang.String">current-dateTime()</xpath>

Shortcut

<xpath saxon="true" resultType="java.lang.String">current-dateTime()</xpath>

Namespace auditing to aid debugging

Available as of Camel 2.10

A large number of XPath-related issues that users frequently face are linked to the usage of namespaces. You may have some misalignment between the namespaces present in your message and those that your XPath expression is aware of or referencing. XPath predicates or expressions that are unable to locate the XML elements and attributes due to namespaces issues may simply look like "they are not working", when in reality all there is to it is a lack of namespace definition.

Namespaces in XML are completely necessary, and while we would love to simplify their usage by implementing some magic or voodoo to wire namespaces automatically, truth is that any action down this path would disagree with the standards and would greatly hinder interoperability.

Therefore, the utmost we can do is assist you in debugging such issues by adding two new features to the XPath Expression Language and are thus accesible from both predicates and expressions.

Logging the Namespace Context of your XPath expression/predicate

Every time a new XPath expression is created in the internal pool, Camel will log the namespace context of the expression under the org.apache.camel.builder.xml.XPathBuilder logger. Since Camel represents Namespace Contexts in a hierarchical fashion (parent-child relationships), the entire tree is output in a recursive manner with the following format:

[me: {prefix -> namespace}, {prefix -> namespace}], [parent: [me: {prefix -> namespace}, {prefix -> namespace}], [parent: [me: {prefix -> namespace}]]]

Any of these options can be used to activate this logging:

  1. Enable TRACE logging on the org.apache.camel.builder.xml.XPathBuilder logger, or some parent logger such as org.apache.camel or the root logger
  2. Enable the logNamespaces option as indicated in Auditing Namespaces, in which case the logging will occur on the INFO level

Auditing namespaces

Camel is able to discover and dump all namespaces present on every incoming message before evaluating an XPath expression, providing all the richness of information you need to help you analyse and pinpoint possible namespace issues.

To achieve this, it in turn internally uses another specially tailored XPath expression to extract all namespace mappings that appear in the message, displaying the prefix and the full namespace URI(s) for each individual mapping.

Some points to take into account:

  • The implicit XML namespace (xmlns:xml="http://www.w3.org/XML/1998/namespace") is suppressed from the output because it adds no value
  • Default namespaces are listed under the DEFAULT keyword in the output
  • Keep in mind that namespaces can be remapped under different scopes. Think of a top-level 'a' prefix which in inner elements can be assigned a different namespace, or the default namespace changing in inner scopes. For each discovered prefix, all associated URIs are listed.

You can enable this option in Java DSL and Spring DSL.

Java DSL:

XPathBuilder.xpath("/foo:person/@id", String.class).logNamespaces()

Spring DSL:

<xpath logNamespaces="true" resultType="String">/foo:person/@id</xpath>

The result of the auditing will be appear at the INFO level under the org.apache.camel.builder.xml.XPathBuilder logger and will look like the following:

2012-01-16 13:23:45,878 [stSaxonWithFlag] INFO  XPathBuilder  - Namespaces discovered in message: 
{xmlns:a=[http://apache.org/camel], DEFAULT=[http://apache.org/default], 
xmlns:b=[http://apache.org/camelA, http://apache.org/camelB]}

Dependencies

The XPath language is part of camel-core.

分享到:
评论

相关推荐

    Apache Camel中文开发使用指南.zip

    1. **DSL与路由定义**:Apache Camel支持多种DSL,如Java、XML和Groovy等,用于定义路由规则。例如,你可以使用`from()`方法指定消息来源,`to()`方法定义目标,中间可以用`process()`,`transform()`,`filter()`等...

    ApacheCamel-JDBC

    Apache Camel 是一个流行的开源集成框架,它允许开发者以声明式的方式定义路由和转换数据,使得在不同的软件组件之间实现通信变得更加简单。JDBC(Java Database Connectivity)是Java平台中的一个标准API,用于与...

    apache camel 集成组件 教程

    - **核心 Camel:** 第二部分“Core Camel”深入探讨了 Camel 的核心特性和高级用法,适合有一定基础的开发者进一步提升技能。 - **消息处理与转换:** 讲解了如何使用 Camel 进行复杂的数据处理和转换操作,包括...

    姜宁-我和Apache Camel这些年/Years of Apache Camel and I

    Apache Camel是Apache软件基金会下的一个开源集成框架,它实现了企业集成模式(Enterprise Integration Patterns,EIP),旨在简化在企业中的应用程序、系统和服务之间的集成工作。Camel支持多种传输协议,如HTTP、...

    Apache Camel 框架之---JMS路由.docx

    *支持多种消息协议:Apache Camel 框架支持多种消息协议,包括 JMS、AMQP、STOMP 等,使得开发者可以轻松地集成不同的消息系统。 Apache Camel 框架之 JMS 路由的应用场景包括: *Message-Oriented Middleware...

    Apache Camel Developer's Cookbook

    Apache Camel是一种开源的集成框架,其目的在于简化企业应用集成和规则流程的开发。它基于企业集成模式(Enterprise Integration Patterns),使得不同系统之间的消息传递和数据交换变得更加容易。Apache Camel支持...

    Apache Camel 开发指南.rar

    Apache Camel 是一个强大的开源企业级集成框架,它简化了在Java应用程序之间建立复杂的消息传递流程。这个"Apache Camel 开发指南"压缩包包含了丰富的资源,帮助开发者深入理解Camel的各个方面,包括路由表达式、...

    ApacheCamel:Estudo Sobre Apache骆驼

    **Apache Camel:深入理解企业级集成的利器** Apache Camel 是一个强大的开源框架,主要用于构建企业级集成解决方案。它以其丰富的组件、灵活的路由规则和基于Java的DSL(领域特定语言)而闻名,使得开发人员能够...

    简化软件集成:一个ApacheCamel教程

    简化软件集成:一个ApacheCamel教程在本教程中,您将了解集成大型软件的一些最大挑战,以及ApacheCamel如何轻松解决这些难题。在您的软件工程中,您可能至少做了一次以下操作:1.确定应启动数据发送的业务逻辑片段。...

    camel:学习使用Apache Camel的企业集成模式

    通过Spring Boot学习Apache Camel Framework通过Spring Boot学习Apache Camel Framework。 使用Active MQ,Kafka和REST API实施EI体系结构模式。 企业集成非常复杂。 微服务和云的发展使企业整合变得更加复杂。 您...

    ApacheCamel-FTP

    Apache Camel 是一个流行的开源集成框架,它允许开发者在不同的系统、协议和服务之间建立灵活的数据通信。FTP(File Transfer Protocol)是互联网上广泛使用的文件传输协议,用于上传、下载和管理远程服务器上的文件...

    apache-camel-3.7.0_数据同步_

    1. **组件**:Apache Camel 包含了大量的预构建组件,如JDBC(Java Database Connectivity)用于与数据库交互,FTP/FTPS/SFTP用于文件传输,MQTT和AMQP用于消息队列,以及HTTP/HTTPS用于Web服务等。这些组件使得数据...

    Java_Apache Camel Spring Boot示例.zip

    Java_Apache Camel Spring Boot示例是一个综合性的项目,展示了如何在Spring Boot应用程序中集成Apache Camel框架。Apache Camel是一个流行的开源框架,它简化了企业级集成(EIP,Enterprise Integration Patterns)...

    05-ApacheCamel-CXF-WebService

    Apache Camel 是一个强大的开源企业集成库,它提供了一种模型化的路由和消息转换方式,使得开发者可以轻松地在各种系统间构建复杂的集成解决方案。CXF 是一个流行的开放源代码服务框架,它支持多种Web服务标准,如...

    01-ApacheCamel-HelloWorld

    Apache Camel 是一个强大的开源集成框架,它允许开发者在各种组件之间路由、转换和处理消息,以构建企业级的集成解决方案。在这个"01-ApacheCamel-HelloWorld"示例中,我们将深入理解如何使用 Camel 实现一个简单的 ...

    Apache Camel 源码分析.rar

    Apache Camel 是一个强大的开源企业集成库,它提供了一种声明式的方式来定义路由和转换数据,使得构建复杂的分布式系统变得更加简单。Camel 使用一种名为“DSL”(Domain Specific Language)的语法规则,允许开发者...

    Apache Camel框架 HTTP相关的jar包

    Apache Camel 是一个强大的开源集成框架,它允许开发者通过声明式的方式连接不同的系统和服务,实现企业级应用程序的集成。这个框架的核心理念是“企业集成模式”(Enterprise Integration Patterns),它提供了丰富...

    ApacheCamelDemo:Apache Camel演示(学习示例演示)

    Apache Camel Demo Apache Camel Blog: 之前不小心将一些无用的配置文件上传了,请自行清除 该项目中包含 Apache Camel - FTP组件 Apache Camel - CXF组件(Code First and WSDL File First) Apache Camel - JMS/...

Global site tag (gtag.js) - Google Analytics