搜索

95.9. 使用命名空间

download PDF

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

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

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

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

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

/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 xmlns="http://camel.apache.org/schema/spring">
    <route>
      <from uri="direct:start"/>
      <filter>
        <xpath logNamespaces="true">/foo:person[@name='James']</xpath>
        <to uri="mock:result"/>
      </filter>
    </route>
  </camelContext>

</beans>

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

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.