命名空间支持
Spring Integration XML 模块中的所有组件都提供命名空间支持。为了启用命名空间支持,你需要导入 Spring Integration XML 模块的 schema。以下示例展示了一个典型的配置:
<?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/> 元素。
所有形式的XPath表达式都会创建一个XPathExpression,它使用Spring的org.springframework.xml.xpath.XPathExpressionFactory。创建XPath表达式时,会使用类路径上可用的最佳XPath实现(JAXP 1.3+ 或 Jaxen,优先选择 JAXP)。
在内部,Spring Integration 使用 Spring Web Services 项目提供的 XPath 功能(www.spring.io/spring-ws)。具体来说,我们使用 Spring Web Services XML 模块(spring-xml-x.x.x.jar)。如需更深入的理解,请参阅 docs.spring.io/spring-ws/docs/current/reference/#xpath 上的相应文档。
以下是 xpath-expression 元素所有可用配置参数的概述:以下列表展示了 xpath-expression 元素的可用属性:
<int-xml:xpath-expression expression="" // <1>
id="" // <2>
namespace-map="" // <3>
ns-prefix="" // <4>
ns-uri=""> // <5>
<map></map> // <6>
</int-xml:xpath-expression>
定义 XPath 表达式。必需。
底层 bean 定义的标识符。它是
org.springframework.xml.xpath.XPathExpression的一个实例。可选。引用包含命名空间的映射。映射的键定义命名空间前缀,映射的值设置命名空间 URI。同时指定此属性和
map元素或ns-prefix与ns-uri属性是无效的。可选。允许您直接在 XPath 表达式元素上设置命名空间前缀作为属性。如果设置了
ns-prefix,则必须同时设置ns-uri属性。可选。允许您直接在 XPath 表达式元素上设置命名空间 URI 作为属性。如果设置了
ns-uri,则必须同时设置ns-prefix属性。可选。定义包含命名空间的映射。只允许一个
map子元素。映射的键定义命名空间前缀,映射的值设置命名空间 URI。同时指定此元素和map属性或设置ns-prefix与ns-uri属性是无效的。可选。
为 XPath 表达式提供命名空间(可选)
对于XPath表达式元素,您可以将命名空间信息作为配置参数提供。您可以通过以下任一方式定义命名空间:
-
通过使用
namespace-map属性引用映射 -
通过使用
map子元素提供命名空间映射 -
指定
ns-prefix和ns-uri属性
所有三个选项互斥。只能设置其中一个选项。
以下示例展示了使用 XPath表达式的几种不同方式,包括设置 XML 命名空间的选项(前文已提及):
<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 文档(表示两份书籍订单):
<?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 表达式将按预期工作:
<int-xml:xpath-expression expression="/order/orderItem" />
你可能会认为同样的表达式也适用于下面的 XML 文件:
<?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>
前面的示例看起来与之前的示例完全相同,但声明了一个默认命名空间。
然而,之前的 XPath 表达式(/order/orderItem)在这种情况下会失败。
为了解决这个问题,您必须提供一个命名空间前缀和命名空间URI,可以通过设置 ns-prefix 和 ns-uri 属性,或者通过设置 namespace-map 属性来实现。命名空间URI必须与您的XML文档中声明的命名空间相匹配。在前面的示例中,该命名空间是 [www.example.org/orders](http://www.example.org/orders)。
然而,你可以任意选择命名空间前缀。实际上,提供一个空字符串也是可行的(但不允许使用 null)。当命名空间前缀为空字符串时,你的 XPath 表达式必须使用冒号 (:) 来表示默认命名空间。如果省略冒号,XPath 表达式将无法匹配。以下 XPath 表达式可以匹配前面示例中的 XML 文档:
<int-xml:xpath-expression expression="/:order/:orderItem"
ns-prefix="" ns-uri="https://www.example.org/prodcuts"/>
你也可以提供任意选择的其他命名空间前缀。以下XPath表达式(使用myorder命名空间前缀)同样匹配:
<int-xml:xpath-expression expression="/myorder:order/myorder:orderItem"
ns-prefix="myorder" ns-uri="https://www.example.org/prodcuts"/>
命名空间 URI 才是真正重要的信息,而非前缀。Jaxen 对此做了很好的总结:
在 XPath 1.0 中,所有无前缀的名称都是未限定的。XPath 表达式中使用的前缀不必与被查询文档中使用的前缀相同。只有命名空间 URI 需要匹配,前缀则不必。