Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
  #2 (permalink)  
Old December 9th, 2009, 09:56 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
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/
  #3 (permalink)  
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
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



All times are GMT -4. The time now is 08:19 AM.


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