
<description> <p> <span> <Font>Hello</Font> </span> World! <a href="/index">Home</a> </p></description>
可以设想任何HTML标签.我不想要所有的标签.我想要的标签是p,i,em,strong,b,ol,ul,li和a.例如,< Font>将被剥离,但< p>和< a>会留下来的.我假设我必须匹配我想要的(并确保没有什么可以匹配其他人),但无法解决如何做到这一点.
有帮助吗?
解决方法 将这些元素列入白名单:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/transform" version="1.0"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="*[not(self::description or self::p or self::i or self::em or self::strong or self::b or self::ol or self::ul or self::li or self::a)]"/></xsl:stylesheet>
请注意,这会删除不需要的元素及其下方的任何内容.例如,要剥离字体元素本身,但允许其子元素,请修改最后一个模板,如下所示:
<xsl:template match="*[not(self::description or self::p or self::i or self::em or self::strong or self::b or self::ol or self::ul or self::li or self::a)]"/> <xsl:apply-templates/></xsl:template>
等效(略微清洁)的解决方案:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/transform" version="1.0"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="@*|node()" priority="-3"> <xsl:copy/> </xsl:template> <xsl:template match="description|p|i|em|strong|b|ol|ul|li|a"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="*"/></xsl:stylesheet>
相反的方法是将不需要的元素列入黑名单:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/transform" version="1.0"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="Font|span"/></xsl:stylesheet>
如果要允许跳过的元素的子项,请再次将apply-templates添加到最终模板.
总结以上是内存溢出为你收集整理的html – 使用XSL删除不需要的标签全部内容,希望文章能够帮你解决html – 使用XSL删除不需要的标签所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)