Pages

Monday, December 10, 2012

XPath Inside Orchestrations in BizTalk


Working with XPath inside Orchestrations is a powerful and simple feature of BizTalk.

The help guide does a good job describing the process (under Using XPath in Message Assignment).

XPath queries can only be done against a Message and the results can be set to a Message, XML Document or other orchestration variables. XPath queries can also be executed against untyped messages. That is, a Message that is of type System.Xml.XmlDocument.

CRITICAL: BizTalk xpath can be used to both read values and set values inside your Message. To set values in message, we need to be inside a Message Construct shape.

Here are some of the things which we can do with xpath and how to do them:

- Set a single values inside a Message using xpath
   xpath(SingleXML, "//LineTotal") = nLineTotal;

- Extract a single piece of data out of a Message
  sCustomer = xpath(InXML,"string(//Customer)");

- Extract a single node out of a large XML Document and assign it to a message or  variable
 sXPath = System.String.Format("//Item[{0}]",nCount);
 xDoc = xpath(InXML, sXPath);

- Count the number of nodes or occurrences of something inside your message
  nNumberItems = System.Convert.ToInt32(xpath(InXML, "count(//Item)"));

A great resource for xpath functions and expressions is the W3Schools.