命名空間支援
Spring Integration XML 模組中的所有元件都提供命名空間支援。為了啟用命名空間支援,您需要匯入 Spring Integration XML 模組的綱要。以下範例顯示典型的設定
<?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>
1 | 定義 XPath 運算式。必要。 |
2 | 底層 bean 定義的識別碼。它是 org.springframework.xml.xpath.XPathExpression 的實例。選用。 |
3 | 參考包含命名空間的地圖。地圖的鍵定義命名空間前綴,而地圖的值設定命名空間 URI。同時指定此屬性和 map 元素或 ns-prefix 和 ns-uri 屬性是無效的。選用。 |
4 | 可讓您直接在 XPath 運算式元素上將命名空間前綴設定為屬性。如果您設定 ns-prefix ,您也必須設定 ns-uri 屬性。選用。 |
5 | 可讓您直接在 XPath 運算式元素上將命名空間 URI 設定為屬性。如果您設定 ns-uri ,您也必須設定 ns-prefix 屬性。選用。 |
6 | 定義包含命名空間的地圖。僅允許一個 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
) 在此情況下會失敗。
為了解決此問題,您必須透過設定 ns-prefix
和 ns-uri
屬性或設定 namespace-map
屬性來提供命名空間前綴和命名空間 URI。命名空間 URI 必須符合您的 XML 文件中宣告的命名空間。在先前的範例中,它是 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 相符,前綴則不需要。