Removing Empty Nodes from a Message using XSLT and the BizTalk Mapper
When you work with BizTalk, it seems that eventually you’ll do everything. On a typical BizTalk engagement I’ve found that I also do quite a bit of .NET and SQL development as well. Once in a while, I even write some XSLT.
On a recent project, I’ve found the need to do go through an Xml Document (as a BizTalk message) and remove empty nodes before passing it on to another process. In this particular case, we were writing data to an Oracle table using WCF adapter for Oracle. The problem is that the Oracle Adapter interprets blank fields as empty instead of null. For our purposes, we needed these values to be interpreted as null values and written as such into the Oracle tables. Simply removing empty nodes would meet our needs as all our nullable fields had a minOccurs value of zero (0).
BizTalk maps don’t really give us the power to remove nodes, at least not directly. Usually, if there’s a corresponding node on the left side of the mapper (null or otherwise), one will be created in the output document.
The work-around was straightforward.
First, create an XSLT document. The code below will work for ALL Xml:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:if test=". != ">
This stylesheet works simply by moving through every node in a document, elements and attributes included, and only copying the nodes that contain values into the output.
Next, you’ll need to wire the XSLT into a BizTalk map. Create a map with the input and outputs of the correct schema type (here they’re the same for both). Then, without creating any links between the sides, assign the XSLT stylesheet. You do this by clicking in the grid area of the map and setting the "Custom XSL Path" property to the location of a stylesheet containing the above code. When this map executes, the custom XSLT will run and output only nodes containing values.
Once again, you will need to make sure that your schema allows the empty nodes to be missing!