BizTalk: Generating Flat File Schema for File with Tag Identifiers not at the Beginning, Grouping Data by Element and Debatching

BizTalk: Generating Flat File Schema for File with Tag Identifiers not at the Beginning, Grouping Data by Element and Debatching


Recently there was a question posted in BizTalk forum, where an user wanted to:
  • Create a flat file schema for delimited file with tag identifiers for header and detail which is not at the beginning of the record.
  • Group the header and detail based on a date field.
  • Debatch the grouped record by date.
This is an interesting question, which is worth an article by itself rather than a post in forum. In real life there a similar requirements for a complex flat file that needs to be debatched by group.


Question asked the forum will be described as follows:

An enterprise receives a flat file which looks something like this:


This is a delimited flat file with tag identifier for header and detail. As you can see, the tag identifiers are not at the beginning of the data for a delimited record. And questioner wants to debatch the file by date.

Figure 1.
Flat File Structure.
I assume the readers for this article are familiar with the basics of BizTalk, Flat file schema processing and debatching in BizTalk. For more basic information about these concepts, readers can refer the reference section of this article.

Flat File Schema for a file with tag identifiers not at the beginning of the record

In the EDI world it is a common practice to precede the tag identifier with batch related information rather than to start with it. Unfortunately however, the BizTalk’s Flat-file schema wizard, doesn’t parse the flat file that way. It expects the tag identifier to be at the beginning of the record. If that is not the case than you will get an error as below:

Figure 2. Flat File schema wizard error for tag identifier not at the beginning.
A possible approach of handling the flat file schema is to design the structure of the schema matching the given flat file first and then start thinking about matching the flat file to the designed schema structure by setting flat file schema properties like delimiters, type order etc. I found this useful many times as this gives an idea about how the end result (flat file schema) would be.

Following is the schema structure for the above flat file.

Figure 3. Schema Structure.

A way to handle the tag identifier, I prefer Johann Cooper’s suggestion. A generic repeating record to have all the common elements (in this case, just ‘Date’) and choice record node (RecordBatch). Within this choice record, I created separate record for each of the different tag identifiers (‘header’ and ‘detail’) with their respective elements (header children and detail children). Now validating the above flat file instance gave the XML instance like this.

Figure 4. XML instance for the given flat file.

Group By Date

As you can see, the above XML represents the flat file instance, but for debatching it by date, we need to group the record set by date.

Figure 5.
XML Instance to group by date element.

We can use the classic Muenchian Method to handle grouping and sorting. In XSLT, Muenchian method is a way to group and sort elements in a more efficient way using keys.  Keys work by assigning a key value to a node and giving you easy access to that node through the key value.

Since the main objective is to debatch the file, I created an envelope schema to match this XML structure (whose structure is same as flat file schema) and mapped the flat file schema to envelope schema using Muenchian method based on custom XSLT.

Figure 6. Map Flat file schema to envelope schema.

Muenchian method based mapping.

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="" exclude-result-prefixes="msxsl var s0" version="1.0" xmlns:ns0="http://MSDN.TechnetWiki.ComplexFFDebatch.EnvelopeSchemaGrouped" xmlns:s0="http://MSDN.TechnetWiki.ComplexFFDebatch.ComplexFFSchemaUngrouped">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:key use="Date" match="/s0:CompleteBatch/Record" name ="groups"/>
  <xsl:template match="/">
    <xsl:apply-templates select="/s0:CompleteBatch" />
  <xsl:template match="/s0:CompleteBatch">
      <!-- In Muenchian grouping , used count()  instead of generate-id() for better performance -->
      <!--<xsl:for-each select="Record[generate-id(.)=generate-id(key('groups',Date))]">   -->
      <xsl:for-each select="Record[count(.|  key('groups',Date)[1])=1]">       
            <xsl:value-of select="Date/text()" />
            <xsl:for-each select ="key('groups',Date)">
            <xsl:for-each select="RecordBatch/Header">
                  <xsl:value-of select="Header_Child1/text()" />
                  <xsl:value-of select="Header_Child2/text()" />
                  <xsl:value-of select="Header_Child3/text()" />
                  <xsl:value-of select="Header_Child4/text()" />
            <xsl:for-each select="RecordBatch/Detail">
                  <xsl:value-of select="Detail_Child1/text()" />
                  <xsl:value-of select="Detail_Child2/text()" />

After applying the map, the XML instance grouped by date would look like this.

Figure 7. XML Instance grouped by date element.


Now with XML being grouped by date in an envelope schema, it can be easily debatched by calling a receive pipeline in Orchestration.  I have executed the map in orchestration just to have better readability.

Figure 8. 
Orchesttration used to call Receive-Pipeline to debatch the message

Note: Debatching in an orchestration can be useful if you require full control of your debatching process. However keep in mind that with large files that need to be debatched than orchestration is not the preferred way. Better performance can be achieved to detach in a receive port.


The output of the debatching is as follows:

Figure 9. Output-Debatched messages.

As with any problem, there many solutions to handle to solve it. The above method is one of the ways to implement a solution to the problem.  

Source Code

Source Code for this article can be downloaded at the MSDN Code Gallery: FlatFileSchema for file with Tag Identifier not at beginning,Grouping,Debatching.


See Also

Read suggested related topics:

Another important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.
Leave a Comment
  • Please add 3 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
  • Steef-Jan Wiggers edited Revision 25. Comment: Added text

  • Steef-Jan Wiggers edited Revision 24. Comment: Correcting link

  • Steef-Jan Wiggers edited Revision 23. Comment: Editing pictures

  • Steef-Jan Wiggers edited Revision 22. Comment: Minor edit

  • Steef-Jan Wiggers edited Revision 21. Comment: Added See Also and links

  • Steef-Jan Wiggers edited Revision 20. Comment: Edits

  • Maheshkumar S Tiwari edited Revision 1. Comment: Added Tag. Hi Ashwin , good topic for an article ..

Page 1 of 1 (7 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.