在消息分配中使用 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]");  

示例 2 - 从目录中选择第四个 book 元素,并使用 XML 反序列化将其转换为 Book 类实例:

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

示例三 - 从目录中选择第四个 book 元素,并将其转换为 Book 类型的消息:

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

示例 4 - 选择目录中的所有书籍元素,其中 MyMethod 将 XmlNodeSet 作为参数:

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

示例 5 - 将 book 元素添加到“BookOfTheMonth”容器:

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

示例 6 - 将所有定价为二十元或以下的书籍添加到推荐书籍集合中:

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

示例 7 - 调用返回 XML 元素的用户代码:

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

在应用示例 5 和 7 之前:

<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>