Wrox Programmer Forums
BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 2nd Edition by Nicholas C Zakas; ISBN: 9780470227800
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800 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 December 9th, 2009, 07:04 PM
Registered User
Join Date: Dec 2009
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default Javascript and XML...how to convert an Xslt file to DOM obj

Working through the Javascript book "Professional Javascript for Web Developers" by Wrox, author "Zakas".
In particular I am working through his section on how to handle XML processing in Javascript.

The book gives a really good cross browser function for handling xslt transformation, but it stops short in
telling me how to create a dom object out of an xslt file. "myfile.xslt"

I have never worked with any of the XML methods or properties before so I am not really sure of what I am
looking for. I do have a function that creates a dom object from an xml string that is cross browser functional.
Can I use this function to create an xslt dom object;

function parseXml(xml){
    var xmldom = null;
    if(typeof DOMParser != "undefined"){
        xmldom = (new DOMParser()).parseFromString(xml, "text/xml");
        var errors = xmldom.getElementsByTagName('parseError');
            throw new Error{"xml parsing error:" + errors[0].textContent);
    } else if (document.implementation.hasFeature("LS", "3.0")){
        var i = document.implementation;
        var parser = i.createLSParser(i.MODE_SYNCHRONOUS, null);
        var input = i.createLSInput();
        input.stringData =xml;
        xmldom = parser.parse(input); // if this fails it automatically throws an error with the data
    }else if(typeof ActiveXObject != "undefined"){
        var xmldom = createDocument();
        if(xmldom.parseError != 0){
            throw new Error("xml parsing error: " + xmldom.parseError.reason);
    } else {
        throw new Error("no parser available");
    return xmldom;
} // end cross browser function for creating DOM object from strings
function createDocument(){
    if(typeof arguments.calle.activeXString != "string"){
        var versions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument"];
        for(var i=0, len=versions.length; i<len; i++){
                var xmldom = new ActiveXObject(versions[i]);
                arguments.calle.activeXString = versions[i];
                return xmldom;
            } catch (ex) {
    return new ActiveXObject(arguments.callee.activeXString);
var xmldom = parseXml("my xml string");
The book on page 540 and 541 shows how to create an xslt dom object using the function:


but it doesn't give an idea on how to create the apropriate code for other browsers. The above is an IE only approach.

I searched on the web but maybe I am using the wrong search criteria
Can you advise?

Old December 9th, 2009, 09:55 PM
nzakas's Avatar
Wrox Author
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts

An XSLT file is just an XML file, so you can load it into a DOM in the exact same way as you would any other XML file. Only IE has the concept of FreeThreadedDocument, all other browsers just use the same DOM document object as they do for XML.
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
The Following User Says Thank You to nzakas For This Useful Post:
krraleigh (December 10th, 2009)

Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript and XML...convert a file into a string krraleigh BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800 1 December 9th, 2009 09:54 PM
Convert XML file using XSLT sushil.sharma75 XSLT 13 November 20th, 2009 01:59 PM
How to save XML file using JScript/DOM in IE induriprakash XML 2 April 26th, 2005 07:37 AM
XML DOM and Javascript lilu XML 3 September 25th, 2003 07:55 AM
Problem writing xml file using DOM Bala XML 0 September 6th, 2003 02:15 AM

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