BizTalk Debatching

I just want to apologize for not posting in a very long time. I was recently in Seattle, WA for business. But I am back now and ready to start talking technology. One topic that got me flustered when learning BizTalk was de-batching. I will show you how to set up debatching in a BizTalk Solution. Just to be clear I am assuming some BizTalk experience such as dealing with schemas, Visual Studio and the BizTalk Administration Console.

First a little definition of debatching in my own words. For any body that doesn't know, BizTalk loves XML and for this example we will be using XML. Many times in BizTalk you will receive messages containing other messages. A good example is a PO or Purchase Order. Instead of sending 100 separate messages, or files, to a vendor for orders, a company will put all of those orders, or in this case POs, in one large PO. The figure below shows a small example of this. Here you can see that the PO is the root node and has a Header tag that contains information about the company or entity making the order. What we want to focus on is the Orders node of the XML. This is where debatching comes into play. Inside the Orders node you will see three child Order nodes. These are the separate orders being placed by the ordering company or entity. These orders will need to be separated and processed individually. You may be thinking, "But how? They are in the same XML file!". This is where the magic of debatching comes in.

 Figure 1.

As you know when taking in a file and processing it you must have a schema for BizTalk to match that file to. My question is how many schemas do you think we need for de-batching? If you answered two you are correct! The first schema is what is called the Envelope. This envelope will strip off certain nodes or data in the XML file and allow you to read what is inside of that envelope. This is much like taking a letter out of an envelope and reading the letter. The second schema will define what you want to read. In this case it will be the order node. Now just to be clear, you do not need a schema for each Order node. You will see why later. So based off of the above XML file we can create two schemas that will create the de-batching solution.

First is the Envelope schema. The figure below shows what we need to create the Envelope that will strip allow BizTalk to remove what we want and look at the node that contains our Order nodes.

Take note of the Orders Element and how it contains a Child Element call <Any>. This Child Element will allow us to not have to create any explicit schema in the Envelope until we create the Document Schema. Just a side note, you can use the <Any> Element where ever you please. I could have used the <Any> Element to replace the street, city, state and zip. You will have to evaluate your needs and apply the <Any> Element appropriately. There are also two Properties in Visual Studio we need to set. Click on Schema at the top of the Envelope Schema and go to properties to set the Envelope to Yes. This one is relatively obvious as to why we have to set this property.

Lastly we need to set the Body XPath to point to the Orders Node. Click on PO in the Envelope Schema and in the Properties set the Body XPath to the Orders node. A window will pop up when you click the ellipsis to the left and all you have to do is drill down and highlight the Orders node and click OK.

 Below is the Document Schema.

The Document schema will allow BizTalk to recognize and parse those Order nodes that have been exposed by removing the nodes defined in the Envelope Schema. Again you see we have the <Any> Element. I was simply lazy and did not want to explicitly define the schema. Explicitly defining the schema will only allow you to read one structure of message. The <Any> Element allows BizTalk to read anything inside the Order node whether it be elements, records or attributes. You will have to decide for your solution what will best fit your needs in your environment. One major property you need to set in the Document schema is the Max Occurs. By default it is set to 1 meaning there can only be one order inside the Document schema and you know that there are more. Either type in Unbounded or *. In the simplest explanation this will tell BizTalk what do to with each order it encounters and match in to the Document schema.

All that's left to do is deploy to BizTalk and create a Receive Location and a Send Port, point them to two test folders on your computer and drop and file in the Receive Location folder. You can recreate the file to be dropped off of the first picture at the top of the PO file. With that exact file you should get three separate files from the one in the out folder(Send Port file location). The output will give you three files. Just so everyone know I am going to do an advanced de-batching post sometime soon.

Well thanks again for tuning in. If you have any questions please comment and I will answer the best I can. See you soon!