名稱空間支援

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 表示式都將建立使用 Spring org.springframework.xml.xpath.XPathExpressionFactoryXPathExpression。建立 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-prefixns-uri 屬性。可選。
4 允許您直接將名稱空間字首設定為 XPath 表示式元素上的屬性。如果設定了 ns-prefix,則還必須設定 ns-uri 屬性。可選。
5 允許您直接將名稱空間 URI 設定為 XPath 表示式元素上的屬性。如果設定了 ns-uri,則還必須設定 ns-prefix 屬性。可選。
6 定義一個包含名稱空間的對映。只允許一個 map 子元素。對映的鍵定義名稱空間字首,對映的值設定名稱空間 URI。不能同時指定此元素和 map 屬性或設定 ns-prefixns-uri 屬性。可選。

為 XPath 表示式提供名稱空間(可選)

對於 XPath 表示式元素,您可以提供名稱空間資訊作為配置引數。您可以透過以下選擇之一定義名稱空間

  • 使用 namespace-map 屬性引用一個對映

  • 使用 map 子元素提供一個名稱空間對映

  • 指定 ns-prefixns-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-prefixns-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 需要匹配,而不是字首。
© . This site is unofficial and not affiliated with VMware.