是否可以从加载到浏览器中的XML文档中执行JavaScript?

发布于 2025-01-22 20:06:53 字数 1218 浏览 2 评论 0 原文

我想知道是否有一些XML处理指令,甚至有一个元素(例如HTML < script> 元素)会吸引Chrome(以及诸如Safari和Edge之类的克隆),Firefox和Opera和Opera加载和加载和加载和即使不是HTML,执行JavaScript也会形成任何XML文档。

例如,

<?xml version="1.0" encoding="UTF-8"?>
<?script type="text/javascript" href="xslt.js"?> 
<root>
  <foo>
    <bar/>
  </foo>
</root>
   

这将加载文档作为XML,并加载JavaScript,然后可以引导其余的渲染。 JavaScript可以从窗口访问XML DOM.DOCEMUMENT变量,并尽其所能最终用HTML节点替换root节点,然后浏览器可以呈现。

一个问题,非常专注。


由于答案可能是“否”的,如果您问“为什么要这样做?”这就是为什么需要这样的事情。不,这不是其他问题,而是一个理由,为什么需要我要求的问题并想解决可能的解决方法。

我知道浏览器中的XSLT已开发为加载任何XML文档,并且使用XML式处理指令,您可以告诉浏览器使用其内置的XSLT引擎将其渲染。不幸的是,浏览器开发人员都不知道XML,XSLT和XPATH的出色力量,并痴迷于这个JSON胡说八道。结果,XSLTProcessor功能是旧的,在XSLT(1.0)的石头版本上,甚至充满了错误并且完全不受欢迎。更糟糕的是,关于“安全性”的借口,它已经如此令人震惊,并计划完全从浏览器代码中删除。

这意味着我们需要找到自己的方法来做到这一点。这意味着,我们需要在XML文档中包含JavaScript Bootstrap代码的某种方法,类似于XSL-StyleSheet处理指令,因此我们可以使用基于JavaScript的XSLT引擎(例如Saxon-js)启动自己的处理。

当他们实际上最终从其代码中删除此不良XSLT V1.0处理器时,问题将是,如果将XML文档(不是HTML)加载到浏览器中,甚至会发生什么?浏览器将如何处理?当前的渲染作为此相当打印的XML使用某种默认样式表完成。然后,问题变成了一个人如何以某种方式将其他一些默认样式表连接到浏览器配置中?

一种可能的方法是创建一个浏览器扩展程序,该扩展程序实际上可以解释XML文档时实际上可以解释这些处理指令。这可以执行脚本处理指令,但也许更好,只需以其自己的方式处理处理指令。

I wonder if there is some XML processing instruction or even an element (like the HTML <script> element) which would entice Chrome (and clones like Safari and Edge), Firefox, and Opera to load and execute javascript form any XML document, even if it's not HTML.

Example

<?xml version="1.0" encoding="UTF-8"?>
<?script type="text/javascript" href="xslt.js"?> 
<root>
  <foo>
    <bar/>
  </foo>
</root>
   

This would load the document as XML and would load the javascript which then could bootstrap the rest of the rendering. The javascript could access the XML DOM from the window.document variable, and do whatever it wants ultimately replacing the root node with an html node which the browser could then render.

One question, very focused.


Since the answer is probably "no", and if you ask "why would you want to do this?" here is why something like this is needed. And no, this is not additional questions, but rather, a justification why what I am asking for is needed and musing about possible workarounds.

I know that XSLT in the browser had been developed to load any XML document and with the xml-stylesheet processing instruction you could tell the browser to render it using its built-in XSLT engine. Unfortunately the browser developers are all ignorant of the awesome powers of XML, XSLT and XPath and obsessed with this JSON nonsense. As a result, the XSLTProcessor feature is old, on a stone-age version of XSLT (1.0) and even with that full of bugs and entirely unmaintained. Worse yet, it has been so botched with excuses about "security" and it is scheduled to be entirely removed from browser code.

What this means is we need to find our own ways to do this. And that means, we need some way of including a javascript bootstrap code in the XML documents, similar to the xsl-stylesheet processing instruction so we can start our own processing with javascript based XSLT engines such as Saxon-JS.

When they actually finally remove this bad XSLT v1.0 processor from their code, then the question will be, what would even happen if you load an XML document (that's not HTML) into the browser? What would the browser do with it? The current rendering as this pretty-printed XML is done with some sort of default stylesheet. The question then becomes how could one somehow hook some other default stylesheet into the browser configuration?

One possible way to do that would be to create a browser extension that would actually interpret these processing instructions when an XML document is loaded. This could execute the script processing instruction, but perhaps even better, just handle the processing instruction in its own way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

疯到世界奔溃 2025-01-29 20:06:53

XHTML 脚本元素可能有效:&lt; root&gt;&gt;&lt; script src =“ foo.js” xmlns =“ http://www.w.org/19999/1999/xhtml” /&gt; ..&lt;/root&gt; do eg &lt; root&gt;&gt;&gt; script src =“ example1.js” xmlns =“ http:///wwww.w3。 org/1999/xhtml“/&gt;&lt; foo&gt;&lt; DOM文档在事件侦听器附加到 domcontentloaded 事件的事件侦听器中。这只是脚本文件已加载和执行并可以访问XML DOM文档的一个示例。

一个示例引导saxon-js 2以运行xslt 3在 https:// martin> https:// martin -honnen.github.io/xslt/2022/2022/example2.xml ,它仅使用两个XHTML脚本元素,一个加载saxon-js库,另一个加载saxon-js库,然后设置事件侦听器以应用示例转换和插入示例它的结果是该文档,替换默认文档内容。

The XHTML script element is likely to work: <root><script src="foo.js" xmlns="http://www.w3.org/1999/xhtml"/>..</root>: https://martin-honnen.github.io/xslt/2022/example1.xml does e.g. <root><script src="example1.js" xmlns="http://www.w3.org/1999/xhtml"/><foo><bar>baz</bar></foo></root> where example1.js outputs the XML DOM document to the console in an event listener attached to the DOMContentLoaded event. That is just an example that the script file is loaded and executed and has access to the XML DOM document.

An example bootstrapping Saxon-JS 2 to run XSLT 3 is at https://martin-honnen.github.io/xslt/2022/example2.xml, it just uses two XHTML script elements, one loading the Saxon-JS library, the other then setting up the event listener to apply an example transformation and insert its result into the document, replacing the default document content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文