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 3606758
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:17:23+00:00 2026-05-18T21:17:23+00:00

I am looking for a Javascript solution to this problem: I have an XML

  • 0

I am looking for a Javascript solution to this problem:

I have an XML document which is loaded by way of a typical AJAX method:

var xml = http.responseXML;

Consider the following example xml snippet:

<main>
    <primary>
         <enabled>true</enabled>
     </primary>
     <secondary>
         <enabled>true</enabled>
     </secondary>
 </main>

I would like to get the value of a node by specifying the path as follows:

var second_enabled = getNodeValueByPath('main/secondary/enabled', xml);

I cannot find any concise way of doing something like this. I seem to be forced to iterate through node collections after using getElementsByTagName and the like.

How would I construct the method getNodeValueByPath or is there some construct in Javascript that allows for this?

I am not well versed in Javascript.

EDIT: Here is an example that shows my attempt at using XPath and how it is failing:

XML:

<?xml version="1.0" ?>
<main xmlns="example.com">
  <primary>
    <enabled>true</enabled>
  </primary>
  <secondary>
    <enabled>false</enabled>
  </secondary>
</main>

JavaScript: (these are the relevant functions only)

function useHttpResponse()
{
    if (http.readyState == 4)
    {
        if(http.status == 200)
        {
            var xml = http.responseXML;
            var evalue = getXMLValueByPath('/main/secondary/enabled', xml);
            alert(evalue);
        }
    }
}

function getXMLValueByPath(nodepath, xml)
{
    var result = xml.evaluate(nodepath, xml, null, XPathResult.STRING_TYPE, null).stringValue;
    return result;
}

I am using the above JavaScript without any additional libraries. I am using Mozilla Firefox 3.6.13 which uses the document.evaluate method to select nodes (according to this information from w3schools). This application is for internal use and does not have to work on multiple browsers.

Given these examples, the alert dialog will appear, but contain no text. If I remove the string xmlns="example.com" from the XML document, the alert dialog appears with the text “false” as desired.

Debugging using Firebug shows that resultNode is an empty string as long as the namespace is declared.

  • 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-18T21:17:23+00:00Added an answer on May 18, 2026 at 9:17 pm

    This is FAQ: In XPath 1.0, a QName test without prefix selects elements under the null (or empty) namespace URI. So you need to register a prefix to the default namespace URI from your input source (example.com).

    How do you do that with DOM 3 XPath API?

    From http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator-evaluate

    evaluate
    Evaluates an XPath
    expression string and returns a result
    of the specified type if possible.
    Parameters
    […]
    resolver of type XPathNSResolver
    The resolver permits translation of all
    prefixes, including the xml
    namespace prefix, within the XPath
    expression into appropriate namespace
    URIs. If this is specified as null,
    any namespace prefix within the
    expression will result in
    DOMException being thrown with the
    code NAMESPACE_ERR.

    And from https://developer.mozilla.org/en/DOM/document.evaluate

    var xpathResult = document.evaluate(
     xpathExpression, 
     contextNode, 
     namespaceResolver, 
     resultType, 
     result);
    
    • namespaceResolver is a function that will be passed any namespace prefixes and should return a string representing the namespace URI
      associated with that prefix. It will
      be used to resolve prefixes within
      the XPath itself, so that they can be
      matched with the document. null is
      common for HTML documents or when no
      namespace prefixes are used.

    Following this guide, you can use document.createNSResolver or a function like:

    function nsResolver(prefix) {   
      return prefix == 'ex' ? 'example.com' : null;   
    }  
    
    function useHttpResponse()
    {
      if (http.readyState == 4)
      {
         if (http.status == 200)
         {
            var xml = http.responseXML;
            var evalue = xml.evaluate('string(/ex:main/ex:secondary/ex:enabled)',
                                      xml,
                                      nsResolver,
                                      XPathResult.STRING_TYPE,
                                      null);
            alert(evalue.stringValue);
         }
      }
    }     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking all day for a PHP or JavaScript solution to do
I am looking at some javascript code and it has this in a function:
I am looking for a good JavaScript library for parsing XML data. It should
I'm looking into a solution for work, where you have a two page spread
I'm looking for the best JavaScript editor available as an Eclipse plugin. I've been
I am looking for a drop-down JavaScript menu. It should be the simplest and
I am looking for a simple JavaScript example that updates DOM. Any suggestions?
I'm looking for a technique (javascript, CSS, whatever ???) that will let me control
I'm looking for a good JavaScript RegEx to convert names to proper cases. For
I'm looking to log unhandled javascript exceptions. Is there an event that fires when

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.