天天看點

開發地圖編輯器_使用地圖編輯器開發地圖

存在XML映射以将源XML文檔轉換為目标XML文檔。 映射編輯器擷取在“映射編輯器”中建立的映射,并生成XSL檔案以在運作時執行實際的XML轉換。 在WebSphere Integration Developer中,XML映射有兩種主要用法:

  • 在建構中介流時,有時會出現一個原語的輸出消息結構與下一個原語的輸入消息結構不比對的情況。 消息作為服務消息對象(SMO)通過中介流傳遞,該消息流具有一緻的XML結構。 在這種情況下,您可以使用映射将消息從一種結構轉換為另一種結構。
  • 在建構流程流時,有時還會需要将流程流中的變量從一種類型轉換為另一種類型。 您還可以在流程流中使用XML映射來轉換和操作變量。

建立映射時,目标是建構一個映射,以生成完整且有效的目标XML文檔。 生成的XML文檔必須完整,因為它包含所有預期資料,并且該文檔也必須有效并比對其對應的架構。

本系列的第2部分, 在Mapping Editor中使用複雜的XML結構 ,介紹了更進階的XML映射主題。

映射編輯器

映射編輯器使使用源XML文檔中的資料填充目标XML文檔成為一項直覺而直覺的任務。 通常,源XML結構顯示在左側,目标XML結構顯示在右側, 如圖1所示。 通過在源字段和目标字段之間繪制連接配接,可以在源元素與目标元素和屬性之間建立映射。 編輯區域下方的屬性視圖使您可以優化和自定義在“映射編輯器”中建立的映射。 映射編輯器還包含一個測試視圖,該視圖允許您關聯樣本輸入XML并立即檢視地圖的輸出XML。

圖1. XML映射編輯器
開發地圖編輯器_使用地圖編輯器開發地圖

建立地圖

在哪裡建立地圖

映射通常是在中介子產品項目中建立的,以在特定中介流中使用,或在子產品項目中建立的,以在特定業務流程中使用。 也可以在“庫”項目中建立它們,這将使它們可在依賴于“庫”的子產品中的任何地圖檔案中重複使用。

在中介流中,使用XSL Transformation原語建立地圖檔案。 每個XSL轉換原語都與一個映射檔案相關聯,該映射檔案描述了要對輸入XML進行的轉換。 映射檔案是根據實作原語時指定的資訊建立的。

在業務流程中,使用“資料映射”基本操作建立映射檔案。

在處理映射時,您可能會發現您正在不斷映射相同的兩種元素類型。 在這種情況下,您可能會發現建立一個Submap很有用,然後可以在映射這兩種類型時重新使用它。 子圖僅是您可以使用子圖變換從其他地圖檔案調用的正常地圖檔案。 您可以從“映射”編輯器内建立一個子圖,也可以通過右鍵單擊“業務內建”視圖并選擇“ 建立”>“ XML映射”來建立它。

選擇映射根

映射根是映射中的頂級輸入和輸出對。 通常,輸入基于一種XML Schema類型,而輸出則基于其他XML Schema類型。

在中介流中建立映射時,需要映射根。 映射根确定原始輸入消息的哪一部分用作映射輸入,以及原始輸出消息的哪一部分用作映射輸出。 要選擇映射根,首先要确定目标文檔的哪個部分需要更改,然後确定将填充目标文檔的資料的來源。 一旦知道要使用文檔的哪些區域,就可以為地圖選擇适當的根。 中介流中使用的XML映射隻能具有單個輸入或源以及單個輸出或目标。 在中介流的情況下,消息是SMO消息,其分解如下:

  • 語境
  • 标頭
  • 身體

您可以選擇SMO的任一部分作為根,或者,如果您需要多個SMO的部分,則可以選擇SMO的根。 在大多數情況下,您的更改将僅在SMO主體中發生,并且該主體是映射的适當根。 但是,有時您可能需要上下文或标頭中的資料來正确填充目标正文。 有時您可能需要更新目标上下文或目标标頭。 在這種情況下,您将需要使用SMO“ /”的根作為映射的根。 使用“ /”作為根可以通路源SMO和目标SMO的所有區域。 使用標明的根建立了SMO映射後,您将無法更改根。

映射細化

在“映射編輯器”中工作時,您會将源模式的元素和屬性與目标的元素和屬性相關聯。 一旦在源和目标之間建立了關聯,該關聯就稱為轉換或映射。 每個映射都可以有一個細化形式來訓示它是什麼類型的映射。 本節描述改進。

移動

Move是最基本的改進。 它在源端采用一個簡單或複雜的字段,并将其不變地從源移動到目标。 對複制字段所做的唯一更改将是名稱空間字首,以確定它們在目标文檔中有效。 當您想将某些内容從源複制到目标而不進行更改時,建議使用“移動”。

兌換

開發地圖編輯器_使用地圖編輯器開發地圖

轉換細化用于在簡單資料類型之間進行簡單轉換。 轉換細化的一個示例用法是将布爾值(真或假)轉換為整數值(1或0)。 另一個示例用法是從字元串中提取特定類型的值。 例如,考慮一個人的年齡,一個業務對象可能将年齡表示為字元串,而另一個則以int表示。 在這種情況下,可以使用Convert來確定期望但并非必須為int的字元串使用年限實際上是目标中的int。 如果無法将字元串轉換為整數,則将使用者指定的預設int放置在目标字段中。

配置設定

要為目标元素或屬性配置設定常量值時使用“配置設定細化”。 配置設定僅可用于将值配置設定給簡單類型字段,例如字元串和整數。 要建立配置設定映射:

  1. 選擇目标元素或屬性以配置設定值。
  2. 右鍵單擊目标元素或屬性,然後選擇建立變換 。
  3. 使用“屬性”視圖将所需的值配置設定給目标元素或屬性。

當地地圖

本地地圖是用于組織映射檔案的工具。 它使您可以嵌套複雜類型的映射,進而使頂層映射不會過于雜亂。 重要的是認識到在源和目标之間建立本地映射不會自行執行任何資料轉換。 除非您進入“本地”地圖并使用諸如“移動”之類的優化方法來建立映射,否則所有内容都不會從源移動到目标。 本地地圖用作容器映射,以對嵌套映射(例如Move)進行本地化,以執行實際的轉換。

本地地圖包含一個輸入字段和一個輸出字段。 在需要多個輸入字段的情況下,合并映射将替換本地映射,但是其行為類似。

在源和目标之間建立本地地圖後,可以輕按兩下本地地圖細化以導航到地圖。 進入本地地圖後,您可以建立子映射。 在本地地圖中,您會注意到,您可以使用“上一級”圖示導航出本地地圖,傳回到父級地圖(

開發地圖編輯器_使用地圖編輯器開發地圖

)在地圖區域的右上角。 在本地地圖中時,您會注意到灰色背景用于表示您正在嵌套映射中工作。

本地地圖不可重用。 如果要映射的源類型和目标類型将在其他映射中以相同的方式映射,請考慮使用可在多個映射檔案中重複使用和共享的子映射。 見子圖這篇文章部分有關建立子圖的更多資訊。

合并

合并細化在某種意義上類似于本地地圖,因為它是嵌套其他地圖的容器。 與本地地圖不同,合并支援多個源輸入。 這使您可以從兩個不同的源字段中擷取資料并将它們合并為一個目标字段。 在處理數組時也使用合并。 有關更多資訊,請參見第2部分中的使用數組 。

子圖

子圖細化是存儲在單獨檔案中的兩種特定類型之間的映射。 子映射是正常映射檔案中的根映射,您可以從任何其他映射檔案中引用它,是以非常适合重用。 由于子圖是為重用而設計的,是以我們建議您将子圖存儲在庫中,以便在相關子產品之間輕松共享和重用子圖。 如果您有兩個不同的複雜類型經常互相關聯以進行映射,則Submap是在這些類型之間建立可重用映射的好方法。

開發地圖編輯器_使用地圖編輯器開發地圖

如果您已經建立了本地地圖,然後又決定要重新使用該本地地圖,則可以将本地地圖的内容重構為子地圖。 要将本地地圖重構為子地圖,請右鍵單擊本地地圖,然後選擇“ 重構為子地圖”。

在某些情況下,您可能會發現無法為所需類型建立子圖,因為該類型未在XSD檔案中定義。 如果類型是在WSDL檔案中定義的,則可能是這種情況。 Submap建立向導将不允許您建立非XSD定義類型的Submap作為輸入或輸出。 在這種情況下,您可以通過執行以下操作從WSDL檔案中重構類型:

  1. 在“業務內建”視圖中,在子產品或引用的庫項目的“資料類型”類别中找到所需的類型。
  2. 右鍵單擊該類型,然後選擇“ 重構”>“提取内聯業務對象” 。

提取所需的類型後,可以使用提取的類型作為輸入或輸出來建立子圖。 使用局部元素或匿名類型時,無法使用Submap優化。 對于本地元素或匿名類型,目前不能選擇可重用的映射。

開發地圖編輯器_使用地圖編輯器開發地圖

提示 :如果子產品或庫中有許多地圖和子地圖,則可以使用“資料地圖目錄”檢視可用地圖的詳細摘要。 要檢視資料映射目錄,請在業務內建視圖中選擇一個項目,右鍵單擊該項目,然後選擇打開資料映射目錄 。

内建功能

您可以在“映射編輯器”中使用一些常用的内置函數,例如Concat,Normalize和Substring。 除這些以外,您還可以輕松使用60多種XPath和EXSLT Java™函數來轉換資料。 以下各節說明一些内置功能。 XPath和EXSLT Java函數部分介紹了如何使用其他函數。

康卡特

Concat函數會将源中的兩個或多個字元串連接配接到目标端的單個字元串值中。 内置的Concat函數通過其屬性頁支援字首,字尾和定界符的規範。

要在表1所示的示例中實作所需的輸出,可以使用具有以下屬性的Concat變換:

  • 指定輸入順序,以使cityName為第一個輸入,countryName為第二個輸入。
  • 在cityName輸入上使用定界符“,”。
表1.用例資料示例
輸入項 所需輸出

<cityName>Toronto</cityName> <countryName>Canada</countryName>

<destination>Toronto,Canada</destination>

歸一化

Normalize函數會将字元串從源移動到目标,并在移動過程中對字元串進行以下修改。

  • 删除前導和尾随空白。
  • 将字元串中的空白序列替換為單個空格。

要在表2所示的示例中獲得所需的輸出,可以使用Normalize變換。

表2.規範化示例
之前

<destination> Toronto Canada</destination>

<destination>Toronto Canada</destination>

子串

Substring函數将從源字元串中提取文本。 Substring函數使用分隔符和索引來确定要從傳入的源字元串中提取什麼文本。 根據指定的定界符,源字元串分為幾部分。 索引用于辨別要使用分割後的字元串的哪一部分。 預設情況下,索引為0,訓示将使用第一部分。 例如,假設您要從

<destination>Toronto, Canada</destination>

提取城市名稱。

您可以通過指定“,”(逗号)作為分隔符來使用Substring函數。 預設情況下,索引0會傳回分隔字元串的第一部分,在本示例中為Toronto。

要從同一字元串中提取國家/地區名稱,請指定“,”(逗号後的空格)作為分隔符,并将索引設定為1,這将導緻使用分割後的字元串的第二部分,在此示例中為加拿大。

如果不能保證源字元串中逗号後有空格,則可以對XPath表達式使用Custom修飾,例如:

normalize-space(substring-after($destination, ","))

XPath和EXSLT Java函數

開發地圖編輯器_使用地圖編輯器開發地圖

您可以使用XPath和EXSLT Java函數輕松處理,評估和格式化資料。 映射編輯器中支援許多此類功能,這些功能分為字元串,數學,布爾值以及資料和時間類别。

字元串函數

這些函數對字元串進行操作或傳回字元串。 映射編輯器支援的字元串功能的完整清單為:包含,格式編号,本地名稱,名稱,名稱空間-uri,開頭為,字元串,字元串長度,子字元串,之後的子字元串,之前的子字元串,系統-屬性,翻譯,對齊,數組連接配接和填充。

作為說明這些功能之一的示例,讓我們考慮數組concat函數。 array concat函數将字元串數組作為輸入,并将字元串的串聯傳回到單個字元串輸出中。

作為說明這些功能之一的示例,請考慮表3的數組concat函數。

表3.數組連接配接示例
輸入項 輸出量

<input>A</input> <input>B</input> <input>C</input> <input>D</input>

<output>ABCD</output>

在“映射編輯器”中建立數組連接配接函數後,“屬性”視圖中的“正常”頁籤将包含一個表,用于指定函數的輸入參數。 這些參數的值是XPath表達式,可以使用表示輸入連接配接的轉換變量。 預設情況下,輸入連接配接變量用作功能的輸入; 但是,可以根據需要進行編輯。

輸入參數的“正常”頁籤和表在所有其他XPath和EXSLT函數中通用。 一些功能包含可選的輸入參數,您可以根據需要添加或删除這些參數。

數學函數

這些函數對數字進行運算或傳回數值。 映射編輯器支援的數學函數的完整清單是:上限,計數,下限,數字,舍入,求和,abs,acos,asin,atan,atan2,常數,cos,exp,log,max,min,power,random ,sin,sqrt和tan。

布爾函數

這些函數對布爾值進行操作或傳回布爾值。 映射編輯器支援的布爾函數的完整清單為:布爾值,false,lang,not和true。

日期和時間功能

這些功能可操作或傳回日期和時間值。 映射編輯器中支援的日期和時間函數的完整清單是:date,dateTime,dayAbbreviation,dayInMonth,dayInWeek,dayInYear,dayName,dayOfWeekInMonth,格式化日期,hourInDay,jumpYear,minuteInHour,monthABbreviation,monthInYear,monthName,secondInMinute,時間weekInYear和year。

您可以使用

dateTime

函數擷取目前日期和時間,而無需輸入轉換。 您也可以使用沒有輸入的其他日期和時間函數來擷取目前值。 例如,使用在星期三無輸入的

dayAbbreviation

函數将産生

Wed

的目标值。

格式化日期函數是有用的函數,用于根據指定的模式轉換給定的dateTime,如表4所示。

表4.格式日期功能示例
之前 模式

Wednesday January 27, 2010

yyyy-MM-dd hh:mm:ss

2010-01-27 06:29:25

Wednesday January 27, 2010

MMMM dd, yyyy

January 27, 2010

“屬性”視圖中的“正常”頁籤包含配置“格式化日期”功能所需的屬性。 日期和時間輸入參數是XPath表達式,預設情況下,它使用表示函數輸入連接配接的變量。 或者,您可以編輯此内容以輸入使用者定義的XPath表達式。

日期選擇器在“格式化日期”功能屬性的“正常”頁籤中也可用,可以用作輸入日期和時間。 選擇日期會生成一個XPath文字表達式,作為表示所選日期和時間的函數的輸入。

格式化日期功能還包含一個模式,用于指定如何格式化給定的日期和時間。 可以從組合框中選擇許多預定義的模式,或者可以輸入使用者定義的模式。 同樣,如果在輸入資料結構中定義的源元素中指定了模式,則也可以将用于表示輸入連接配接的XPath變量用作模式。

最後,為格式日期功能提供了一個示例字段,以顯示期望的結果以及提供的資訊。

擡頭

開發地圖編輯器_使用地圖編輯器開發地圖

您可以使用“查找”細化功能來基于鍵查找值。 這個想法是将查找的源輸入用作鍵。 查找将使用鍵檢索目标的值。 查找使用查找引擎來關聯鍵和值。 映射編輯器中提供了以下查找引擎:

  • 逗号分隔值檔案查找 :CSV(逗号分隔值)查找引擎将使用逗号分隔值檔案來執行查找。 CSV檔案的每一行都被視為新條目。 CSV檔案的第一行可以是檔案的列标題。 在CSV檔案中進行查找時,這些屬性允許您指定将哪個列用作鍵列,将哪個列用作值列。 您可以使用從0開始的索引或使用列标題名稱來辨別列。 要了解CSV檔案中的列,請考慮以下資料:
    State Name, Abbreviation, Old GPO, FIPS
    
    Alabama, AL, Ala., 01
    
    Alaska, AK, Alaska, 02
    
    Arizona, AZ, Ariz, 04
    
    Arkansas, AR, Ark., 05
               

    如果使用上述CSV檔案根據狀态縮寫檢索狀态名稱,則可以設定查找以将第1列用作鍵列,将0列用作值列。 或者,您也可以設定查找,以将“縮寫”列用作鍵列,将“狀态名稱”用作值列。 在這兩種情況下,鍵AK都會傳回阿拉斯加的值。

    要使用CSV查找,CSV檔案必須具有* .csv檔案擴充名。 CSV檔案必須與地圖檔案位于同一項目中,或位于從屬項目中。

  • 屬性檔案查找 :屬性檔案查找使用“ key = value”格式的檔案來執行查找。
  • 關系查找 :關系使用現有的靜态關系來執行查找。 在WebSphere Integration Developer中定義了靜态關系來描述靜态關聯。 例如,可以定義一個關系來将狀态名稱與狀态縮寫相關聯。 一旦定義了關系,映射内的查找就可以使用該關系來檢索值。 這些屬性使您可以定義關系中的哪個角色用作鍵,以及哪個角色用作值。

以下示例說明了查找映射的用法。 讓我們考慮圖2所示的CSV檔案。

圖2. CSV檔案示例
開發地圖編輯器_使用地圖編輯器開發地圖

在此示例中,輸入檔案将包含狀态縮寫,而輸出将需要完整的狀态名稱, 如圖3所示。

圖3.查找映射
開發地圖編輯器_使用地圖編輯器開發地圖

上面查找的屬性如圖4所示。

圖4.查找屬性
開發地圖編輯器_使用地圖編輯器開發地圖

上圖的結果如圖5所示。

圖5.映射結果
開發地圖編輯器_使用地圖編輯器開發地圖

也可以提供自定義查找引擎。 産品幫助中的“ 建立自定義查找”主題說明了定義自定義查找引擎的過程。

其他改進

本文中未提及的其他改進主要用于處理數組和其他結構化資料時。 有關更多資訊,請參見第2部分中的使用數組和自定義 。

條件映射

有時,地圖中的變換取決于條件。 條件用于确定特定映射在運作時是否會發生,進而提供一種控制映射邏輯流程的方式。 映射編輯器包含對使用If,Else if和Else轉換在映射上實作這種條件邏輯的支援。 這些轉換根據給定的XPath表達式條件對一組映射進行分組。 另外,XML映射編輯器還通過直接提供XPath表達式或使用靜态Java方法調用來支援單個映射的條件。 最後,隐式條件自動應用于連接配接到源資料的轉換,這是可選的。 僅當源資料在運作時存在時,才會對可選源資料進行任何轉換。 否則,将不會發生轉換。

If,Else if和Else細化

開發地圖編輯器_使用地圖編輯器開發地圖

If,Else if和Else細化是用于控制映射内映射流的條件轉換。 這些改進中的每一個都是容器轉換,這意味着它們需要包含嵌套映射以提供任何有用的實作。 由于If,Else if和Else細化是容器變換,是以這些容器提供了一種基于已知條件對一組所需映射進行分組的方法。

通過從“屬性”視圖的“條件”頁籤中為其配置設定XPath表達式,可以在If和Else if精簡條件上提供條件。 Else細化隻能與關聯的If或Else if細化一起存在,并且不具有任何條件。 請參閱XPath條件部分,以了解如何評估此類表達式。

在“映射編輯器”中建立If細化類似于建立其他變換。 隻需在源元素和目标元素之間建立連接配接,并将轉換類型更新為If細化。 建立If細化後,可以通過選擇If細化并從懸停操作欄或上下文菜單中調用“添加其他if”或“添加其他”操作來添加其他if或Else細化。 建立Else if或Else精煉後,您必須添加目标連接配接以提供該精煉将在輸出中填充的内容的上下文。 同樣,您可以選擇添加源連接配接以提供優化的輸入上下文。 與其他細化不同,If,Else if和Else細化可以分别具有多個輸入和輸出連接配接,這将為嵌套映射提供操作依據。

If,Else if和Else優化項組合在一起以說明它們彼此之間的關聯。 當映射包含If,Else if或Else細化的此類分組時,基于提供的條件,組中的這些細化中隻有一個或一個都不會在運作時執行。 僅執行條件為第一個If或Else if的條件細化,該條件的計算結果為true。 否則,如果所有其他關聯條件在分組内均失敗,則将執行Else細化。 一旦執行了這些優化之一,與條件優化相關的所有嵌套映射将在運作時執行。

在If,Else if和Else細化分組上建立目标連接配接時,由于對這些細化施加了限制,是以必須小心:

  1. If,Else if和Else精煉分組中的所有目标連接配接都必須以目标資料結構上的同級元素為目标。 否則,将導緻錯誤消息如下:“如果轉換組未将彼此相同的元素作為目标,則Else中的所有轉換”。 若要更正此問題,請確定條件精簡組内的所有目标連接配接都是彼此的同級。
  2. 不相關的轉換輸出不能存在于條件轉換組的輸出範圍内。 不相關的轉換是那些不包含在If,Else if和Else改進中的轉換。 條件轉換組的輸出範圍定義為在最頂部連接配接的輸出元素和最底部連接配接的輸出元素之間。 不遵守此限制将導緻以下錯誤消息:“ Move轉換存在于條件轉換的輸出範圍之外,這是無效的”。 若要更正此問題,請将無關轉換放在條件轉換之一中(如果在所有情況下都适用,則将其複制到所有條件轉換中)。 以下說明了圖6中所示的條件分組範圍的示例。
    圖6.條件映射
    開發地圖編輯器_使用地圖編輯器開發地圖
    注意,條件轉換組的輸出範圍在元素“ field1”和元素“ field5”之間。 除非将其包含在If,Else if或Else精簡之一中,否則其他任何不相關的轉換都不能包含在此突出顯示的區域内。

單變換條件

有時,單個轉換隻需要一個簡單的條件即可執行所需的映射。 在這種情況下,您可以将XPath表達式應用于單個轉換,而無需進行If細化。 就像在If和Else if精煉上添加XPath表達式一樣,您可以使用Properties視圖中的Condition頁籤在單個轉換上添加條件。

為了更好地了解“映射編輯器”中内置的單個轉換條件和隐式條件,請考慮一種情況,如果您想使用一個輸入字段的值(如果源中存在該值)。 否則,您将使用其他輸入字段的值。 請考慮以下情形:

輸入:

  • 用于特定建議的可選字元串輸入。
  • 一般推薦的必需字元串輸入。

輸出:

  • 單個建議盡可能具體。 如果有具體的建議,那就是價值。 否則,将使用一般建議。

在這種情況下,您可以考慮這樣做:

  • 在輸入特定建議和輸出建議之間建立移動細化。 存在特定建議時,将發生“移動”并且正确設定了值。 由于将為可選輸入自動生成條件,是以無需在此轉換中添加其他條件。
  • 在輸入的一般建議和輸出的建議之間建立移動細化。 使用以下條件對Move進行條件處理,以便僅在輸入中不存在特定建議時才執行Move:

    not(/body/response/specificRecommendation)

使用條件的另一種方法是将過濾器應用于可重複元素清單。 有關更多資訊,請參見第2部分中的過濾數組 。

XPath條件

您可以使用XPath表達式為If,Else If或其他優化條件編寫條件。 評估XPath表達式時,将根據表達式的傳回值确定條件的值,如表5所示。

表5. XPath條件評估标準
表達式傳回類型 真實結果 錯誤的結果
節點集 非空節點集 空節點集
長度為一個或多個字元的字元串 零長度的字元串
零以外的任何數字(負數或正數)
布爾值 真正

使用XPath編寫條件時,可以使用相對或絕對路徑。 轉換的輸入可用作變量,并插入表達式中。 您可以在條件輸入區域中使用内容輔助來檢視可用變量的清單。 您還可以在條件輸入區域中使用内容輔助來利用XPath函數。 要在條件輸入區域中調用内容輔助,請使用CTRL + Space 。

使用相對路徑時,目前無法使用

../<element name>

文法在上下文節點上方引用。 如果需要通路在輸入值之一中不可通路的資訊,請使用絕對路徑。 對于使用變量的示例, 圖7中的内容幫助顯示cityName可作為變量使用:

圖7. XPath内容助手
開發地圖編輯器_使用地圖編輯器開發地圖

單擊上面清單中的cityName變量後,将“ $ cityName”插入輸入字段,然後可以繼續建立條件,例如

$cityName = "Toronto"

要了解使用條件的基礎,請考慮以下示例。 一個旅行公司以下列提示之一的形式向客戶提供有關其旅行目的地的建議:

  • 天氣會很熱 -帶上防曬霜。
  • 天氣會很冷 -帶上皮大衣。

提示取決于所選目的地的溫度。 對于溫度高于0°C的目的地,會給出炎熱的天氣提示。 否則,會給出寒冷的天氣提示。 您可以像這樣調整映射:

  1. 根據源celsiusTemperature元素建立If細化,該元素的目标是輸出中的咨詢元素。
  2. 将以下XPath表達式添加為If細化條件

    $celsiusTemperature > 0

  3. 向If細化添加和其他細化,然後将顧問元素定位到輸出上。
  4. 在If細化中添加一個嵌套的Assign變換,其值為“天氣會很熱 -帶上防曬霜”。
  5. 在“ Else”細化中添加一個嵌套的Assign變換,其值為“天氣将很冷 -帶上您的風衣”。

Java條件

您還可以使用對靜态Java方法的調用來建構條件。 使用Java方法調用時,該方法的傳回值确定條件結果,如表6所示。

表6. Java條件評估标準
方法傳回類型 真實條件 錯誤條件
org.w3c.dom.NodeList 非空節點清單 空節點清單或null
長度為一個或多個字元的字元串 零長度的字元串
整型 零以外的任何數字(負數或正數)
布爾值 真正

編寫Java方法以計算條件的邏輯:

  1. 在現有的Java項目中建立新的靜态Java方法,或建立一個新的Java項目以在其中存儲該方法。
  2. 確定您的映射項目或其庫依賴項之一包含對Java項目的依賴項。
  3. 單擊“ Java導入”屬性頁面,然後單擊“ 添加”按鈕。 接下來,指定字首和關聯的Java類。
  4. 在條件輸入字段中,使用内容輔助(可通過按CTRL + Space來獲得 )來插入适當的方法調用。

例如,假設您具有以下條件:

類: TravelCompanyUtilities
方法: isDesirableCity
參數: cityName(類型為String)

The condition might look like this:

TravelCompanyUtilities::isDesirableCity(/body/response/destination/cityName)

要麼

TravelCompanyUtilities::isDesirableCity($cityName)

技巧和竅門

This section contains some tips and tricks to help make XML mapping an easier task. The tips in this section include:

  • Tips for working faster : Match mapping, content assist.
  • Tips for working with large schemas : Scrolling, filtering, navigation trail.
  • Other tips : Test map view, sorting transforms, viewing model groups, moving mappings.

Match mapping

You can use match mapping when the source and target are similar or contain similar sections. Match mapping always attempts to match whatever is showing on the source side of the editor with whatever is on the target side of the editor. It is not dependent on a selection. A match is made when an element or attribute on the source side has the same name as an element or attribute on the target side. The types of the matched elements must be similar for the match to produce useful mappings. If the matched types are complex types, a container map is used to match them at the top level, and then match mapping will continue within the container mapping until no more matches are found. To invoke match mapping, use the "Map source to target based on the name and types" toolbar button (

開發地圖編輯器_使用地圖編輯器開發地圖

)。 Match mapping will always use container mappings (for example, Local map, or For each in the case of arrays) to map complex types rather than using a Move, which makes it easier to tweak the mappings that are created.

For example, if a Move transform had been used to map the body on the source to the body on the target, and you wanted to make a minor adjustment to something in the body, you need to delete the Move transform and create individual transforms at a more granular level. However, when using match mapping, container maps are used and drilling into those container maps allow you to get to the exact location where the changes need to be made. In this way, only transforms that correspond to the minor adjustments need to be changed.

When the source and target have similar fields of the same type, but may not have the same name, you can use Local maps to match these similar types, and then use match mapping within the Local maps that you create. For example, suppose we have the following source and target as shown in Figure 8 .

Figure 8. Mapping editor before match mapping is invoked
開發地圖編輯器_使用地圖編輯器開發地圖

Invoking match mapping at this level will not find any matches and will display an error message indicating that no matches were found for the "getWeather" element in the target. However, the GetWeather element in the target is looking for a cityName and a countryName which will come from the first element in the destinations array on the source side. We can reset the starting point for the match mapping by creating a Local map between the destinations element in the source and the GetWeather element in the target as shown in Fiure 9.

Figure 9. Mapping editor after match mapping is invoked
開發地圖編輯器_使用地圖編輯器開發地圖

From within the Local map, invoking match mapping will map the CityName and CountryName.

Content assist and XPath Expression Builder

Some of the entry fields that are used within the property pages of the Mapping editor contain content assist to help when syntax is important. If content assist is available in an entry field, a light-bulb icon (

開發地圖編輯器_使用地圖編輯器開發地圖

) will appear next to the entry field and the hover help on the light-bulb will indicate whether or not content assist is available. To invoke the content assist, use CTRL+Space .

When editing an XPath expression, the content assist will contain a menu item called "Insert Simple XPath" expression. Clicking the menu option will display the "Simple XPath Expression Builder" dialog. The XPath Expression Builder assists in creating simple XPath expressions as shown in Figure 10 .

Figure 10. Simple XPath expression builder
開發地圖編輯器_使用地圖編輯器開發地圖

卷動

When creating a transform or mapping in the editor, it is typical to do so by locating a source input field, clicking on the source input field, holding the mouse button down, dragging a connection over to the target field, and finally releasing the mouse button to complete the action. Using this method is quick and easy, but inhibits the ability of using the editor's scroll bars at the same time. This makes it hard to work with large schemas where the source and target fields of an intended transformation are not always in view with each other at the same time. You can use the following methods to create transformations between a source and target that are not visible at the same time.

  1. Hover over the source input field, single-click the drag handle that appears, but do not hold down the mouse button as shown in Figure 11 .
    Figure 11. Drag handle
    開發地圖編輯器_使用地圖編輯器開發地圖
    This will initiate the connection creation process while still giving the ability to use the scrollbars at the same time. While the connection wire is active, you can also initiate scrolling by hovering near the editor edge and pausing briefly.
  2. Select the source input field without holding down the mouse button. Scroll to the appropriate matching field in the target, hold the CTRL key and click the target field to select it. Finally, right-click on the target field and select Create Transform . A transform between the selected source and target will be created and you can then customize the transform as desired.
  3. Select the source input field, right-click, and select Create Connection . Once the connection wire appears, you can use the scrollbars at the same time to find the target field and complete the transformation. While the connection is being created, you can also initiate scrolling by hovering near the editor edge and pausing briefly.

篩選

開發地圖編輯器_使用地圖編輯器開發地圖

In cases where the input or output of a mapping contain many fields, finding and navigating to a particular field can be tedious. A filter is provided on both the source and target columns to make finding such fields easier. Once a filter is applied to a source or target column, the column will show only fields that contain the filter text in the field name. To use a filter, enter the filter text in the following location and press Enter .

Figure 12. Mapping editor source and target filters
開發地圖編輯器_使用地圖編輯器開發地圖

To clear a filter, click <Show all> at the bottom of the column or clear the filter text.

Navigation trail

As you create nested mappings such as Local maps, you can find yourself deep within an element structure wondering where exactly you came from. At the top of the editor is a navigation trail that lets you know what elements you are nested in, which is shown in Figure 13 . Clicking any part of the navigation trail will quickly bring the editor view to the associated level.

Figure 13. Mapping editor navigation trail
開發地圖編輯器_使用地圖編輯器開發地圖

Test map view

While working in the Mapping Editor, you can use the Test Map view to test your mapping transforms as you create them. For details on the The Test Map view, see Problem determination .

Sorting transforms

By default, the Mapping Editor will sort the transforms column based on the source input. This means that the transformations align themselves with their associated source input fields. In some cases, it may be more convenient to have the transform column sorted by the target outputs. Since this will align transformations closest to their target output, it provides a quick way to see what type of transformation is operating on a particular target. To switch the sorting method, right-click in the mapping editor area and use the "Sort Transforms" menu action, or click the desired sort method button on the local toolbar (by source

開發地圖編輯器_使用地圖編輯器開發地圖

, or by target

開發地圖編輯器_使用地圖編輯器開發地圖

)。

Viewing model groups

By default, the Mapping Editor uses a simplified view of the XML inputs and outputs. There is some information that may be in the schema that is not shown by default in the Mapping Editor. For example, a choice defined in the schema is shown as a flat list in the mapping editor. To see such model group information in the XML Mapping Editor, edit the XML Mapping preferences to show the groups. To edit the preferences, click the Preferences (

開發地圖編輯器_使用地圖編輯器開發地圖

) button on the Mapping Editor local toolbar.

In addition to the details shown in the Mapping Editor window, you can also see general information about the selected element in the properties view as well.

Moving mappings

開發地圖編輯器_使用地圖編輯器開發地圖

As of Version 7.0, the Mapping Editor supports cutting, copying, and pasting mappings. The cut, copy, and paste actions are useful in the following cases:

  • A map becomes disorganized because not enough container mappings are being used. In this case, you can create new container mapping and then cut (CTRL+X) the existing mappings that are outside the new container mapping and paste (CTRL+V) them onto the new container mapping. The pasted mapping will then be moved inside the new container mapping. For more information about organizing maps, see Organizing mappings .
  • An existing mapping needs to be moved from one target to another. In some cases, you may have already created a mapping to a certain target and then realize that you meant to create that mapping from the same source but to a different target with the same type. In that case, you can cut (CTRL+X) the existing map, select the correct target field, and then select paste (CTRL+V).
  • An existing mapping needs to be duplicated for a different source and target. If you have already created a mapping between a source and target, then you decide that you want to create the same mapping between a different source and target that have the same types as the original source and target, you can copy (CTRL+C) the existing mapping, select the new desired source and target elements (hold the CTRL key to multi-select), then invoke paste (CTRL+V).
  • A mapping from one file needs to be duplicated in another file. As long as the source and target types are the same, you can copy a mapping from one file to another using copy and paste. Start by copying (CTRL+C) the mapping in one file, go to the new file, select the new source and target fields (using the CTRL key to multi-select), and then invoke the paste action (CTRL+V).

Problem determination

While creating a map, it is a good idea to test the map periodically to ensure that the desired results are being achieved.

Input files

To test a map in the tooling, the first step is to create or select an input document to test. The easiest way to accomplish this is to use the input document generation tools that are included in the Mapping Editor. These tools will create a basic input document based on the input XML schema. Once the input document has been generated, you can start testing immediately and can later enhance the input by adding additional elements and values. Use the following methods to create input documents:

  1. While using the Create New XML Mapping wizard from an XSL Transformation primitive in a mediation flow, you can check the Create a sample XML input file for testing the XML Map checkbox.
  2. From within the Mapping Editor, you can click the Associate XML files toolbar button, and then use the Generate Input button to create and associate a new sample input file.

    The Generate Sample Input File button is disabled in cases where the root input of the map is not a global element. Maps with global element inputs can only be tested by calling them from another map.

  3. From within the Mapping Editor, you can click the Test Map toolbar button to open the Test Map view. From the Test Map view, click the Associate XML files toolbar button and then use the Generate Input button to create and associate a new sample input file.

As an alternative to creating a sample input file, if you already have a sample input file, you can associate the existing XML input file with the map file using the Associate XML files toolbar action. Currently, input files are generally saved in the same project as the maps they apply to and show up in the Business Integration view under the Transformations > Data Map Test Data category .

Iterative testing using the Test Map view

Once you have a sample input file associated with the map file, you can use the Test Map view to test the map file. This method of testing can be used as you are still developing your mappings. The view will perform transformations on the input document using whatever transformations you have implemented in your mapping file, even if you have not yet saved the changes to the mapping file. This allows you to ensure that the transforms you create have the desired result before committing changes to your mapping file. To open the Test Map view, use the Test Map action (

開發地圖編輯器_使用地圖編輯器開發地圖

) on the mapping editor toolbar.

While using the Test Map view, you can refresh the output at anytime by clicking the Run transformations button on the Test Map view toolbar. You can also make updates to the input XML file from the Test Map view and can save those changes by clicking the Save selected input file toolbar button. Changes to input XML files can also be committed to a new input file by using the Save selected input file as toolbar button.

The Test Map view provides two methods for viewing the input and output XML. The visual tab represents the XML in a tree format allowing you to navigate and edit the values easily. The visual tab also provides some validation, such as verifying whether the value assigned to a simple type is valid.

Testing maps using the Integration Test Client

In addition to the Test Map view, you can also test a completed XML Map using the Integration Test Client. You can save tests that you create in the test client and re-run them multiple times. You can also debug mapping transformations while testing with the Integration Test Client.

To test a map file using the integration test client, right-click on an XML map file in the Business Integration view and select Test . You can also select an XSL Transformation node in a mediation flow and right-click to invoke the Test XML Map action as well.

To debug a map while using the integration test client, select the Stop for debug before transformations checkbox in the Invoke XML Map Event detailed properties before running the event. You can also set breakpoints on the transformations in the Mapping Editor by right-clicking the transformation and selecting an action from the "Debug" menu.

Testing maps on the server

When using the above methods of testing, the maps are being tested locally in the tooling. There may be cases where the map executes successfully in the tooling, but behaves incorrectly at runtime while running on the server. In these cases, you will need to debug the problem on the server.

小費

While testing a component, if you want to use input data that you have already created in one of your maps input files, you can do so by making use of the XML editor tab or the value pool. To make use of the XML editor tab, copy portions of the raw XML from one of your map input files and then paste it into the XML editor tab on the Integration Test Client.

To make use of a value pool, when you are locally testing an individual map in the integration test client, you can populate the value editor using data from one of the map's input files. Once the data is visible in the value editor, you can save individual values to a data pool.

After saving a value to a data pool, it will be available when you later run a component test to test your entire mediation flow or business process flow. For example, if you have created an array of sample inputs in one of your input XML files, you can save the array to the data pool and use it as an input for a component test.

Testing an XML map on the server is accomplished by testing the component that contains the XML Map to be tested. In the Assembly Editor, right-click the mediation flow or business process flow component that contains the map to be tested and invoke the Integration Test Client using Test Component or Test Component in Isolation.

To understand how an XSL Transformation primitive in a mediation flow is executed at runtime, consider the following scenario shown in Figure 14 .

Figure 14. XSLT primitive
開發地圖編輯器_使用地圖編輯器開發地圖
  1. SMO input object is received by the "SortDestinations" XSL Transformation primitive.
  2. SMO input is serialized into XML for processing.
  3. XML input is transformed using the XSL file associated with the primitive and an XML output is produced.
  4. Output XML is de-serialized into the SMO output object.

Fine-grained trace in the Integration Test Client will allow you to see the inputs and outputs to each XSL Transformation primitive in the mediation flow. In most cases, if an XML map works during local testing and fails at runtime, the problem is in Step 4 from above, the conversion of the output XML to the SMO object. This is the point where an inconsistency between the output XML and the schema will cause a failure. To help diagnose and fix these types of problems, you can turn on server tracing to allow you to examine the messages.

Note : Currently, fine-grained trace is only available within a Mediation Flow component and is not available in a Business Process Flow.

To turn on server tracing:

  1. In the Servers view, while the server is running, right-click the applicable server and select Administration > Run administrative console .
  2. Log-in to the administrative console (

    admin

    is the default ID and password).
  3. In the Troubleshooting section, select Logs and Trace .
  4. In the Logging and Tracing area, click the server name, such as server1.
  5. Click Change Log Detail Levels .
  6. In the Change Log Details Level entry field, add the following text to whatever text already exists in the entry field:

    : com.ibm.ws.sibx.*=all

    .

    For example, if the entry field contains this before making a change:

    *=info

    .

    The entry field will look like this after the changes:

    *=info: com.ibm.ws.sibx.*=all

    .
  7. 單擊确定 。
  8. Click Save to save changes and promote the configuration changes to the server.
  9. Logout of the administration console.
  10. Restart the server.

Once the server tracing has been enabled, rerun your test and then review the trace.log file. To determine the location of the trace.log file:

  1. Open the Server Logs view.
  2. In the Server Logs view toolbar, click the Load server console or log button.
  3. Select Load from server log directory .
  4. Click Browse .
  5. Check the checkbox for the trace.log file and click OK .
  6. Note the location of the trace.log file and click Cancel .

    Note : Although you can use the viewer to view the trace.log file, finding the required trace information is easier when using a text file editor to view the log file.

When you open the trace.log file, you can locate serialized SMO instances by searching for the string:

<smo

.

You can also view the raw XML data before and after a transformation primitive by looking for the following strings:

Serialized DataObject prior to transformation

and

Result of transformation

.

Another option for debugging is to set a breakpoint on the XSL transformation primitive in the Mediation Flow Editor. The debugger will allow inspection of SMO values both before and after the XSL transformation primitive at runtime.

最佳實踐

Organizing mappings

When working in the Mapping Editor, there are things you can do to make the maps easier to navigate, understand, and maintain. One technique for implementing a map is to start by looking at the fields in the target one by one. For each field in the target, consider:

  • Does the target field need to be populated? (Is it required?)
  • If the target field needs to be populated, where will the data come from?
  • If the target is going to be populated from data that is in the source XML, where in the source is the data located?
  • Is the data source of the same type as the target data type? If the types are the same, and the data does not need to be modified in any way, consider using a Move transform.
  • If the data from the source and the target are not the same type or manipulation of the data is required, consider using a Local map or a Submap to map the source and target. By using a Local map or a Submap, you can nest the details of the mapping inside a child transform, leaving less clutter and confusion at the root level of your mapping file.
  • Will there be a future requirement to map the source and target types in another mapping or multiple times in the current mapping? If you determine that these types are frequently mapped together, consider using a Submap which is re-usable. Otherwise, a Local map is the way to go.
  • If there are multiple source inputs required to populate a single target, consider using a Merge which is a specialized Local map that accepts multiple inputs.
  • Within a Submap or Local map, do many of the source and target fields have the same name and types? If there is a lot of similarity between the source and target, consider using Match Mapping to map the similar fields as a starting point for further customizations.
  • For complex types that are the target of a mapping, ensure that none of the target types' children are targets of any other mappings other than those that are defined within a container mapping on the parent. For example, suppose that you wanted to copy all the context information from the source to the target, but you also wanted to add something from the body to the context or correlation as well. The correct way to accomplish this is shown in Figure 15 .
    Figure 15. Correct way to handle nested mappings
    開發地圖編輯器_使用地圖編輯器開發地圖
    Figure 16 is incorrect because there is a Move on context and another Move to contextor correlation.
    Figure 16. Incorrect way to handle nested mappings
    開發地圖編輯器_使用地圖編輯器開發地圖
    Figure 17 is also wrong because the Move to context or correlation is not within the Local map defined on context.
    Figure 17. Incorrect way to handle nested mappings
    開發地圖編輯器_使用地圖編輯器開發地圖
    Even if the Local map on context does not map the correlation element, the Move is still wrong, because it must be within the Local map.

Once you have determined the best type of mapping for each target field at the root level, you can use the same process to create mappings within the Local map, Merge, and Submap transforms that you have created.

While organizing mappings, the following actions might be helpful:

  • Cut, copy, and paste : See Moving mappings for more information.
  • Refactor to submap : If you already created a Local map and later decide it is better as a reusable submap, right-click the existing local map and invoke the Refactor to submap action.

Migrating maps to Integration Developer

In most cases, a .map file that was created in a previous version of Integration Developer will work correctly in the new versions of Integration Developer. In some cases, a warning message may appear to indicate that the XSL file associated with a given map was generated using an earlier version. In those cases, we recommend to regenerate the associated XSL file. The XSL can be regenerated using the quick fix associated with the warning message in the Problems view, or also when the .map file is open in the Mapping Editor by using the "Generate XSLT script" button on the Mapping Editor local toolbar.

One exception to the general migration rule is that the format of XML maps changed significantly between Integration Developer V6.0.2 and Integration Developer V6.1. A tool that migrates Integration Developer V6.0.2 XML mapping files (.xmx) to the newer Integration Developer V6.1.2 and later format (.map) is provided in Integration Developer. After importing projects containing *.xmx files, you will receive warnings in the Problems view. To launch the migration tool, right-click on a warning message and select Quick Fix , or try to open a *.xmx file in Integration Developer V6.1.2 or later.

Once you have completed the auto migration, you will still need to test your migrated map file to ensure that it produces the desired results. In V6.0.2, empty elements were created for required elements in the target XML even if those elements were not mapped in the mapping. However, in V6.1.2 and later, these empty elements are no longer created unless you explicitly create a mapping. Because of this and other differences in the two versions, we recommend that you test the maps after migration.

Part 2 of this series, Working with complex XML structures in the Mapping Editor , explains more advanced XML mapping topics.

結論

In this article, you learned how to create, build, and test XML maps using WebSphere Integration Developer V7, as well as how to create simple mappings using a variety of refinements available within the XML Mapping editor. You now have enough skills to design, develop, and deploy XML maps within WebSphere Integration Developer.

翻譯自: https://www.ibm.com/developerworks/websphere/library/techarticles/1003_spriet1/1003_spriet1.html