Sammy
Sammy

Reputation: 121

How to remove CDATA without removing the element which doesn't contain CDATA?

While removing CDATA with the below XSLT some element is getting removed where CDATA is not present.

Can someone throw some light on the code? Where I am making mistake. Thanks.

Input:

<?xml version="1.0" encoding="UTF-8"?>
<response status="200">
    <CrsCreateCourseExpResponse>
        <pCategoryOut>
            <![CDATA[<XX_IL_OLM_CRS_CAT_TAB_OBJ>Y<XX_IL_OLM_CRS_CAT_TAB_OBJ>]]>
        </pCategoryOut>
        <pLearnerAccessOut>
            <![CDATA[<XX_IL_OLM_LRNR_ACC_TAB_OBJ><P_OLM_LRNR_ACC_ERRORS>N</P_OLM_LRNR_ACC_ERRORS></XX_IL_OLM_LRNR_ACC_TAB_OBJ>]]>
        </pLearnerAccessOut>
        <pActivityVersionId>42002</pActivityVersionId>
        <pOvn>1</pOvn>
        <pErrorCode>0</pErrorCode>
        <pErrorMsg>success</pErrorMsg>
    </CrsCreateCourseExpResponse>
</response>

Output:

<?xml version="1.0"?>
<response status="200">
    <XX_IL_OLM_CRS_CAT_TAB_OBJ>Y<XX_IL_OLM_CRS_CAT_TAB_OBJ>
            <XX_IL_OLM_LRNR_ACC_TAB_OBJ>
                <P_OLM_LRNR_ACC_ERRORS>N</P_OLM_LRNR_ACC_ERRORS>
            </XX_IL_OLM_LRNR_ACC_TAB_OBJ>
            4200210success
            </response>

Desired output:

<?xml version="1.0" encoding="UTF-8"?>
<response status="200">
    <CrsCreateCourseExpResponse>
        <pCategoryOut>
            <XX_IL_OLM_CRS_CAT_TAB_OBJ>Y<XX_IL_OLM_CRS_CAT_TAB_OBJ>
        </pCategoryOut>
        <pLearnerAccessOut>
            <XX_IL_OLM_LRNR_ACC_TAB_OBJ><P_OLM_LRNR_ACC_ERRORS>N</P_OLM_LRNR_ACC_ERRORS></XX_IL_OLM_LRNR_ACC_TAB_OBJ>
        </pLearnerAccessOut>
        <pActivityVersionId>42002</pActivityVersionId>
        <pOvn>1</pOvn>
        <pErrorCode>0</pErrorCode>
        <pErrorMsg>success</pErrorMsg>
    </CrsCreateCourseExpResponse>
</response>

XSLT I'm using:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="name/text()">
    <xsl:value-of select="." disable-output-escaping="yes" />
    <Language>English</Language>
</xsl:template>

<xsl:strip-space elements="*"/>

<xsl:template match="*">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
        <xsl:value-of select="." disable-output-escaping="yes"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Views: 3690

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

I don't see any name element in the input so the use of <xsl:template match="name/text()"> is not clear to me, however instead of the template <xsl:template match="*"> you could simply use <xsl:template match="text()"><xsl:value-of select="." disable-output-escaping="yes"/></xsl:template> to make sure disable-output-escaping is applied when copying all text nodes, if you don't need it for all of them then restrict it to e.g. <xsl:template match="pCategoryOut/text() | pLearnerAccessOut/text()"><xsl:value-of select="." disable-output-escaping="yes"/></xsl:template>. Then remove the template match="*", the first template, the identity transformation template, will take care of copying elements.

Upvotes: 1

Related Questions