Namespace Support
Spring Integration XML 模块中的所有组件都提供命名空间支持。为了启用命名空间支持,需要导入 Spring Integration XML 模块的架构。以下示例显示了典型的设置:
All components within the Spring Integration XML module provide namespace support. In order to enable namespace support, you need to import the schema for the Spring Integration XML Module. The following example shows a typical setup:
<?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 Expressions
Spring Integration XML 模块中的很多组件都可以使用 XPath 表达式。这些组件中的每一个都引用了一个已定义为顶级元素的 XPath 表达式,或使用嵌套 <xpath-expression/>
元素。
Many of the components within the Spring Integration XML module work with XPath Expressions.
Each of those components either references an XPath Expression that has been defined as a top-level element or uses a nested <xpath-expression/>
element.
所有形式的 XPath 表达式都会创建一个使用 Spring org.springframework.xml.xpath.XPathExpressionFactory
的 XPathExpression
。在创建 XPath 表达式时,会使用在类路径上可用的最佳 XPath 实现(JAXP 1.3+ 或 Jaxen,首选 JAXP)。
All forms of XPath expressions result in the creation of an XPathExpression
that uses the Spring org.springframework.xml.xpath.XPathExpressionFactory
.
When XPath expressions are created, the best XPath implementation that is available on the classpath is used (either JAXP 1.3+ or Jaxen, with JAXP being preferred).
在内部,Spring Integration 使用 Spring Web Services 项目提供的 XPath 功能 ([role="bare"][role="bare"]https://www.spring.io/spring-ws)。具体来说,我们使用 Spring Web Services XML 模块 (spring-xml-x.x.x.jar)。为了更深入地理解,请参阅 [role="bare"][role="bare"]https://docs.spring.io/spring-ws/docs/current/reference/#xpath 中的相应文档。 |
Internally, Spring Integration uses the XPath functionality provided by the Spring Web Services project ([role="bare"]https://www.spring.io/spring-ws). Specifically, we use the Spring Web Services XML module (spring-xml-x.x.x.jar). For a deeper understanding, see the respective documentation at [role="bare"]https://docs.spring.io/spring-ws/docs/current/reference/#xpath. |
这里是对 xpath-expression
元素所有可用配置参数的概述:以下列表显示了 xpath-expression
元素的可用属性:
Here is an overview of all available configuration parameters of the xpath-expression
element:
The following listing shows the available attributes for the xpath-expression
element:
<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 | Defines an XPath expression. Required. |
2 | The identifier of the underlying bean definition.
It is an instance of org.springframework.xml.xpath.XPathExpression .
Optional. |
3 | Reference to a map that contains namespaces.
The key of the map defines the namespace prefix, and the value of the map sets the namespace URI.
It is not valid to specify both this attribute and the map element or the ns-prefix and ns-uri attributes.
Optional. |
4 | Lets you set the namespace prefix directly as an attribute on the XPath expression element.
If you set ns-prefix , you must also set the ns-uri attribute.
Optional. |
5 | Lets you directly set the namespace URI as an attribute on the XPath expression element.
If you set ns-uri , you must also set the ns-prefix attribute.
Optional. |
6 | Defines a map that contains namespaces.
Only one map child element is allowed.
The key of the map defines the namespace prefix, and the value of the map sets the namespace URI.
It is not valid to specify both this element and the map attribute or set the ns-prefix and ns-uri attributes.
Optional. |
Providing Namespaces (Optional) to XPath Expressions
对于 XPath 表达式元素,可以提供命名空间信息作为配置参数。可以使用以下选项之一来定义命名空间:
For the XPath Expression Element, you can provide namespace information as configuration parameters. You can define namespaces by using one of the following choices:
-
Reference a map by using the
namespace-map
attribute -
Provide a map of namespaces by using the
map
sub-element -
Specify the
ns-prefix
andns-uri
attributes
所有三个选项都互斥。只能设置一个选项。
All three options are mutually exclusive. Only one option can be set.
以下示例显示了使用 XPath 表达式的若干不同方式,包括对 XML 命名空间 [之前提到的 xpath-namespace-support] 进行设置的选项:
The following example shows several different ways to use XPath expressions, including the options for setting the XML namespaces xpath-namespace-support:
<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>
Using XPath Expressions with Default Namespaces
在使用默认命名空间时,可能会遇到与预期有差异的行为。假设有以下 XML 文档(它表示一个两个书籍的订单):
When working with default namespaces, you may run into situations that behave differently than you might expect. Assume we have the following XML document (which represents an order of two books):
<?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 表达式可以按预期工作:
This document does not declare a namespace. Therefore, applying the following XPath Expression works as expected:
<int-xml:xpath-expression expression="/order/orderItem" />
你可能会预料相同的表达式也适用于以下 XML 文件:
You might expect that the same expression also works for the following XML file:
<?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>
上述示例与前一个示例完全相同,但声明了一个默认命名空间。
The preceding example looks exactly the same as the previous example but declares a default namespace.
然而,之前的 XPath 表达式 (/order/orderItem
) 在这种情况下失败了。
However, the previous XPath expression (/order/orderItem
) fails in this case.
为了解决此问题,您必须提供命名空间前缀和命名空间 URI,可以通过设置 ns-prefix
和 ns-uri
属性或设置 namespace-map
属性来实现。命名空间 URI 必须与 XML 文档中声明的命名空间匹配。在上述示例中,即 http://www.example.org/orders
。
In order to solve this issue, you must provide a namespace prefix and a namespace URI either by setting the ns-prefix
and ns-uri
attributes or by setting the namespace-map
attribute.
The namespace URI must match the namespace declared in your XML document.
In the preceding example, that is http://www.example.org/orders
.
但是,您可以任意选择命名空间前缀。事实上,提供一个空字符串实际上有效。(但是,不允许为 null。)如果命名空间前缀由一个空字符串组成,您的 Xpath 表达式必须使用冒号 (":") 来指示默认命名空间。如果您不使用冒号,那么 XPath 表达式将不匹配。以下 XPath 表达式与上述示例中的 XML 文档匹配:
You can, however, arbitrarily choose the namespace prefix. In fact, providing an empty string actually works. (However, null is not allowed.) In the case of a namespace prefix consisting of an empty string, your Xpath expression must use a colon (":") to indicate the default namespace. If you leave off the colon, the XPath expression does not match. The following XPath Expression matches against the XML document in the preceding example:
<int-xml:xpath-expression expression="/:order/:orderItem"
ns-prefix="" ns-uri="https://www.example.org/prodcuts"/>
您也可以提供任何其他任意选择的命名空间前缀。以下 XPath 表达式(使用 myorder
命名空间前缀)也匹配:
You can also provide any other arbitrarily chosen namespace prefix.
The following XPath expression (which use the myorder
namespace prefix) also matches:
<int-xml:xpath-expression expression="/myorder:order/myorder:orderItem"
ns-prefix="myorder" ns-uri="https://www.example.org/prodcuts"/>
命名空间 URI 是真正重要的信息,而不是前缀。 Jaxen 很好地总结了这一要点:
The namespace URI is the really important piece of information, not the prefix. The Jaxen summarizes the point very well:
In XPath 1.0, all unprefixed names are unqualified. There is no requirement that the prefixes used in the XPath expression are the same as the prefixes used in the document being queried. Only the namespace URIs need to match, not the prefixes.