Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 10th, 2006, 11:07 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old January 11th, 2006, 10:49 AM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
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/
Reply With Quote
  #3 (permalink)  
Old January 13th, 2006, 10:35 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
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

Reply With Quote
  #4 (permalink)  
Old January 18th, 2006, 07:31 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
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


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 03:26 AM
Transform SQL server 2005 XML with xslt bonekrusher XSLT 0 July 11th, 2007 08:45 PM
firefox/mozilla crash: javascript in XSLT stekker XSLT 4 May 3rd, 2006 05:15 PM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 09:18 PM



All times are GMT -4. The time now is 03:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.