Is it possible to execute javascript from an XML document loaded into the browser?

417 views Asked by At

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.

1

There are 1 answers

1
Martin Honnen On BEST ANSWER

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.