使用 XSLT 解码 HTML 编码内容
我有一个网页,点击链接后会执行一些 JavaScript。链接是:
<a
href="javascript:void(0)"
onclick="XsltTransform('category.xml','category.xslt');">Latest news</a>
javascript 是:
<script type="text/javascript">
function XsltTransform(xmlfile, xslfile) {
var xml = document.implementation.createDocument("", "", null);
var xslt = document.implementation.createDocument("", "", null);
xml.async = false;
xslt.async = false;
xml.load(xmlfile);
xslt.load(xslfile);
var processor = new XSLTProcessor();
processor.importStylesheet(xslt);
var XmlDom = processor.transformToDocument(xml)
var serializer = new XMLSerializer();
var output = serializer.serializeToString(XmlDom.documentElement);
var outputDiv = document.getElementById("contentbody");
outputDiv.innerHTML = output;
}
</script>
处理的 XML 看起来非常像:
<Content>
<Body><p>It may have taken over 12 years</Body>
</Content>
处理它的 XSL 是一个简单的 xsl:value-of 语句:
<xsl:template match="/">
<p>
<xsl:value-of select="*/*/Body" disable-output-escaping="yes" />
</p>
</xsl:template>
问题是,无论我在“禁用输出”中使用什么值“value-of”的“-escaping”属性,我总是得到这个渲染(如 Firefox Web 开发人员生成的源视图中所示):
<p>It may have taken over 12 years
我希望解码的 HTML 块在渲染时被编码,我的印象是这个是什么禁用输出转义将允许。
如何让这个非常原始的 XML 再次变成真正的 HTML?
I have a web page which executes some javascript upon a link click. The link is:
<a
href="javascript:void(0)"
onclick="XsltTransform('category.xml','category.xslt');">Latest news</a>
The javascript is:
<script type="text/javascript">
function XsltTransform(xmlfile, xslfile) {
var xml = document.implementation.createDocument("", "", null);
var xslt = document.implementation.createDocument("", "", null);
xml.async = false;
xslt.async = false;
xml.load(xmlfile);
xslt.load(xslfile);
var processor = new XSLTProcessor();
processor.importStylesheet(xslt);
var XmlDom = processor.transformToDocument(xml)
var serializer = new XMLSerializer();
var output = serializer.serializeToString(XmlDom.documentElement);
var outputDiv = document.getElementById("contentbody");
outputDiv.innerHTML = output;
}
</script>
The XML which is processed looks very much like:
<Content>
<Body><p>It may have taken over 12 years</Body>
</Content>
And the XSL which processes it is a simple xsl:value-of statement:
<xsl:template match="/">
<p>
<xsl:value-of select="*/*/Body" disable-output-escaping="yes" />
</p>
</xsl:template>
The problem is that no matter what value I use in the 'disable-output-escaping' attribute of the 'value-of', I always get this rendered (as seen in Firefox web developer generated source view):
<p>It may have taken over 12 years
I would like the block of decoded HTML to become encoded when rendering and I was under the impression that this is what the disable-output-escaping would allow.
How do I get this very raw XML to become real HTML again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并非所有 XSLT 处理器都支持
disable-output-escaping
。 Firefox 就是其中之一。Firefox 有一个未解决的错误,并且缺乏对其的支持:Bug 98168 - (doe)不工作
Not all XSLT processors support
disable-output-escaping
. Firefox is one of them that does not.There is an open bug for Firefox and it's lack of support for it: Bug 98168 - (doe) not working
在转换 ATOM XML 时遇到类似的问题,其中帖子内容包含转义的 HTML 标签。禁用输出转义似乎适用于大多数浏览器,但不适用于 Firefox。因此,作为解决方法,在我的 XSLT 中,我向所有有问题的输出节点添加了 class="renderhtml" 属性,作为 JavaScript 的提示。然后在转换后添加以下代码。
Had a similar problem transforming ATOM XML where the content of the posts contained escaped HTML tags. disable-output-escaping seems to work in most browsers but not Firefox. So as a work around, in my XSLT I added the class="renderhtml" attribute to all problematic output nodes, as a hint to javascript. Then added the following code after the transform.