Monday, October 21, 2013

XQuery expression to count certain element occurances in OSB

Some times we may need to count the number of elements in an XML document that start with specific characters.

Following examples shows the solution.

Ex:

We have XML as below. And this XML is in a variable say, $varXML

<ns0:Data_Structure Name="Group">
<ns0:Lookup Name="GroupReceiverID"></ns0:Lookup>
<ns0:Lookup Name="GroupSenderID"></ns0:Lookup>
<ns0:Lookup Name="GroupVersionNumber"></ns0:Lookup>
<ns0:Property Name="AckChildInvalid"></ns0:Property>
<ns0:Property Name="GroupAgencyCode"></ns0:Property>
<ns0:Property Name="GroupChildCount"></ns0:Property>
<ns0:Property Name="GroupControlNumber"></ns0:Property>
<ns0:Property Name="GroupDate"></ns0:Property>
<ns0:Property Name="GroupID"></ns0:Property>
<ns0:Property Name="GroupReceiverID"></ns0:Property>
<ns0:Property Name="GroupSenderID"></ns0:Property>
<ns0:Property Name="GroupTime"></ns0:Property>
<ns0:Property Name="GroupTrailerControlNumber"></ns0:Property>
<ns0:Property Name="GroupVersionNumber"></ns0:Property>
<ns0:Property Name="TPName"></ns0:Property>
<ns0:Data_Structure Name="Transaction">
<ns0:Lookup Name="ActionCode"></ns0:Lookup>
<ns0:Lookup Name="TransactionID"></ns0:Lookup>
<ns0:Property Name="ActionCode"></ns0:Property>
<ns0:Property Name="TPName"></ns0:Property>
<ns0:Property Name="TransactionChildCount"></ns0:Property>
<ns0:Property Name="TransactionControlNumber"></ns0:Property>
<ns0:Property Name="TransactionID"></ns0:Property>
<ns0:Property Name="TransactionImplementationReference"></ns0:Property>
<ns0:Property Name="TransactionTrailerControlNumber"></ns0:Property>
</ns0:Data_Structure>
</ns0:Data_Structure>

We need to find the number of elements that start with ns0:L . Here, we basically counting the number of ns0:Lookup elements in the entire XML data.

The XQuery expression to find the count would be as follow.

count($varXML//*[fn:starts-with(name(),'ns0:L')])

Use this expression inside any of the message processing actions of OSB.

No comments:

Post a Comment