Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 10th, 2006, 11:07 AM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Transform XML with XSLT in JavaScript Mozilla

Hi,

I'm trying to transform XML with XSLT in Mozilla and it doesn't seam to work.

All the XML Data is stored in a Data Island inside the HTML site I call the JavaScript from.

Here's what I've already done

So I can use the loadXML() Funktion in Mozilla I extended the Document class as told in http://www.webreference.com/programm...per/index.html

Code:
Document.prototype.loadXML = function(strXML) {
        //create a DOMParser
        var objDOMParser = new DOMParser();
        //create new document from string
        var objDoc = objDOMParser.parseFromString(strXML, "text/xml");  
        //make sure to remove all nodes from the document
        while (this.hasChildNodes())
            this.removeChild(this.lastChild);            
        //add the nodes from the new document
        for (var i=0; i < objDoc.childNodes.length; i++) {            
            //import the node
            var objImportedNode = this.importNode(objDoc.childNodes[i], true);            
            //append the child to the current document
            this.appendChild(objImportedNode);
        } //End: for
    } //End: function
And also extended the functionality to transfer it back to XML and added the xml attribute

Code:
    /**
    * add the xml attribute to the node class
    * Everytime the .xml attribute is called it will be transformed to string
    */
    Node.prototype.__defineGetter__("xml", function () {
        var oSerializer = new XMLSerializer();
        return oSerializer.serializeToString(this, "text/xml");
    });
Now I'm creating 2 Dom Documents and load the xml Data for the XML and the XSLT.

Code:
    
    objSrcTree = document.implementation.createDocument("", "", null);  
    objXSLT = document.implementation.createDocument("", "", null);  
    objSrcTree.async = false;
    try {
      objSrcTree.loadXML(document.getElementById("xmlData").innerHTML); 
      objXSLT.loadXML(document.getElementById("xsltData").innerHTML); 
    }
    catch(Exception){
        alert("loadXML failed in Mozilla");
    }
This works gread. An alert(objSrcTree.xml) or alert(objXSLT.xml) show's the 2 Documents :)

Now I create the XSLT Prozessor and try to transform
Code:
    objXSLTProc = new XSLTProcessor();
    objXSLTProc.importStylesheet(objXSLT);

    try {
        var resultDOM = objXSLTProc.transformToDocument(objSrcTree);
        var sResult = resultDOM.xml;
        alert(sResult);
    }
    catch(Exeption){
        alert("XSLT Transformation failed");
    }
He's in the try block because I don't get the alert("XSLT Transformation failed"). On debugging it runs over it and alert(sResult) shows an empty document. Document because alert(resultDOM) gives "XML Document Object"

What am I missing???

Greetings
Spanky :mad:

PLEASE HELP ME :o

 
Old January 11th, 2006, 10:49 AM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

It depends on what your XSLT code is. Maybe it's not returning anything.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 
Old January 13th, 2006, 10:35 AM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The xslt should be ok because IE is working with it fine. It's an Mozilla Problem. It also works in Mozilla if I use the XSLT as a file and don't embedd it in the HTML.Did this for testing.

But as Data Island it's not working!!

Need help on that

 
Old January 18th, 2006, 07:31 AM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it solved....info can be found here

http://forums.mozillazine.org/viewto...026837#2026837







Similar Threads
Thread Thread Starter Forum Replies Last Post
XML in JavaScript for Mozilla jwalborn Javascript 2 February 6th, 2008 11:47 AM
XML/XSLT Transform Error in ASP kwilliams Classic ASP XML 2 October 9th, 2007 02:26 AM
Transform SQL server 2005 XML with xslt bonekrusher XSLT 0 July 11th, 2007 07:45 PM
firefox/mozilla crash: javascript in XSLT stekker XSLT 4 May 3rd, 2006 04:15 PM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.