如何在XSLT中移除作为参数传入节点的HTML锚点标签
解决方法:递归遍历节点移除
<a>标签,保留其他格式 要实现只移除<a>标签但保留其余HTML结构,需要把原XSL模板改成递归遍历整个节点树的逻辑,对不同节点做针对性处理:
修改后的完整XSL模板
<xsl:template name="remove-a"> <xsl:param name="node"/> <!-- 处理文本节点:直接输出内容 --> <xsl:if test="$node/text()"> <xsl:value-of select="$node/text()"/> </xsl:if> <!-- 遍历所有子元素节点 --> <xsl:for-each select="$node/*"> <xsl:choose> <!-- 遇到<a>标签:只递归处理它的子节点,不输出<a>本身 --> <xsl:when test="local-name() = 'a'"> <xsl:call-template name="remove-a"> <xsl:with-param name="node" select="."/> </xsl:call-template> </xsl:when> <!-- 其他标签:先复制标签(含属性),再递归处理内部子节点 --> <xsl:otherwise> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:call-template name="remove-a"> <xsl:with-param name="node" select="."/> </xsl:call-template> </xsl:copy> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template>
模板调用方式保持不变
<xsl:call-template name="remove-a"> <xsl:with-param name="node" select="tbody/tr[@class = 'ou-text']/td[@class = 'ou-text-2']"/> </xsl:call-template>
效果说明
这个模板会:
- 完整保留
<p>、<td>等所有非<a>标签及其属性 - 移除所有
<a>标签,但保留<a>内部的文本内容 - 递归处理所有嵌套节点,确保深层的
<a>也能被正确移除
用你提供的XML数据测试,最终输出与期望一致:
<p>The college's brand standards for logo treatment, font usage, and color usage, along with templates for PowerPoint presentations, memos, and email signatures. The downloadable Brand Standards Guide is available.</p>
内容的提问来源于stack exchange,提问作者Timothy
相关产品推荐
相关产品推荐

