我想删除仅包含空格/换行符/制表符的标签,如下所示:
<p> </p>
您将如何使用 xpath 函数和 xslt 模板来做到这一点?
这种转变(覆盖身份规则 http://dpawson.co.uk/xsl/sect2/identity.html):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(*) and not(text()[normalize-space()])]"/>
</xsl:stylesheet>
当应用于以下 XML 文档时:
<t>
<a>
<b>
<c/>
</b>
</a>
<p></p>
<p> </p>
<p>Text</p>
</t>
正确地产生想要的结果:
<t>
<a>
<b/>
</a>
<p>Text</p>
</t>
Remember:使用和覆盖身份规则/模板是最基本、最强大的 XSLT 设计模式。对于大多数节点需要原封不动地复制,而只有一些特定节点需要更改、删除、重命名等的各种问题来说,它是正确的选择。
Note:@Abel 在他的评论中建议该解决方案的某些部分需要进一步解释:
对于不熟悉或好奇的人:not(*)
意思是:没有孩子
元素;not(text()[normalize-space()])
意思是:没有文本节点
与非纯空白文本。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)