2

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>
4
  • Which is the output you expect given that you have the first element designated as a CDATA section element and it has a child element? Commented Oct 11, 2016 at 17:19
  • The above sample reflects real documents structure and content. The first and the second elements may have a CDATA section. The above identity template works as expected if first or second contains CDATA. Also input XML may have nested second in the first without CDATA. I expected the same output as an input while applying identity template. Commented Oct 12, 2016 at 10:11
  • I asked about the expected output. If you think that an identity transformation in XSLT is supposed to preserve CDATA sections, then no, it does not, the XML is parsed by an XML parser into a tree model underlying XSLT/XPath which does simply have text nodes, that input tree is transformed into a result tree where text node children of certain elements can be serialized as CDATA sections if asked for with 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. Commented Oct 12, 2016 at 10:19
  • Sorry for not the clear question and thank you for clarification. I updated the input file and provided output with/without 'strip-space'. The output with strip-space is expected. Commented Oct 12, 2016 at 12:01

2 Answers 2

1

Keyword to the solution is function strip-space. Go ahead with:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output cdata-section-elements="first"/>
    <xsl:strip-space elements="first second"/>
    ...

To be precise there are whitespace-text nodes between these two nodes:

<first>
    <second/>

The CDATA can't ignore these whitespaces, otherwise it would change the content. So you have to command the processor, what to do with these text-nodes.


Second possible solution: You address the whitespace-text via template and remove them:

<xsl:template match="first/text()[not(normalize-space())]"/>
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest you try it this way:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" cdata-section-elements="second"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Although I don't quite see the point of this exercise, as the output is identical to the input - both semantically and lexically (except possibly the amount of indent).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.