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:06 PM
Registered User
 
Join Date: Dec 2009
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default Javascript and XPath when XPathEvaluator fails...

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.

What I am trying to ascertain is:
His section on dealing with XPath notes that there are some browsers that won't work with the XPathEvaluator.
He states that I should use the "corresponding methods on the Document" to make up for the short coming in
some browsers. Could someone give me an idea on what he means by "corresponding methods on the Document"?

His crossbrowser functions for Xpath:

Code:
 
// { prefix1: 'uri1', prefix2: 'uri2' } //example of namespace object
}
function selectSingleNode(context, expression, namespaces){
    var doc = (context.nodeType != 9? context.ownerDocument : context);
    
    if (typeof doc.evaluate != "undefined"){
        var nsresolver = null;
        if(namespaces instanceof Object){
            nsresolver = function(prefix){return namespaces[prefix];};
        }
        
        var result = doc.evaluate(expression, context, nsresolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        
        return (result !== null ? result.singleNodeValue : null);
        
    }else if(typeof context.selectSingleNode != "undefined"){
        //create namespace string
        if(namespaces instanceof Object){
            var ns = "";
            for(var prefix in namespaces){
                if(namespaces.hasOwnProperty(prefix){
                    ns += "xmlns:" + prefix + "='" + namespaces[prefix] + "' ";
                                             }
            }
            doc.setProperty("SelectionNamespaces", ns);
        }
        
        return context.selectSingleNode(expression);
        
    }else {
        throw new Error("no xpath engine found"):
    }
}// end function selectSingleNode()
 
var result = selectSingleNode(xmldom.documentElement, "wrox:book/wrox:author", {wrox: "http://www.wrox.com/" });
// result is an object
 
//using the same namespaces literal above...
 
function selectNodes(context, expression, namespaces){ //selecting multiple nodes in a crossbrowser fashion
        var doc = (context.nodeType != 9? context.ownerDocument : context);
    
    if (typeof doc.evaluate != "undefined"){
        var nsresolver = null;
        if(namespaces instanceof Object){
            nsresolver = function(prefix){return namespaces[prefix];};
        }
        
        var result = doc.evaluate(expression, context, nsresolver, XPathResult.ORDERED_SNAPSHOT_TYPE, null);
        
        var nodes = new Array();
        
        if(result !== null){
            for (var i=0, len=result.snapshotLength; i < len; i++){
                nodes.push(result.snapshotItem[i];
            }
        }
        
        return nodes;
        
    } else if (typeof context.slectNodes != "undefined"){
        //create namespace string
        if(namespaces instanceof Object){
            var ns = "";
            for(var prefix in namespaces){
                if(namespaces.hasOwnProperty(prefix){
                    ns += "xmlns:" + prefix + "='" + namespaces[prefix] + "' ";
                                             }
            }
            doc.setProperty("SelectionNamespaces", ns);
        }
        
        var result = context.selectNodes(expression);
        var nodes = new Array();
        
        for (var i = 0, len= result.lenght; i<len; i++){
            nodes.push(result[i]);
        }
        
        return nodes;
        
    } else {
        
        throw new Error("no xpath engine");
        
    }
    
}// end function selectNodes();
 
//function call
var result = selectNodes(xmldom.documentElement, "wrox:book/wrox:author", { wrox: "http://www.wrox.com/" });
thanx kevin
 
Old December 9th, 2009, 09:56 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Internet Explorer is the only browser that now doesn't support XPathEvaluator. You can use selectNode() and selectSingleNode() on the DOM Document object instead (IE only).
__________________
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 
Old December 10th, 2009, 04:23 PM
Registered User
 
Join Date: Dec 2009
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default

thanx
great book

Kevin





Similar Threads
Thread Thread Starter Forum Replies Last Post
INSERT Article fails also category fails too f117f117f117677 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 November 4th, 2009 09:20 AM
INSERT Fails teddyk ASP.NET 2.0 Basics 3 July 23rd, 2008 08:58 AM
List of Distinct values using xpath and javascript pnviv XSLT 4 November 5th, 2007 06:52 AM
CreateObject fails slam Javascript 1 October 6th, 2006 10:32 AM
This statement fails! pankaj_daga Oracle 1 July 6th, 2004 02:38 PM





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