title: jquery transform xsl xml problem..works okay in IE, fails in FireFox
body: <p>For the record, I am using Windows XP SP3, running FireFox 5 and IE 8.</p>
  
  <p>I am working on a project that is using code from the stone ages, trying to refactor it, modernize it.  It only worked on IE and involves heavy use of XML and XSLT.</p>
  
  <p>When the page loads, content is dynamically placed inside a div using XSLT and XML preprocessing.  This content consists of an img, map, and input tag, which is retrieved from an XSL file.  That works fine in both Firefox and Internet Explorer; it loads fine and the image displays correctly, with the proper mappings, utilizing usemap attribute on the img.</p>
  
  <p>There is a select box that allows the user to load a different image -- this is where I'm having issues.  When the user makes a selection in the select box, it mirrors the steps when the page first loaded: XSLT, via an XML object and an XML file.  Essentially, the contents of the div is reloaded.</p>
  
  <p>This works well in Internet Explorer (IE); however, FireFox is is not transforming at all.</p>
  
  <p>I found a jquery transform plugin and here is the line of code I am using:</p>
  
  <pre><code>function TransformXML()
  {
    var html = null;
    var XML_Image = null;
    // Load XML document into XML_Image and do manipulation on it before transformation
    // ...
    // Further down in the function..
  
    if (document.implementation &amp;&amp; document.implementation.createDocument)
    {
      html = $.transform({async:false, xmlobj:XML_Image, xsl:"xsl/ImageData.xsl"});
      document.getElementById("DivContent").appendChild(html);
    }
    else
    {
      html = $.transform({async:false, xmlobj:XML_Image, xsl:"xsl/ImageData.xsl"});
      document.getElementById("DivContent").innerHTML = html;
    }
  }
  </code></pre>
  
  <p>As I said, IE works fine.  Unfortunately, the <em>html</em> variable value is blank (FireBug reports "").  What could be the issue???</p>
  
  <p>The XSL file does use XSL:import, but the jquery transform plugin worked fine for IE; I don't know if FireFox doesn't like it, though (FireBug is awful at debugging XSLT).</p>
  
  <p>Thank you in advance for any help!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have determined that it is something with the XSL file.  I tried using a sample XML and XSL file from the jquery transform plugin and it worked.  Then, I tried using my XML object, XML_Image, and the sample XSL file and it worked, too.  Finally, I tried using a sample XML file and my XSL file, ImageData.xsl, and it did not work.  I am not sure what is wrong with the XSL file, however.</p>
  
  <p><strong>UPDATE2</strong></p>
  
  <p>Well, I think it is something with the XSL file.  Here it is:</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
  &lt;xsl:output method="xml" indent="yes"/&gt;
  
    &lt;xsl:template name="chgBR"&gt;
  
      &lt;xsl:param name="str"/&gt;
      &lt;xsl:choose&gt;
        &lt;xsl:when test='$str = ""'&gt;
          &lt;br/&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;
          &lt;xsl:value-of select="$str"/&gt;
        &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
    &lt;/xsl:template&gt;
  
  &lt;xsl:template name="replace"&gt;
    &lt;xsl:param name="str"/&gt;
    &lt;xsl:param name="src"/&gt;
    &lt;xsl:param name="dst"/&gt;
    &lt;xsl:if test='$str != ""'&gt;
      &lt;xsl:choose&gt;
      &lt;xsl:when test='substring-before( $str, $src ) = "" '&gt;
        &lt;xsl:value-of select='$str'/&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:value-of select='substring-before( $str, $src )'/&gt;
      &lt;xsl:choose&gt;
        &lt;xsl:when test='$dst = "&amp;lt;br/&amp;gt;"'&gt;
          &lt;xsl:element name="br"/&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;
          &lt;xsl:value-of select='$dst'/&gt;
        &lt;/xsl:otherwise&gt;
        &lt;/xsl:choose&gt;
        &lt;xsl:call-template name='replace'&gt;
          &lt;xsl:with-param name='str' select='substring-after( $str,$src )'/&gt;
          &lt;xsl:with-param name='src' select='$src'/&gt;
          &lt;xsl:with-param name='dst' select='$dst'/&gt;
        &lt;/xsl:call-template&gt;
      &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
    &lt;/xsl:if&gt;
  &lt;/xsl:template&gt;
  
  
  &lt;xsl:template match="/"&gt;
    &lt;xsl:apply-templates select="Figure" /&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Figure"&gt;
  
    &lt;input type="hidden" name="pdfpath" id="pdfpath"&gt;
      &lt;xsl:attribute name="value"&gt;Catalog/PDF/&lt;xsl:call-template name='replace'&gt;
          &lt;xsl:with-param name='str' select='./nm_pdf'/&gt;
          &lt;xsl:with-param name='src' select='" "'/&gt;
          &lt;xsl:with-param name='dst' select='"%20"'/&gt;
        &lt;/xsl:call-template&gt;
      &lt;/xsl:attribute&gt;
    &lt;/input&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>UPDATE3</strong></p>
  
  <p>I downloaded an XML editor, in hope it could debug my XML and XSL files and execute a transformation of it, outside of the browser.  The XSL file appeared to be okay; so, I went back to testing if the XML was, somehow, corrupt.  As it turns out, the XML editor was able to transform the XML file using the XSL file.  I can say the XML file does contain english and eastern asian language characters, encoded as UTF-8.  But IE was able to parse and transform it, as well as the XML Editor.  So, perhaps Firefox has a problem with the XML Files' UTF-8 encoding?</p>
  
  <p>Having no answer to this problem, I tried this:</p>
  
  <pre><code>if (window.DOMParser)
  {
    var serializer = new XMLSerializer();
    var XMLString = serializer.serializeToString(XML_Image);
    var parser = new DOMParser();
    var XML_Image_Again = parser.parseFromString(XMLString, "text/xml");
  }
  
  var Works = $.transform({async:false, xmlobj:XML_Image_Again, xsl:"xsl/ImageData.xsl"});
  var AlsoWorks = $.transform({async:false, xmlstr:XMLString, xsl:"xsl/ImageData.xsl"});
  
  Works = Works.replace(/&amp;quot;/g,"\"");
  Works = htmlfixed.replace(/(.gif).*?\"/g,"$1\"");
  
  document.getElementById("DivContent").innerHTML = Works;
  </code></pre>
  
  <p>What I did was serialize the XML to a string.  Then, I parsed the string and attempted to convert it back to a XML DOM object.  For some reason, it stayed a string. As a result, <em>Works</em> takes the value of a string.  AlsoWorks* takes the value of a string, as well, but this is the desired result.  What's odd is after I transformed the XML and passed it to <em>Works</em>, most of the quotation symbols got messed up and some some other data was appended to the end of the image I'm retrieving (.gif).  Because <em>Works</em> is a string, I could not use the appendChild method to attach it to the DOM tree.  I was left with no choice but to use innerHTML, which I did.</p>
  
  <p>All this time and energy because the project I'm working with is entrenched with XML and XSL files.</p>
  
  <p>I found that this blog also documents difficulties with FireFox and XSLT/XML/XSL:</p>
  
  <p><a href="http://ajaxandxml.blogspot.com/search/label/Firefox" rel="nofollow">http://ajaxandxml.blogspot.com/search/label/Firefox</a></p>
  
  <p>It works now, but I'm still scratching my head.  In the beginning, I mentioned it works in IE just fine.  And, perhaps more importantly, it works in <strong>BOTH</strong> FireFox and IE the first time I attach content to the div, which is upon initialization of the page.  It's only on subsequent times I am attaching content to the div that FireFox doesn't cooperate (and Firebug doesn't help much, nor does it report errors, unlike IE's web devleoper toolbar).  And upon initialization, I'm not even using jquery, just native DOM.  And, yet, when I used sample XML and XSL file pairs from the jquery transform plugi, those worked fine.  It's only when I use both my XML and XSL files together that it complains (again, not when the page first loads).  The strange thing is it wouldn't transform the XML object, but after I serialized it, the transformation worked fine.</p>
  
  <p>Too many questions.  I say it's probably FireFox and not the XML or XSL, because if the XML and XSL files were at fault, then the transformation should fail when the page loads and, yet, it does not.</p>
  
  <p>I sure as heck would love to know why appending the object to the div didn't work but serializing the object to a string and setting it to the innerHTML property, after transformation, passed with flying colors.</p>
  
  <p><strong>UPDATE4</strong></p>
  
  <p>In my XSL file, I placed the first two templates in a separate XSL file and am using the xsl:import feature, instead.  This broke it again.  So, FireFox and jquery transform plugin do not respond well to the xsl:import feature (IE has no issue with it).  I tried using the sample XML and XSL files from the transport plugin, the example which specifies how to use xsl:import, and it failed in firefox.</p>
  
  <p><strong>UPDATE5</strong></p>
  
  <p>I refactored the code, such that it does not use jquery, to investigate if xsl:import will fail.</p>
  
  <pre><code>// Test for Firefox only, purposely not including IE code
  function TransformNodeTest(xmlobj, xslpath)
  {
    var xslobj = LoadXSML(xslpath);
    var xsltProcessor = new XSLTProcessor();
    xsltProcessor.importStylesheet(xslobj);
    return(xsltProcessor.transformToFragment(xmlobj, document));
  }
  function LoadXSML(xslpath)
  {
    var xhttp=new XMLHttpRequest();
    xhttp.open("GET",xslpath,false);
    xhttp.setRequestHeader("Pragma", "no-cache");
    xhttp.send();
    return xhttp.responseXML;
  }
  
  // ImageData.xsl contains xsl:import statement
  // XML_Image is an XML obj
  var FireFoxHTML = TransformNodeTest(XML_Image, "xsl/ImageData.xsl");
  </code></pre>
  
  <p>It worked fine.</p>
  
  <p>Conclusions for replacing XML/XSL content in div with XML/XSL content--</p>
  
  <p>jquery transform plugin::</p>
  
  <p>IE: Works fine; no issues with xsl:import feature in xsl file.</p>
  
  <p>FireFox: Does not like xsl:import feature for some unknown reason (to me).  My stopgap, until a better solution comes along, is copy/paste all XSL statements into one file and not use xsl:import.  Also, need to serialize the XML obj and, optionally, parse it back to an XML object to transform.  Why does serializing fix it?  I do not know.</p>
  
  <p>XML DOM::</p>
  
  <p>IE: Works fine; no issues with xsl:import feature in xsl file.</p>
  
  <p>FireFox: No issue with xsl:import feature. Need to serialize the XML obj and, optionally, parse it back to an XML object to transform.  Why does serializing fix it?  I do not know.</p>
  
  <p>To reiterate, when the page loads initially, XSLT is conducted on the div and it works fine in FF and IE.  It's only on subsequent times does it fail in FF (but not IE).</p>