Search code examples
bookmarkletxslt

bookmarklet to apply xslt stylesheet to a displayed xml file


How would I write a bookmarklet that applies a given XSLT stylesheet to XML file that is displayed in, e.g., firefox? The XML Document is already loaded in the browser and displayed without a stylesheet, and the XSLT is available under a fixed URL which should be encoded in the bookmarklet.


Solution

  • Here is an example:

    A bookmarklet for applying an XSLT stylesheet to an XML document - just insert the base64 encoded xsl in atob("") - tested on firefox

    javascript: (function() {
      var parser = new DOMParser();
      var ss = parser.parseFromString(atob(""), 'application/xml');
      var xsltProcessor = new XSLTProcessor();
      xsltProcessor.importStylesheet(ss);
      var newdoc = xsltProcessor.transformToDocument(content.document);
      var myWindow = window.open("data:text/html," + encodeURIComponent(newdoc.documentElement.innerHTML), "_blank", "");
      myWindow.focus();
    }());
    

    ref: https://gist.github.com/gosub/c7576b0c99ffdd7e993c