当使用下面的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="*">
您还可以简单地使用它<xsl:template match="text()"><xsl:value-of select="." disable-output-escaping="yes"/></xsl:template>
来确保在复制所有文本节点时应用了disable-output-escaping(如果您不这样做)所有人都需要它,然后将其限制为例如<xsl:template match="pCategoryOut/text() | pLearnerAccessOut/text()"><xsl:value-of select="." disable-output-escaping="yes"/></xsl:template>
。然后删除template match="*"
,第一个模板,身份转换模板,将负责复制元素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句