如何在不删除不包含 CDATA 的元素的情况下删除 CDATA?



使用下面的XSLT删除CDATA时,在不存在CDATA的地方会删除一些元素。

有人能透露一下密码吗?我犯错误的地方。谢谢

输入:

<?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>

输出:

<?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>

期望输出:

<?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:

<?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>

我在输入中没有看到任何name元素,所以我不清楚<xsl:template match="name/text()">的使用,但是,您可以简单地使用<xsl:template match="text()"><xsl:value-of select="." disable-output-escaping="yes"/></xsl:template>来确保在复制所有文本节点时应用禁用输出转义,而不是模板<xsl:template match="*">,如果您不需要所有文本节点都使用它,则将其限制为例如<xsl:template match="pCategoryOut/text() | pLearnerAccessOut/text()"><xsl:value-of select="." disable-output-escaping="yes"/></xsl:template>。然后移除template match="*",第一个模板,身份转换模板,将负责复制元素。

最新更新