Monday, November 2, 2009

Using xpath queries in BizTalk

We commonly use Xpath query to retrieve values within BizTalk. The few commonly used xpath queries in BizTalk are explained below. I am using the following XML to explain the xpath queries.



1) Retrieve a node with an attribute "@FieldName=EmailAddress" using xpath.

xpath(Message,"/*[local-name()='Line' and namespace-uri
()='http://www.alpha-helix.org.uk/eCommerce/OrderLine/v1']/*[local-name()='Optional' and namespace-uri()='http://www.alpha-helix.org.uk/eCommerce/SalesLine/v1']/*[local-name()='Data' and @FieldName='EmailAddress']");

2) Retrieve attribute "@Value" from the message with "@FieldName"attribute and value "EmailAddress" using xpath.

string EmailAddress = xpath(Message,"string(/*[local-name()='Line' and namespace-uri
()='http://www.alpha-helix.org.uk/eCommerce/OrderLine/v1']/*[local-name()='Optional' and namespace-uri()='http://www.alpha-helix.org.uk/eCommerce/SalesLine/v1']/*[local-name()='Data' and @FieldName='EmailAddress']/@Value)");

3) Retrieve the total count of node "Data" using xpath.

int x= xpath(Message,"number(count(/*[local-name()='Line' and namespace-uri()='http://www.alpha-helix.org.uk/eCommerce/OrderLine/v1']/*[local-name()='Optional' and namespace-uri()='http://www.alpha-helix.org.uk/eCommerce/SalesLine/v1']/*[local-name()='Data' and namespace-uri()='http://www.alpha-helix.org.uk/eCommerce/OrderLine/v1']))");

4) Retrieving a single node from the node collection.

intLoopCount = intLoopCount+1; // position of the node in the list. A looping shape could be used to loop around each node in message.

string strExtractPath = System.String.Format("/*[local-name()='Line' and namespace-uri()='http://www.alpha-helix.org.uk/eCommerce/OrderLine/v1']/*[local-name()='Optional' and namespace-uri()='http://www.alpha-helix.org.uk/eCommerce/SalesLine/v1']/*[local-name()='Data' and namespace-uri()='http://www.alpha-helix.org.uk/eCommerce/OrderLine/v1' and position()={0}]",intLoopCount);

ExtractMessage = xpath(Message,strExtractPath);

where intLoopCount, is the position of the node "Data" in the above message. ExtractMessage will hold the node/message-part extracted from the actual message.

5) Retrieving a single attribute value from the message.

string fieldName = xpath(ExtractMessage,"string(//@FieldName)");

This will return single value if the attribute "@FieldName" is not repeated within the ExtractMessage.


Post any comments if you have. Good Luck!.