I wish to remove all the blank tags from my xml using xslt, except when a tag contains a whitespace.Also there should be no impact on line breaks etc. The xml should be copied as is, except for the blank tags with no value or whitespace.
Source XML
<inRoot>
<t1></t1>
<t2> </t2>
<t3>World</t3>
</inRoot>
Output I want
<inRoot>
<t2> </t2>
<t3>World</t3>
</inRoot>
XSLT I tried is removing t1 but is giving me a blank line in place of t1.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:strip-space elements=""/>
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<xsl:if test=".!=''">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
This code gives me output as
<inRoot>
<t2> </t2>
<t3>World</t3>
</inRoot>
Could anyone please advise why I am getting this extra line for t1.
Regards
Use the identity transformation template (in XSLT 1 and 2) or declare (in XSLT 3) <xsl:mode on-no-match="shallow-copy"/>
plus the template
<xsl:template match="*[not(node())]"/>
to ensure that element nodes not having any contents/any child nodes are not copied through.