SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question XSLT Tranformation with Javascript in Google Chrome?

    Could someone who's skilled in javascript please help me solve this.

    I'm trying to get the W3C tutorial XSLT - On the Client to work in the Google Chrome Browser.
    It works fine in IE and FF but in Chrome it doesn't work at all, no error message even.

    I read some things about XMLdocument.load isn't supported in Chrome so I'm guessing it's something to do with that but I have no idea how to implement a fix.

    I read this on a different forum. I've tried implementing it in the code but cannot get it to work.

    Try replacing:
    Code:
    var xmlDoc = document.implementation.createDocument("", " ", null);
    xmlDoc.async = false;
    xmlDoc.load(xmlsrc);
    With:
    Code:
    var xmlhttp = new window.XMLHttpRequest();
    xmlhttp.open("GET", xmlsrc, false);
    xmlhttp.send(null);
    var xmlDoc = xmlhttp.responseXML.documentElement;
    Here is the code I'm trying to get to work:

    Code:
    <html>
    <head>
    <script>
    function loadXMLDoc(fname)
    {
      var xmlDoc;
      // code for IE
      if (window.ActiveXObject)
      {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      }
      // code for Mozilla, Firefox, Opera, etc.
      else if (document.implementation
      && document.implementation.createDocument)
      {
        xmlDoc=document.implementation.createDocument("","",null);
       }
      else
      {
        alert('Your browser cannot handle this script');
      }
    xmlDoc.async=false;
    xmlDoc.load(fname);
    return(xmlDoc);
    }
    
    function displayResult()
    {
    xml=loadXMLDoc("cdcatalog.xml");
    xsl=loadXMLDoc("cdcatalog.xsl");
    // code for IE
    if (window.ActiveXObject)
      {
        ex=xml.transformNode(xsl);
        document.getElementById("example").innerHTML=ex;
      }
      // code for Mozilla, Firefox, Opera, etc.
      else if (document.implementation
      && document.implementation.createDocument)
      {
        xsltProcessor=new XSLTProcessor();
        xsltProcessor.importStylesheet(xsl);
        resultDocument = xsltProcessor.transformToFragment(xml,document);
        document.getElementById("example").appendChild(resultDocument);
      }
    }
    </script>
    </head>
    <body id="example" onLoad="displayResult()">
    </body>
    </html>
    Thanks for any help!!

  2. #2
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No one?

  3. #3
    SitePoint Member
    Join Date
    Jan 2011
    Location
    Athens, Greece
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Solution

    I realise this thread is >17 months old, but just for archival purposes and for "closure" I am posting a version of the OP's script with a couple of tiny changes which made it "work for me" (at least it did on Firefox, Chrome, Internet Explorer, and Dragon - I don't know about Safari, Konqueror, etc but I presume they would be like Chrome, due to also being Webkit-based?):

    Code:
    <html>
    <head>
    <script>
    function loadXMLDoc(fname)
    {
      var xmlDoc;
      // code for IE
      if (window.ActiveXObject)
      {
        xmlDoc=new ActiveXObject("Microsoft.XMLHTTP");
      }
      // code for Mozilla, Firefox, Opera, etc.
      else if (window.XMLHttpRequest)
      {
        xmlDoc=new XMLHttpRequest();
       }
      else
      {
        alert('Your browser cannot handle this script');
      }
    xmlDoc.open("GET",fname,false);
    xmlDoc.send("");
    return(xmlDoc.ResponseXML);
    }
    
    function displayResult()
    {
    xml=loadXMLDoc("cdcatalog.xml");
    xsl=loadXMLDoc("cdcatalog.xsl");
    // code for IE
    if (window.ActiveXObject)
      {
        ex=xml.transformNode(xsl);
        document.getElementById("example").innerHTML=ex;
      }
      // code for Mozilla, Firefox, Opera, etc.
      else if (window.XMLHttpRequest)
      {
        xsltProcessor=new XSLTProcessor();
        xsltProcessor.importStylesheet(xsl);
        resultDocument = xsltProcessor.transformToFragment(xml,document);
        document.getElementById("example").appendChild(resultDocument);
      }
    }
    </script>
    </head>
    <body id="example" onLoad="displayResult()">
    </body>
    </html>
    This wraps the generated code inside the <body> tags, so make sure the xsl doesn't also create <html> or <body> tags (if you care about validation...).
    Last edited by rowanthorpe; Jan 14, 2011 at 22:51. Reason: highlight bbcode limits height too much


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •