Professional Documents
Culture Documents
Using XPaths in Message Assignment
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:
Note If you want to assign a value to a string, use the XPath string() function. For
example:
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.
construct objMessage
{
objMessage.BooleanPart = xpath("false()");
objMessage.IntPart = xpath("100");
objMessage.StringPart = xpath("'Hello'");
objMessage.StringPart2 = xpath("'World'");
}
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:
[Serializable]
Class Book {...}
Example One — select the fourth book element from the catalog, and assign it to an XML
element variable:
Example Two — select the fourth book element from the catalog, and convert it using XML
deserialization into a Book class instance:
Example Three — select the fourth book element from the catalog, and convert it a message
of type Book:
MyMethod(xpath(myMsg, "/catalog/book"));
Example Six — add all books that are priced at twenty or less to a set of recommended
books:
<RecommendedBooks>
<BookOfTheMonth/>
<BestPriceBooks/>
<AdvertisedByPartner/>
</RecommendedBooks>
<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>