首页 文章

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

提问于
浏览
0

在使用以下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>

1 回答

  • 1

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

相关问题