Skip to Content
Technical Articles
Author's profile photo Vikas Kumar Singh

B2B_SEG_COUNTER- Now Count your EDI Segments on the Cloud!

Recently, I got the chance to work with B2B integrations using SAP CPI and CPI is no more amateur when it comes to EDIs.

SAP CPI Enterprise License comes with its Integration Content Advisor which is still not so easy to license 🙂 , but Gunther Stuhec blogs on MIGs and ICA will get you started once you have procured the license. If you have worked with EDIs on SAP PO B2B add-on , you would have understood the context by the title now, but there are good chances that a lot of folks reading this are not only new to CPI but also to EDI or never have been to SAP PO.

The trailer segment of an EDI file contains the count of total segment in the EDI file. This was handled using a variable $B2B_SEG_COUNTER in SAP PO. Some links on the same topic are as below, this can be also be found in pre-packaged mapping delivered with SAP B2B add-on. The converter module automatically populates the count to this variable. Few links talking about the same(though no standard documentation)—hierarchical-and-parent-id-number.html


But, surprisingly I have not found anything to achieve this pre-delivered with SAP Cloud Platform Integration. And thus I am making an attempt to help fellow community members to overcome this quickly if they come across this situation.

A standard Integration Flow for EDI XML to EDI looks as below






with Message Implementation Guideline , SAP ships the XSD (schema) for the EDI files along with pre-processing and post-processing XSLT. I was very hopeful to have this feature shipped with the post-processing XSLT, but it is not there YET. 

In a typical EDI iFlow in CPI in case of EDIFACT the schema contains UNH to UNT and envelope is added at the end using a content modifier. In case of ANSI X12 the schema from MIG contains ST to SE and the envelope is added at the end with a content modifier similarly. So just a XSL code snippet to count all the nodes starting with S_ , would do the trick


UPDATE: The code should allow developers to also map any constant value or value of their choice thus updating the code to allow the flexibility.

Map the field with constant(“B2B_SEG_COUNTER”), the xslt checks if value within tag is B2B_SEG_COUNTER it applies the count of all segments else keeps the value of xml intact.


<xsl:stylesheet version="1.0"

 <xsl:template match="node()|@*">
       <xsl:apply-templates select="node()|@*"/>

<xsl:template match="//S_UNT/D_0074/text()[.='B2B_SEG_COUNTER'] | //S_SE/D_96/text()[.='B2B_SEG_COUNTER']">
  <xsl:value-of select="count(//*[starts-with(name(), 'S_')])"/>


But, why have another XSLT map when this could be done with the post-processing XSLT shipped by MIG from Integration Content Advisor, So I just appended the code snippet at the end of the SAP shipped post-processing XSLT to solve the problem




I am sure sooner than later SAP will ship this along with lot more necessary and logical features with the improved Integration Content Advisor and the dream TPM 🙂


Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo George Knoske
      George Knoske

      Thank you.  This was a helpful solution.

      Author's profile photo Vikas Kumar Singh
      Vikas Kumar Singh
      Blog Post Author

      Glad to see this

      Author's profile photo Fatih Pense
      Fatih Pense

      Hello Vikas,
      Thanks for sharing the useful XSLT!
      I shared the Groovy version here and added a link to your blog post.