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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:38:32+00:00 2026-05-24T02:38:32+00:00

I need to create an XML xPath parser. All parsing has to happen on

  • 0

I need to create an XML xPath parser. All parsing has to happen on client side (using javascript). I created an javascript that does this, and everything looks OK until default namespaces come into play. I simply can’t query XML that has default namespace.

I created an example code on fiddle. In xmlString is XML string received from server. In xPathString is query done on received XML.

Here are some scenarios:

  1. http://jsfiddle.net/BF34q/1/ – no namespaces – everything works OK
  2. http://jsfiddle.net/BF34q/2/ – ns namespace added. element has ns: prefix. xPath uses this prefix – OK
  3. http://jsfiddle.net/BF34q/3/ – default namespace used – not sure how to configure xPathString.

Note that others will use this parser, so I would really like to avoid solutions like

var xPathString = "//*[local-name()='book']";

and enable them to parse it using simple xPath expressions. I wonder if it is possible to assign default namespace prefix in javascript?

Note: The example provided on fiddle will not work in IE.

  • 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-24T02:38:32+00:00Added an answer on May 24, 2026 at 2:38 am

    I think there are three ways to do this:

    1. Use //*[local-name()='book'] syntax for accessing nodes
    2. Convert XML to string, remove default namespace using RegExp, convert it back to XML
    3. For XML files where you know namespaces in advance, you can create your own namespace resolver, which will allow you to use your own prefix for default namespace.

    This can be achieved like this:

    function nsResolver(prefix) {
        switch (prefix) {
            case 'xhtml':
                return 'http://www.w3.org/1999/xhtml';
            case 'mathml':
                return 'http://www.w3.org/1998/Math/MathML';
            default:
                return 'http://example.com/domain';
        }
    }
    xml.evaluate('//myPrefix:book', xml, nsResolver, XPathResult.ANY_TYPE, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using iReport and I need to create a sub-report using a XML DataSource
I need to parse Visual Studio automatically generated XML documentation to create a report.
I need to create xml message and send it to the web service. Then
How to create and set values for attribute in XML ? I need to
I am using the Xerces implementation within JDK6 to perform XPath queries on an
I have been tasked with finding an open source DOM XML parser. The parser
My current program need to use programatically create a XPathExpression instance to apply to
I need to generate a number of XML documents from Java objects. The objects
I need to create a web app which will consume RSS feed. Do I
I have the following XML: <InterSection criteria=Microsoft-ASP>value</Intersection> <InterSection criteria=Microsoft-MVC>value</Intersection> <InterSection criteria=HP-MVC>value</Intersection> I need to

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.