Example of Xpath Count in BizTalk (and how to check empty/missing data)

One way to check for missing data is to do an xpath count.

The two-step approach (put the xpath in a variable first). Some might find this code cleaner, but you use a variable. Note that you have to use the conversion statement to convert the string result of the xpath to a number, if you need to test that number as I do in the following “if” statement.

 

strCountMessageKeysXPath = "count(//*[local-name()='MessageKey']";

intCountMessageKeys =
System.Convert.ToInt32(
xpath(msgCreateSPServiceOrderAckResp.parameters,strCountMessageKeysXPath)
);

// Yes you can put if statement in expression shapes (but not message assignment shapes) 
if (intCountMessageKeys >= 2) 
{
   strXPath = "//*[local-name()='MessageKey'][2]"; 
   strWebRespMessage2Key = xpath(msgCreateSPServiceOrderAckResp.parameters,"string(" + strXPath + ")"); 
}

 

The one-step approach:

 

strCountMessageKeysXPath = ;

intCountMessageKeys =
System.Convert.ToInt32(
xpath(msgCreateSPServiceOrderAckResp.parameters,"count(//*[local-name()='MessageKey']")
);
// Yes you can put if statement in expression shapes (but not message assignment shapes) 
if (intCountMessageKeys >= 2) 
{
   strXPath = "//*[local-name()='MessageKey'][2]"; 
   strWebRespMessage2Key = xpath(msgCreateSPServiceOrderAckResp.parameters,"string(" + strXPath + ")"); 
}


 

 

NOTE: The reason I have .parameters after the message is that it is a multi-part message.
If you don’t get that correct, you may get the error “The expression you have entered is not valid”, or “An xpath expression must be of the form” if you hover over the error.

 

Uncategorized  

Leave a Reply