Pages

Wednesday, November 30, 2011

Removing empty nodes from XML using xslt


Removing empty nodes from XML using xslt

The below XSLT can be used to remove the empty nodes from the XML file.

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="node()|SDLT">
<xsl:if test="count(descendant::text()[string-length(normalize-space(.))>0] | @*[string-length(.)>0])">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@*">
<xsl:copy />
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
</xsl:stylesheet>






1 comment:

  1. Good solution.

    It works as per the requirement. For example,

    Source XML



    node1_1_1








    Target XML




    node1_1_1


    ReplyDelete