Wrox Programmer Forums
|
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 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 October 15th, 2005, 09:52 AM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default gecko based xpath

Ok, I solved my last problem by passing in the whole DOM object to a function, however I'm back to the gecko side. The problem is the gecko browser (ff in my case) prepends it's own prefix to the tree when using a default namespace. Normally this is no biggy, but my tree is an xhtml document, hence while the browser will understand a tag like [div] it doesnt understand [A0:div]. Here is my function:

    customXPath_selectSingleNode = function(curElement, xPathStr, nsPrefix, nsValue)
    {
        var NSResolver = null;
        if(nsPrefix != null)
            NSResolver = function (prefix)
            {
                if(prefix == nsPrefix)
                    return nsValue;
                else
                //this shouldn't ever happen
                    return null;
            }

        var oEvaluator = new XPathEvaluator();
        var oResult = oEvaluator.evaluate(xPathStr, curElement.documentElement, NSResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

        if (oResult != null)
        {
            return oResult.singleNodeValue;
        }

        return null;
    }

a slight variation from the implementation from page 15 to handle NS and have the same signiture for the IE version (where I have to pass in the current element). Infact, just for completeness here is the IE version:
    customXPath_selectSingleNode = function(curElement, xPathStr, nsPrefix, nsValue)
    {
        curElement.setProperty("SelectionNamespaces", "xmlns:"+ nsPrefix + "='" + nsValue + "'");
        return curElement.documentElement.selectSingleNode(xPathS tr);
    }

Anyway, my question is, how can I prevent Firefox/Gecko from outputing a tree with a namespace prefix in each tag, A0 by default?

Any help would be greatly appreciated, i've exausted all my other search options, thanks!

-Paul

 
Old October 15th, 2005, 12:57 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Paul,

As best I understand it, you will only get the A0 namespace when you specifying a namespace using document.implementation.createDocument() and passing in a namespace. Try using loadXML() from the book or DOMParser on a root element that has the namespace defined such as <xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml" /> and build upon that.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 
Old October 15th, 2005, 01:36 PM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nicholas, my patient mentor:)

I do infact load my document with the loadXML from the book. when i print it out with .xml (to string) it looks just as expected. It's only when I use the xpath stuff that the returned nodes take the namespace.

A call with a default NS would look something like:
customXPath_selectSingleNode(XMLdivDOM, "xhtml:body/xhtml:div[@class = '*someClassName*']", "xhtml", XMLdivDOM.documentElement.namespaceURI)
else
customXPath_selectSingleNode(XMLdivDOM, "body/div[@class = '*someClassName*']", null, null) for no namespace (updated ie function to test if 3rd param null and not set ns if that's the case)


my xml document has the default ns, <html xmlns="http://www.w3.org/1999/xhtml" lang="en">

Now, currently the returned document, when a default NS is used, contains a automatically created prefix for the default NS on anything returned by a gecko based browser.
Thus I have a ton of a0: 's all over my document. IE on the other hand leaves them out (though uses a ns in it's selection). The returned node in gecko/firefox defines xmlns:a0="http://www.w3.org/1999/xhtml at the root of the node and all elements following have that a0 prefix. I'd say that's the better way to do it, as to not lose the namespace, set in the parent, however for my use, it complicates things. The document i'm getting back is html and I want to write it directly to a div.
IE if fine cause the html tags still look like html tags, however FF get's that prefix on all the tags so of course the browser doesn't understand something like <a0:div> and it ignores all the tags.

I know the better way would be just to send an xml doc and use a transform to mark it up, however I don't have that option, I have to read in an xhml file, pull out a few pieces and write a certain section back to the browser.

So to summerize, is there a way to get back a xpath node in a gecko based engine which doesn't carry its parent namespace (hence define a0 namespace and prefix all child elements). As I said, IE doesn't do that and if u do a selection like customXPath_selectSingleNode(XMLdivDOM, "/", "def", XMLdivDOM.documentElement.namespaceURI) FF doesn't return it either (guess it's an optimization since it just returns the original (unaltered document), no processing).

I'd appreciate any help on this, thanks!
-Paul

P.S. i could "hack" it will a replace of /a0/g to "" for Firefox but ewwww






Similar Threads
Thread Thread Starter Forum Replies Last Post
XPATH pallone XSLT 4 November 19th, 2006 07:50 PM
Overflow scrollbars vs. div size in IE vs. Gecko mishagos BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 4 April 3rd, 2005 02:38 PM
XPath - Selecting nodes based on attribute values billy_bob_the_3rd XML 4 December 1st, 2004 06:12 PM
Navigator/Gecko Headaches! richard.york Javascript 1 December 23rd, 2003 08:22 PM
XPath help itekcorp XML 1 June 17th, 2003 01:14 PM





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