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">
  <ns0:Task>
  <ns0:CallID>CallID_0</ns0:CallID>
  <ns0:Number>125</ns0:Number>
  <ns0:Status>
  <ns0:Name>Name_0</ns0:Name>
  </ns0:Status>
  <ns0:Customer>Customer_0</ns0:Customer>
  <ns0:WorkOrderNumber>WorkOrderNumber_0</ns0:WorkOrderNumber>
  <ns0:RequiredSkills1>Skill</ns0:RequiredSkills1>
  </ns0:Task>
  </ns0:SXPTaskUpdate>

 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"'.

No comments:

Post a Comment