title: How to transform an XML file with XSLT, using a Greasemonkey script?
body: <p>I have a search server that provides a test page where I can input a query and it returns the results in XML. I want to be able to go through the results in a more user friendly way so I started playing around with XSLT and I have a simple style-sheet now that turns a somehow bloated XML into a simple table showing just some of the data. This works fine when I do it locally - that is, adding the XSL declaration to the XML and then opening the XML in a browser like Firefox.</p>
  
  <p>What I want to do though is to apply this transformation live in the browser, as soon as I fetch the results from the server through that test page. I investigated a bit and found that <a href="http://www.w3schools.com/xsl/xsl_client.asp" rel="nofollow">it's possible to do this with javascript</a>.</p>
  
  <p>Then I thought about Greasemonkey userscripts that can inject javascript into a page dynamically. I would just need a script that would kick in when I get the XML results from the test page. However, I'm stuck there because it seems like Greasemonkey isn't allowing scripts to run on XML files (at least in Firefox).</p>
  
  <p>I found very few examples and tried to use them as inspiration but couldn't make them work. (<a href="http://sunh11373.blogspot.be/2006/03/my-first-grease-monkey-script.html" rel="nofollow">Here's one, for example</a>.)</p>
  
  <p>Here's a simplified example of the XML I'm getting:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Results&gt;
      &lt;Result&gt;
          &lt;Listings total="2"&gt;
              &lt;Res&gt;
                  &lt;Result index="0"&gt;
                      &lt;id&gt;123456&lt;/id&gt;
                      &lt;name&gt;My Business&lt;/name&gt;
                      &lt;category&gt;Restaurants&lt;/category&gt;
                      &lt;phone&gt;9872365&lt;/phone&gt;
                  &lt;/Result&gt;
              &lt;/Res&gt;
              &lt;Res&gt;
                  &lt;Result index="1"&gt;
                      &lt;id&gt;876553&lt;/id&gt;
                      &lt;name&gt;Some Other Business&lt;/name&gt;
                      &lt;category&gt;Restaurants&lt;/category&gt;
                      &lt;phone&gt;9834756&lt;/phone&gt;
                  &lt;/Result&gt;
              &lt;/Res&gt;
          &lt;/Listings&gt;
      &lt;/Result&gt;
  &lt;/Results&gt;
  </code></pre>
  
  <hr>
  
  <p>Here's the script I'm loading in Greasemonkey - where nothing's happening:</p>
  
  <pre><code>// ==UserScript==
  // @name test xml renderer
  // @namespace http://sample.com
  // @description stylesheet for xml results
  // @include *
  // ==/UserScript==
  
  (function () {
      var xsl_str = '&lt;?xml version="1.0" encoding="utf-8"?&gt;\n\
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;\n\
      &lt;xsl:output method="html"/&gt;\n\
      &lt;xsl:template match="/"&gt;\n\
          &lt;html&gt;\n\
              &lt;head&gt;&lt;/head&gt;\n\
              &lt;body&gt;\n\
                  &lt;table id="results" border="1" cellspacing="0" cellpadding="0"&gt;\n\
                      &lt;thead&gt;\n\
                          &lt;tr&gt;\n\
                              &lt;th class="name"&gt;id&lt;/th&gt;\n\
                              &lt;th class="name"&gt;category ID&lt;/th&gt;\n\
                              &lt;th class="name"&gt;name&lt;/th&gt;\n\
                              &lt;th class="name"&gt;phone&lt;/th&gt;\n\
                          &lt;/tr&gt;\n\
                      &lt;/thead&gt;\n\
                      &lt;tbody&gt;\n\
                          &lt;xsl:for-each select="Results/Result/Listings/Res"&gt;\n\
                              &lt;tr&gt;\n\
                                  &lt;td class="small" width="120"&gt;\n\
                                      &lt;xsl:value-of select="Result/id"/&gt;\n\
                                  &lt;/td&gt;\n\
                                  &lt;td class="small" width="120"&gt;\n\
                                      &lt;xsl:value-of select="Result/category"/&gt;\n\
                                  &lt;/td&gt;\n\
                                  &lt;td class="small" width="120"&gt;\n\
                                      &lt;xsl:value-of select="Result/name"/&gt;\n\
                                  &lt;/td&gt;\n\
                                  &lt;td class="small" width="120"&gt;\n\
                                      &lt;xsl:value-of select="Result/phone"/&gt;\n\
                                  &lt;/td&gt;\n\
                              &lt;/tr&gt;\n\
                          &lt;/xsl:for-each&gt;\n\
                      &lt;/tbody&gt;\n\
                  &lt;/table&gt;\n\
              &lt;/body&gt;\n\
          &lt;/html&gt;\n\
      &lt;/xsl:template&gt;\n\
  &lt;/xsl:stylesheet&gt;\n\
  ';
  
      var processor = new XSLTProcessor();
      var dataXSL = new DOMParser().parseFromString(xsl_str, "text/xml");
  
      processor.importStylesheet(dataXSL);
      dataXML = document;
      var ownerDocument = document.implementation.createDocument("", "", null);
      var newFragment = processor.transformToFragment(dataXML, ownerDocument);
      dataXML.documentElement.replaceChild(newFragment, dataXML.documentElement.firstChild);
  })();
  </code></pre>
  
  <p>When I enable this script in Greasemonkey then all pages are successfully replaced with the HTML above in the XSL template. However it doesn't seem to apply to a local XML file or any XML coming from my server.(I know that to make Greasemonkey work with local files a setting needs to be changed in <code>about:config</code> in Firefox - <code>extensions.greasemonkey.fileIsGreaseable</code>).</p>
  
  <p>I don't have any experience with javascript so most likely I'm just making a very basic mistake. In in case, all the help is really appreciated.</p>