对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

Spring Integration XML模块中的所有组件都提供命名空间支持。 为了启用命名空间支持,您需要导入 Spring Integration XML 模块的架构。 以下示例显示了一个典型设置:Spring中文文档

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:int="http://www.springframework.org/schema/integration"
  xmlns:int-xml="http://www.springframework.org/schema/integration/xml"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration
    https://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/xml
    https://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd">
</beans>

XPath 表达式

Spring Integration XML 模块中的许多组件都与 XPath 表达式一起使用。 这些组件中的每一个都引用已定义为顶级元素的 XPath 表达式,或者使用嵌套元素。<xpath-expression/>Spring中文文档

所有形式的 XPath 表达式都会导致创建一个使用 Spring 的表达式。 创建 XPath 表达式时,将使用类路径上可用的最佳 XPath 实现(JAXP 1.3+ 或 Jaxen,首选 JAXP)。XPathExpressionorg.springframework.xml.xpath.XPathExpressionFactorySpring中文文档

在内部,Spring Integration 使用 Spring Web 服务项目 (www.spring.io/spring-ws) 提供的 XPath 功能。 具体来说,我们使用 Spring Web Services XML 模块 (spring-xml-x.x.x.jar)。 如需更深入的了解,请参阅 docs.spring.io/spring-ws/docs/current/reference/#xpath 上的相应文档。

以下是该元素的所有可用配置参数的概述: 以下列表显示了该元素的可用属性:xpath-expressionxpath-expressionSpring中文文档

<int-xml:xpath-expression expression="" (1)
          id=""                         (2)
          namespace-map=""              (3)
          ns-prefix=""                  (4)
          ns-uri="">                    (5)
    <map></map>                         (6)
</int-xml:xpath-expression>
1 定义 XPath 表达式。 必填。
2 基础 Bean 定义的标识符。 它是 的实例。 自选。org.springframework.xml.xpath.XPathExpression
3 引用包含命名空间的映射。 映射的键定义命名空间前缀,映射的值设置命名空间 URI。 同时指定此属性和元素或 and 属性是无效的。 自选。mapns-prefixns-uri
4 允许您将命名空间前缀直接设置为 XPath 表达式元素上的属性。 如果设置了 ,则还必须设置属性。 自选。ns-prefixns-uri
5 允许您直接将命名空间 URI 设置为 XPath 表达式元素上的属性。 如果设置了 ,则还必须设置属性。 自选。ns-urins-prefix
6 定义包含命名空间的映射。 只允许一个子元素。 映射的键定义命名空间前缀,映射的值设置命名空间 URI。 同时指定此元素和属性或设置 and 属性是无效的。 自选。mapmapns-prefixns-uri

向 XPath 表达式提供命名空间(可选)

对于 XPath 表达式元素,可以提供命名空间信息作为配置参数。 您可以使用以下选项之一来定义命名空间:Spring中文文档

这三个选项都是相互排斥的。 只能设置一个选项。Spring中文文档

下面的示例演示了使用 XPath 表达式的几种不同方法,包括用于设置前面提到的 XML 命名空间的选项:Spring中文文档

<int-xml:xpath-filter id="filterReferencingXPathExpression"
                      xpath-expression-ref="refToXpathExpression"/>

<int-xml:xpath-expression id="refToXpathExpression" expression="/name"/>

<int-xml:xpath-filter id="filterWithoutNamespace">
    <int-xml:xpath-expression expression="/name"/>
</int-xml:xpath-filter>

<int-xml:xpath-filter id="filterWithOneNamespace">
    <int-xml:xpath-expression expression="/ns1:name"
                              ns-prefix="ns1" ns-uri="www.example.org"/>
</int-xml:xpath-filter>

<int-xml:xpath-filter id="filterWithTwoNamespaces">
    <int-xml:xpath-expression expression="/ns1:name/ns2:type">
        <map>
            <entry key="ns1" value="www.example.org/one"/>
            <entry key="ns2" value="www.example.org/two"/>
        </map>
    </int-xml:xpath-expression>
</int-xml:xpath-filter>

<int-xml:xpath-filter id="filterWithNamespaceMapReference">
    <int-xml:xpath-expression expression="/ns1:name/ns2:type"
                              namespace-map="defaultNamespaces"/>
</int-xml:xpath-filter>

<util:map id="defaultNamespaces">
    <util:entry key="ns1" value="www.example.org/one"/>
    <util:entry key="ns2" value="www.example.org/two"/>
</util:map>

将 XPath 表达式与默认命名空间结合使用

使用默认命名空间时,可能会遇到与预期不同的情况。 假设我们有以下 XML 文档(表示两本书的顺序):Spring中文文档

<?xml version="1.0" encoding="UTF-8"?>
<order>
    <orderItem>
        <isbn>0321200683</isbn>
        <quantity>2</quantity>
    </orderItem>
    <orderItem>
        <isbn>1590596439</isbn>
        <quantity>1</quantity>
    </orderItem>
</order>

本文档不声明命名空间。 因此,应用以下 XPath 表达式可以按预期工作:Spring中文文档

<int-xml:xpath-expression expression="/order/orderItem" />

您可能期望相同的表达式也适用于以下 XML 文件:Spring中文文档

<?xml version="1.0" encoding="UTF-8"?>
<order xmlns="http://www.example.org/orders">
	<orderItem>
		<isbn>0321200683</isbn>
		<quantity>2</quantity>
	</orderItem>
	<orderItem>
		<isbn>1590596439</isbn>
		<quantity>1</quantity>
	</orderItem>
</order>

前面的示例看起来与上一个示例完全相同,但声明了默认命名空间。Spring中文文档

但是,在这种情况下,前面的 XPath 表达式 () 失败。/order/orderItemSpring中文文档

若要解决此问题,必须通过设置 and 属性或设置属性来提供命名空间前缀和命名空间 URI。 命名空间 URI 必须与 XML 文档中声明的命名空间匹配。 在前面的示例中,即 .ns-prefixns-urinamespace-mapwww.example.org/ordersSpring中文文档

但是,您可以任意选择命名空间前缀。 事实上,提供空字符串确实有效。 (但是,不允许使用 null。 如果命名空间前缀由空字符串组成,则 Xpath 表达式必须使用冒号 (“:”) 来指示默认命名空间。 如果省略冒号,则 XPath 表达式不匹配。 以下 XPath 表达式与前面示例中的 XML 文档匹配:Spring中文文档

<int-xml:xpath-expression expression="/:order/:orderItem"
    ns-prefix="" ns-uri="https://www.example.org/prodcuts"/>

还可以提供任何其他任意选择的命名空间前缀。 以下 XPath 表达式(使用命名空间前缀)也匹配:myorderSpring中文文档

<int-xml:xpath-expression expression="/myorder:order/myorder:orderItem"
    ns-prefix="myorder" ns-uri="https://www.example.org/prodcuts"/>

命名空间 URI 是真正重要的信息,而不是前缀。 Jaxen很好地总结了这一点:Spring中文文档

在 XPath 1.0 中,所有不带前缀的名称都是非限定的。 不要求 XPath 表达式中使用的前缀与所查询文档中使用的前缀相同。 只有命名空间 URI 需要匹配,而不需要前缀匹配。
在内部,Spring Integration 使用 Spring Web 服务项目 (www.spring.io/spring-ws) 提供的 XPath 功能。 具体来说,我们使用 Spring Web Services XML 模块 (spring-xml-x.x.x.jar)。 如需更深入的了解,请参阅 docs.spring.io/spring-ws/docs/current/reference/#xpath 上的相应文档。
1 定义 XPath 表达式。 必填。
2 基础 Bean 定义的标识符。 它是 的实例。 自选。org.springframework.xml.xpath.XPathExpression
3 引用包含命名空间的映射。 映射的键定义命名空间前缀,映射的值设置命名空间 URI。 同时指定此属性和元素或 and 属性是无效的。 自选。mapns-prefixns-uri
4 允许您将命名空间前缀直接设置为 XPath 表达式元素上的属性。 如果设置了 ,则还必须设置属性。 自选。ns-prefixns-uri
5 允许您直接将命名空间 URI 设置为 XPath 表达式元素上的属性。 如果设置了 ,则还必须设置属性。 自选。ns-urins-prefix
6 定义包含命名空间的映射。 只允许一个子元素。 映射的键定义命名空间前缀,映射的值设置命名空间 URI。 同时指定此元素和属性或设置 and 属性是无效的。 自选。mapmapns-prefixns-uri