共用方式為


在訊息指派中使用 XPath

您可以使用 xpath 函式將 XPath 值指派給訊息部分,或將某個值指派給參考訊息部分的 XPath。 如需將訊息和訊息部分進行指派的詳細資訊,請參閱 建構訊息

備註

如需 xpath 函式的詳細資訊,請參閱 XML 路徑語言 (XPath) 的第三方檔。

備註

xpath 函式的使用不限於訊息指派。 您也可以在任何表示式中使用,例如:

If ((System.Double) xpath(_RequestMessage.part, "number(//book[last()]/price)") == 75.00 && (System.Boolean) xpath(msgBoolean, "string(//boolean)") == false)...  

備註

如果您想要將值指派給字串,請使用 XPath string() 函式。 例如:

myString = xpath(msg, "string(/*/book[1]/title)");  

備註

引擎不是架構感知,因此您只能讀取或寫入包含訊息中存在節點的值(必須存在完整路徑),否則引擎將引發例外狀況。 即使您提供預設值,也是如此。

將 XPath 指派到訊息元件中

請考慮下列架構:

<?xml version="1.0" encoding="utf-16"?>  
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  <xs:element name="catalog">  
    <xs:complexType>  
      <xs:sequence>  
        <xs:element minOccurs="1" maxOccurs="unbounded" name="book">  
          <xs:complexType>  
            <xs:sequence>  
              <xs:element name="title" type="xs:string" />  
              <xs:element name="author">  
                <xs:complexType>  
                  <xs:sequence>  
                    <xs:element name="FirstName" type="xs:string" />  
                    <xs:element name="LastName" type="xs:string" />  
                  </xs:sequence>  
                </xs:complexType>  
              </xs:element>  
              <xs:element name="price" type="xs:string" />  
            </xs:sequence>  
            <xs:attribute name="country" type="xs:string" />  
          </xs:complexType>  
        </xs:element>  
      </xs:sequence>  
    </xs:complexType>  
  </xs:element>  
</xs:schema>  

您可以使用 函式,如下所示,在該架構類型的檔案實例上設定值:

//assumes that a message named _ResponseMessage is already constructed  
_ResponseMessage.part = _RequestMessage.part;  
xpath(_ResponseMessage.part, "/*/book[1]/@country") = "USA";  
xpath(_ResponseMessage.part, "/*/book[1]/title") = "Legends";  
xpath(_ResponseMessage.part, "/*/book[1]/author/FirstName") = "A";  
xpath(_ResponseMessage.part, "/*/book[1]/author/LastName") = "B";  
xpath(_ResponseMessage.part, "/*/book[1]/price") = 50;  

從 XPath 指派給訊息元件

//assumes that a message named objMessage is already constructed  
objMessage.BooleanPart = xpath("false()");  
objMessage.IntPart = xpath("100");  
objMessage.StringPart = xpath("'Hello'");  
objMessage.StringPart2 = xpath("'World'");  

使用 XPath 從節點和節點集指派

您也可以使用 XPath 將 XML 節點和節點集指派給 XML 元素、類別或架構型或類別型訊息。

假設您有一個名為 Book 的 XML 可串行化類別,並考慮下列範例:

[Serializable]  
Class Book {...}  

範例一 - 從目錄選取第四個 book 元素,並將它指派給 XML 元素變數:

myXmlElement = xpath(myMsg, "/catalog/book[3]");  

範例二 — 從目錄中選取第四個 book 元素,並使用 XML 還原串行化將其轉換成 Book 類別實例:

myBook = xpath(myMsg, "/catalog/book[3]");  

範例三 — 從目錄選取第四個書籍元素,並將它轉換成 Book 類型的訊息:

myBookMsg = xpath(myMsg, "/catalog/book[3]");  

範例四 : 選取目錄中的所有書籍元素,其中 MyMethod 會採用 XmlNodeSet 做為參數:

MyMethod(xpath(myMsg, "/catalog/book"));  

範例五 — 將 book 元素新增至 “BookOfTheMonth” 容器:

xpath(MyMsg2, "/RecommendedBooks/BookOfTheMonth") = myBook;  

範例六 — 將所有定價為二十以下的書籍新增至一組建議書籍:

xpath(MyMsg2, "/RecommendedBooks/BestPriceBooks") = xpath(MyMsg, "/catalog/book[@price <= 20]");  

範例七 - 呼叫傳回 XML 元素的使用者程式代碼:

xpath(MyMsg2, "/RecommendedBooks/AdvertisedByPartner") = GetPartnerAdvertisedBook();  

套用範例五和七之前:

<RecommendedBooks>  
       <BookOfTheMonth/>  
       <BestPriceBooks/>  
       <AdvertisedByPartner/>  
</RecommendedBooks>  

套用範例 5 和 7 之後:

<RecommendedBooks>  
       <BookOfTheMonth>  
              <Book country="USA">  
                     <title>McSharry</title>  
                     <author>  
                            <FirstName>Nancy</FirstName>  
                            <LastName>Jensen</LastName>  
                     </author>  
              </Book>  
       </BookOfTheMonth>  
       <BestPriceBooks/>  
       <AdvertisedByPartner>  
              <Book country="USA">  
                     <title>The Rooster</title>  
                     <author>  
                            <FirstName>Mindy</FirstName>  
                            <LastName>Martin</LastName>  
                     </author>  
              </Book>  
       </AdvertisedByPartner>  
</RecommendedBooks>