/    Sign up×
Community /Pin to ProfileBookmark

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 [URL]http://www.webreference.com/programming/javascript/domwrapper/index.html [/URL]

[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
[/code]

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”);
});
[/code]

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”);
}
[/code]

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”);
}
[/code]

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 😡

PLEASE HELP ME 😮

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Spankmaster79authorJan 11.2006 —  http://www.webreference.com/js/column100/4.html or http://www.peachpit.com/articles/article.asp?p=20984[/QUOTE]

hm....what did the author of the answer try to tell me ? Probably you think I don't know how to write XSLT Files and XML Files.....

YOUR WRONG....thnx anyway :rolleyes:

I got this working in IE now I need help on doing it in Firefox. If you want to see the xml and xslt just tell me so..... :mad:

Greets

Spanky
Copy linkTweet thisAlerts:
@Spankmaster79authorJan 18.2006 — I got it done.....if someones interested here's the help

http://forums.mozillazine.org/viewtopic.php?p=2026837#2026837
×

Success!

Help @Spankmaster79 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...