Tuesday, January 29, 2013

xpath in expression shapes

 xpath is a notation for returning specific elements found in xml. It is very useful. I will go over a few problems I had getting it to work in Biztalk 2010 and the solutions I came accross.
First of all suppose you have some message in Biztalk,
  <ns0:TaskUpdate xmlns:ns0="TaskUpdateTN">

 and you want to extract a certain element of the xml, say the 'Customer' element.

1) Create a couple of strings in the orchestration, tempStr and customerStr.

2) Use the schema properties to grab the xpath of the element you want,
 /*[local-name()='TaskUpdate' and namespace-uri()='TaskUpdateTN']/*[local-name()='Task']/*[local-name()='Customer']. Notice you dont need every last namespace in the xpath, just enough so it can safely find what you're looking for.

3) Open up an expression shape and type:
 tempStr="string(/*[local-name()='TaskUpdate' and namespace-uri()='TaskUpdateTN']/*[local-name()='Task']/*[local-name()='Customer']/text())";
 customerStr = xpath(Message_1, tempStr);

 And youre done; 'customerStr=Customer_0'. In some cases the 'string()' is not neccesary. The '/text()' however is, or else you would get 'customerStr=<ns0:Customer>Customer_0</ns0:Customer>' instead of just 'customerStr=Customer_0'. If you have a list of Customer elements in your message and you want the 3rd element in that list then your xpath will be '...[local-name()='Customer'][3]/text()'.

 If you want to assign a certain element in the Message to a value, say the 'Customer' element, to "John Doe". Change step 3) above to:

3)Open up an expression shape and type:
 tempStr="/*[local-name()='TaskUpdate' and namespace-uri()='TaskUpdateTN']/*[local-name()='Task']/*[local-name()='Customer']";
 xpath(Message_1, tempStr)="John Doe";

 Here a 'string()' doesnt work. Also an important thing to remember is if Message_1 is from a Message Part then you will have to write 'xpath(Message_1.part1,tempStr)="John Doe"'.