【问题标题】:How to remove CDATA without removing the element which doesn't contain CDATA?如何在不删除不包含 CDATA 的元素的情况下删除 CDATA?
【发布时间】:2015-12-11 14:15:37
【问题描述】:

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

【问题讨论】:

    标签: xslt cdata


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多