搜索

112.4. 示例

download PDF
from("queue:foo")
  .filter().xquery("//foo")
  .to("queue:bar")

您还可以使用查询中的功能,在这种情况下,您需要明确的类型转换,或者您将收到一个 org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR。您需要传递函数的预期输出类型。例如,concat 函数返回一个 String,它按如下方式完成:

from("direct:start")
  .recipientList().xquery("concat('mock:foo.', /person/@city)", String.class);

在 XML DSL 中:

<route>
  <from uri="direct:start"/>
  <recipientList>
    <xquery type="java.lang.String">concat('mock:foo.', /person/@city</xquery>
  </recipientList>
</route>

112.4.1. 使用命名空间

如果您有一个标准的命名空间集,并且希望在多个 XQuery 表达式之间共享它们,您可以在使用 Java DSL 时使用 org.apache.camel.support.builder.Namespaces,如下所示:

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

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

注意如何将命名空间提供给 xquery 以及作为第二参数传递的 ns 变量。

每个命名空间都是 key=value 对,前缀是键。在 XQuery 表达式中,命名空间被前缀使用,例如:

/c:person[@name='James']

命名空间构建器支持添加多个命名空间,如下所示:

Namespaces ns = new Namespaces("c", "http://acme.com/cheese")
                     .add("w", "http://acme.com/wine")
                     .add("b", "http://acme.com/beer");

在 XML DSL 中使用命名空间时,如您在 XML root 标签中设置命名空间(或 camelContext, routes, route tag 之一)。

在下面的 XML 示例中,我们使用 Spring XML,其中在 root 标签 Bean 中声明命名空间,在 xmlns:foo="http://example.com/person" 一行中:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:foo="http://example.com/person"
       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">
    <route>
      <from uri="activemq:MyQueue"/>
      <filter>
        <xquery>/foo:person[@name='James']</xquery>
        <to uri="mqseries:SomeOtherQueue"/>
      </filter>
    </route>
  </camelContext>
</beans>

这个命名空间使用 foo 作为前缀,因此 <xquery> 表达式使用 /foo: 来使用这个命名空间。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.