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"&gt;
    <xsl:template match="@*|node()">
        <xsl:if test=". != ‘’">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

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!

Advertisements

About Ed Jones

Ed is a .NET and Integration architect for Avanade in the Twin Cities. Contact Ed

5 responses to “Removing Empty Nodes from a Message using XSLT and the BizTalk Mapper”

  1. ali Raza says :

    Solution is working while testing the map, but fail to use at Construct message at Orchestration. Message display usage of Unreconstructed Message

    • Ed Jones says :

      As with all maps, you’ll need to make sure that the output message is created in a construct shape when running inside an orchestration. You’ll also want to be sure that the output from executing this meets requirements for the output schema.

  2. mcsweenus says :

    If you copy the sample above, make sure to change “>” at the end of the first tag to a “>”. I copied this and it took me a couple runs to figure out why it wasn’t working 🙂

  3. Marvin Perkins says :

    Yes, as mcsweenus said, the “>” is actually escaped in the copy/paste text so it shows up as “& gt ;” (without the spaces), but also, the single quotes are hex 91 and 92 rather than the ascii single quote. So, you need to make that correction as well. It works perfectly once you make sure the characters are correct.

Trackbacks / Pingbacks

  1. 2010 in review | Extremely Talented Monkeys - January 8, 2011

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: