前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Using XPaths in Message Assignment[转]

Using XPaths in Message Assignment[转]

作者头像
阿新
发布2018-04-12 19:00:45
7010
发布2018-04-12 19:00:45
举报
文章被收录于专栏:c#开发者c#开发者

Microsoft BizTalk Server 2004

Using XPaths in Message Assignment

You can use the xpath function to assign an XPath value to a message part, or to assign a value to an XPath that refers to a message part. For more information on assigning to messages and message parts, see Constructing Messages.

Note  The use of the xpath function is not limited to message assignment. You can also use it in any expression, for example:

Copy Code

代码语言:javascript
复制
If ((System.Double) xpath(_RequestMessage.part, "number(//book[last()]/price)") == 75.00 && (System.Boolean) xpath(msgBoolean, "string(//boolean)") == false)...

Note  If you want to assign a value to a string, use the XPath string() function. For example:

Copy Code

代码语言:javascript
复制
myString = xpath(msg, "string(/*/book[1]/title)");

Note  The engine is not schema-aware, so you can only read values from or write values to a node that exists in the containing message (the complete path must exist), or the engine will raise an exception. This is true even if you supply a default value.

Assigning to an XPath in a message part

Consider the following schema:

Copy Code

代码语言:javascript
复制
<?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>
You can use the XPath function as follows to set values on a document instance of that schema type:
construct _ResponseMessage
{
_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;
}

Assigning to a message part from an XPath

Copy Code

代码语言:javascript
复制
construct objMessage
{
objMessage.BooleanPart = xpath("false()");
objMessage.IntPart = xpath("100");
objMessage.StringPart = xpath("'Hello'");
objMessage.StringPart2 = xpath("'World'");
}

Using XPath to assign from nodes and node sets

You can also use XPath to assign XML nodes and node sets to an XML element, a class, or a schema-based or class-based message.

Suppose you have an XML-serializable class called Book, and consider the following examples:

Copy Code

代码语言:javascript
复制
[Serializable]
Class Book {...}

Example One — select the fourth book element from the catalog, and assign it to an XML element variable:

Copy Code

代码语言:javascript
复制
myXmlElement = xpath(myMsg, "/catalog/book[3]");

Example Two — select the fourth book element from the catalog, and convert it using XML deserialization into a Book class instance:

Copy Code

代码语言:javascript
复制
myBook = xpath(myMsg, "/catalog/book[3]");

Example Three — select the fourth book element from the catalog, and convert it a message of type Book:

Copy Code

代码语言:javascript
复制
myBookMsg = xpath(myMsg, "/catalog/book[3]");

Example Four — select all book elements in the catalog, where MyMethod takes an XmlNodeSet as a parameter:

Copy Code

代码语言:javascript
复制
MyMethod(xpath(myMsg, "/catalog/book"));

Example Five — add a book element to the "BookOfTheMonth" container:

Copy Code

代码语言:javascript
复制
xpath(MyMsg2, "/RecommendedBooks/BookOfTheMonth") = myBook;

Example Six — add all books that are priced at twenty or less to a set of recommended books:

Copy Code

代码语言:javascript
复制
xpath(MyMsg2, "/RecommendedBooks/BestPriceBooks") = xpath(MyMsg, "/catalog/book[@price <= 20]");

Example Seven — call user code that returns an XML element:

Copy Code

代码语言:javascript
复制
xpath(MyMsg2, "/RecommendedBooks/AdvertisedByPartner") = GetPartnerAdvertisedBook();

Before applying examples five and seven:

Copy Code

代码语言:javascript
复制
<RecommendedBooks>
<BookOfTheMonth/>
<BestPriceBooks/>
<AdvertisedByPartner/>
</RecommendedBooks>

After applying examples five and seven:

Copy Code

代码语言:javascript
复制
<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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2007-11-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Assigning to an XPath in a message part
  • Assigning to a message part from an XPath
  • Using XPath to assign from nodes and node sets
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档