views:

859

answers:

5

I'm trying to write a bookmarklet to help some QA testers submit useful debugging information when they come across issues. Currently I can set window.location to a URL that provides this debugging information, but this resource is an XML document with an xml-stylesheet processing directive.

It would actually be more convenient if the testers were able to see either the raw XML data as plain text, or the default XML rendering for IE and Firefox.

Does anyone know a way to disable or override xml-stylesheet directives provided inside an XML document, using Internet Explorer or Firefox?

Edit: I've opened a bounty on this question. Requirements:

  • Client-side code only, no user intervention allowed
  • Solutions for both IE and Firefox needed (they can be different solutions)
  • Disabling stylesheet processing and rendering it as text is acceptable
  • Overriding stylesheet processing with a custom XSL is acceptable
  • Rendering the XML with the browser default XML stylesheet is acceptable
A: 

As far as I know, there is no way of doing what you are trying to do. The issue is, javascript cannot read the dom of the xml from a client side xml/xsl transform. As far as the javascript is concerned, it is executing on a normal html dom.

However, there may be some hope depending on the type of webapp. You could use ajax to get the xml of the current url. As long as there is no post data, or any other randomness, this method should work fine.

Buddy
+2  A: 

Can't you just do "View Source" in both browsers?

I don't want to place too much mental effort on the testers. :) But not surprisingly this is looking like the best option so far.
Steven Huwig
+1 in IE8 and Firefox the source viewer does a very good job of displaying XML.
Martin Peck
If you want to change the app that views the source (say, to Notepad2) then check out http://dotnet.org.za/thea/archive/2004/11/25/7933.aspx
Martin Peck
the firefox source browser also accepts ctrl+r to reload the page, which could come in handy.a neat solution would be to put a location bar into the source browser
xkcd150
+1  A: 

You can avoid a processing instruction by using an intermediary step to pre-process the XML before the content is output in the browser.

Client-side suggestion

  1. Retrieve the relevant XML document via an AJAX request
  2. Parse the XML into a DOM (note: a DOM not the DOM)
  3. Traverse the DOM and render in the browser the required data

Server-side suggestion

Instead of directly requesting the pertinent XML document, make a request instead to a proxy script that removed from the XML content all processing instructions, or indeed all that you don't want.

Instead of:

window.location = 'http://example.com/document.xml';

use:

window.location = 'http://example.com/proxy/';


The script at http://example.com/proxy/ would:

  1. Load document.xml
  2. Use whatever is necessary to remove the processing instruction from the XML content
  3. Output the XML content
Jon Cram
You could have made that shorter by just writing "render a different document or point to a different document."
Steven Huwig
+2  A: 

As long as you wouldn't have to deal with cross domain permissions, a simple ajax request / alert box with the XML source would do the trick. You'll have to add a little bit to the xmlHttp declaration to make it compatible with IE.

<html>
<body>

<script language="JavaScript">
function ajaxFunction()
{

var xmlHttp=new XMLHttpRequest();;

  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      alert(xmlHttp.responseText);
      }
    }
  xmlHttp.open("GET","YOURFILE.xml",true);
  xmlHttp.send(null);

}
</script>

<a href="#" onclick="JavaScript:ajaxFunction()">Errors</a>

</body>
</html>
oneporter
+2  A: 

EDIT: too bad, though all seemed fine in the preview, the clickable examples seem to mess up things... Maybe the layout is fine in the history.

I've heard, but cannot validate for IE, that both IE and Firefox support the "view-source:" pseudo-protocol. Firefox on Mac indeed understands it, but Safari does not.

The following bookmarklet will not trigger the XSLT transformation specified in the XML. And though Firefox will render this using some colours, it does not execute the default transformation it would normally use for XML without any XSLT (so, the result of view-source does NOT yield a collapsable document tree that Firefox would normally show):

javascript:(function(){
  var u = 'http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml';
  var w = window.open();
  w.document.location.href = 'view-source:' + u;
})()

When fetching the document using Ajax then one is not limited to the alert oneporter used, but can display it in a new window as well. Again: this will not invoke the XSLT transformation that is specified:

javascript:(function(){
  var u = 'http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml';

  var w = window.open(); /* open right away for popup blockers */

  var x = new XMLHttpRequest();
  x.open('GET', u, true); 
  x.onreadystatechange = function(){
    if(x.readyState == 4){
      w.document.open('text/html');
      /* hack to encode HTML entities */
      var d = document.createElement('div'); 
      var t = document.createTextNode(x.responseText); 
      d.appendChild(t);
      w.document.write('<html><body><pre>' 
          + d.innerHTML + '</pre></body></html>');
      w.document.close();
      w.focus();
    }
  };
  x.send(null); 
})()
Arjan