June 25, 2009

Check for and remove empty nodes in XSLT

On one of our projects, we use AIA canonicals extensively, and for those familiar with them, their definition is quite large. At times, our payloads were over 1.5MB because all elements, including empty ones, were mapped to the final message.

One way to work around this is to create a transformation to remove the empty nodes.

For example, your process may have 5 input elements and 5 result elements. Your transformation may merely copy each input element to it's corresponding output element as shown here:
    <xsl:template match="/">
    <xsl:value-of select="/ns1:EmptyNodesTestProcessRequest/ns1:input1"/>
    <xsl:value-of select="/ns1:EmptyNodesTestProcessRequest/ns1:input2"/>
    <xsl:value-of select="/ns1:EmptyNodesTestProcessRequest/ns1:input3"/>
    <xsl:value-of select="/ns1:EmptyNodesTestProcessRequest/ns1:input4"/>
    <xsl:value-of select="/ns1:EmptyNodesTestProcessRequest/ns1:input5"/>
However, during execution, if an input element is empty or non-existent, it will still map as 'blank' to the output element. For example, if there is no data for input3, the result is still returned as follows:
As XSDs are larger in size, this becomes more of a size concern.

To work around this, create a transformation that has a source and target variable of your output variable. In this, add the following:
    <xsl:template match="@*|node()">
    <xsl:if test=". != '' or ./@* != ''">
    <xsl:apply-templates select="@*|node()" />
As you can see from the execution below, the first transformation essentially copied all input elements to their corresponding output result element, including the blank element result3. However, by applying a second transformation, all empty nodes are removed.