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

  • Home
  • SEARCH
  • 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 need to create program that creates a XML schema like below using System.Xml.XmlSchema
I need to access a XML document I created with JavaScript via XPath. If
I'm reading xml document using XPath and I need to create a Document object
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create an XML schema that validates a tree structure of an
I need to create an XML schema definition (XSD) that describes Java objects. I
I have a XML schema that I will need to create Java classes for.
I'm using iReport and I need to create a sub-report using a XML DataSource
I need to create an XML document for a website service that shows products
I need to parse a xml file using JAVA and have to create a

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.