使用 XSL 删除不需要的标签
我收到了一些未知的内容作为描述,可能是这样的:
<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。因此,例如,会被剥离,但是
有什么帮助吗?
I've got some unknown content coming in as a description, maybe something like this:
<description>
<p>
<span>
<font>Hello</font>
</span>
World!
<a href="/index">Home</a>
</p>
</description>
There could conceivable be any HTML tag. I don't want all the tags. The tags I want to allow are p, i, em, strong, b, ol, ul, li and a. So, for example, <font> would be stripped, but <p> and <a> would remain. I'm assuming I have to match the ones I want (and make sure there's nothing to match the others), but can't work out how to do it.
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将这些元素列入白名单:
请注意,这会删除不需要的元素以及它们下面的任何内容。例如,要仅删除
font
元素本身,但允许其子元素,请修改最后一个模板,如下所示:等效(且稍微简洁)的解决方案:
相反的方法是黑名单 不需要的元素:
如果您想允许跳过元素的子元素,请再次将
apply-templates
添加到最终模板。Whitelist those elements:
Note that this removes the undesired elements and anything below them. To just strip the
font
element itself, for example, but allow its children, modify the last template like this:An equivalent (and slightly cleaner) solution:
The opposite approach is to blacklist the unwanted elements:
Again, add an
apply-templates
to the final template if you want to allow children of the skipped elements.