Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3793840
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:50:15+00:00 2026-05-19T12:50:15+00:00

I’m having trouble evaluating XPaths using the nsIDOMXPathEvaluator from Mozillas XPCOM. I’m running the

  • 0

I’m having trouble evaluating XPaths using the nsIDOMXPathEvaluator from Mozillas XPCOM. I’m running the following javascript code through XPCShell:

[...]  
var myPaths = new Array();  
myPaths[0] = "/";
myPaths[1] = "/node()";  
myPaths[2] = "/html";   

for(i in myPaths) {  
    myPath = myPaths[i];  
    var document = doms[0];  
    var xpEval = Components.classes["@mozilla.org/dom/xpath-evaluator;1"].createInstance(Components.interfaces.nsIDOMXPathEvaluator);  
    var ns = xpEval.createNSResolver(document.documentElement);  
    var type =  Components.interfaces.nsIDOMXPathResult.UNORDERED_NODE_SNAPSHOT_TYPE;  

    var res = xpEval.evaluate(myPath, document.documentElement, ns, type, null);  
    dump("\nPath: "+myPath+"\n");  
    dump("Result length: "+res.snapshotLength+"\n");  
    for ( var i=0 ; i < res.snapshotLength; i++ )  
        dump("... Node: "+res.snapshotItem(i)+"\n"); 
        dump("... ... .nodeName: "+res.snapshotItem(i).nodeName+"\n");  
}  
[...]  

The doms-list is an array of html-documents parsed using do_parse_document from the xpcshell testing utilities. The top of all documents are:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr">

My trouble is that the XPaths I use as input doesn’t quite return what I expect. Below is the output from the above snippet:

Path: /
Result length: 1
... Node: [object XMLDocument]
... ... .nodeName: #document


Path: /node()
Result length: 2
... Node: [object DocumentType]
... ... .nodeName: html
... Node: [object HTMLHtmlElement]
... ... .nodeName: html

Path: /html
Result length: 0

Path: //html
Result length: 0

I would expect at least 1 or 2 results from the /html and //html paths. (Those returned when using /node() as path)

I can’t get correct output when doing queries like count(//p) either (that one returns 0 although there are plenty of paragraphs in the document).

I have tried calling document.evaluate() instead of xpEval.evaluate() with the same results. I have tried just passing null for namespace, same results.

Am I making some stupid mistake(s), or might there be some quirks in the nsIDOMXPathEvaluator that means I can’t use it how I intend?

Thanks for your time!

Regards,
Torin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:50:16+00:00Added an answer on May 19, 2026 at 12:50 pm

    Dump the namespaceURI property of the document.documentElement node, it is likely in the XHTML namespace http://www.w3.org/1999/xhtml and in that case with XPath 1.0 to select elements in any namespace you need to bind a prefix to the namespace URI and use that prefix. With the API you use you have to make sure your namespace resolver resolves your choosen prefix to the XHTML namespace URI. So you need e.g.

      var nsResolver = function (prefix) { if (prefix === 'xhtml') return 'http://www.w3.org/1999/xhtml'; else return null; };
      var res = xpEval.evaluate(myPath, document, nsResolver, type, null); 
    

    and then paths like /xhtml:html or /xhtml:html/xhtml:body/xhtml:h1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I am currently running into a problem where an element is coming back from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.