The below is used XSLT and input/output XML. The output XML contains the empty CDATA elements. How to prevent adding it without excluding from cdata-section-elements? XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output cdata-section-elements="first second" indent="yes"/>
<xsl:strip-space elements="first second"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Input
<?xml version="1.0" encoding="UTF-8"?>
<top>
<first>
<second/>
</first>
<first>
<second><![CDATA[! Please note...]]></second>
</first>
</top>
Output with strip-space
<?xml version="1.0" encoding="UTF-8"?>
<top>
<first>
<second/>
</first>
<first>
<second><![CDATA[! Please note...]]></second>
</first>
</top>
Output without strip-space
<?xml version="1.0" encoding="UTF-8"?>
<top>
<first><![CDATA[
]]><second/><![CDATA[
]]></first>
<first><![CDATA[
]]><second><![CDATA[! Please note...]]></second><![CDATA[
]]></first>
</top>
firstelement designated as a CDATA section element and it has a child element?firstand thesecondelements may have a CDATA section. The above identity template works as expected iffirstorsecondcontains CDATA. Also input XML may have nestedsecondin thefirstwithout CDATA. I expected the same output as an input while applying identity template.xsl:output cdata-section-elements. But that is a result tree serialization step that then serializes the text nodes as CDATA section, it does not depend on the input markup format.