使用 XSLT 解码 HTML 编码内容

发布于 2024-12-08 10:16:03 字数 1528 浏览 0 评论 0原文

我有一个网页,点击链接后会执行一些 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>&lt;p&gt;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 开发人员生成的源视图中所示):

&lt;p&gt;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅语花开 2024-12-15 10:16:03

并非所有 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

绝影如岚 2024-12-15 10:16:03

在转换 ATOM XML 时遇到类似的问题,其中帖子内容包含转义的 HTML 标签。禁用输出转义似乎适用于大多数浏览器,但不适用于 Firefox。因此,作为解决方法,在我的 XSLT 中,我向所有有问题的输出节点添加了 class="renderhtml" 属性,作为 JavaScript 的提示。然后在转换后添加以下代码。

// work around for XSLT disable-output-escaping="yes" not working in Firefox
if (navigator.userAgent.indexOf("Firefox")!=-1) {
  // get all "renderhtml" hints (HTML escaped within CDATA/text) nodes, then unescape and render HTML code
  var nodes = document.getElementsByClassName("renderhtml");
  for (var i = nodes.length - 1; i >= 0; i--) {
    nodes[i].innerHTML = nodes[i].textContent;
  }
}

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.

// work around for XSLT disable-output-escaping="yes" not working in Firefox
if (navigator.userAgent.indexOf("Firefox")!=-1) {
  // get all "renderhtml" hints (HTML escaped within CDATA/text) nodes, then unescape and render HTML code
  var nodes = document.getElementsByClassName("renderhtml");
  for (var i = nodes.length - 1; i >= 0; i--) {
    nodes[i].innerHTML = nodes[i].textContent;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文