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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:56:52+00:00 2026-06-10T23:56:52+00:00

At the moment I am struggling with clientside xpath evaluation. For developing purposes I

  • 0

At the moment I am struggling with clientside xpath evaluation. For developing purposes I added:

<?php header( 'Content-type: application/xhtml+xml' ); ?> 

to the top of my page to get parse errors if I produce non valid (x)html. I want to make some xpath queries like //div, or //div[@class='test'] and so on using this code:

function xpath( query ){
    var evaluater = new XPathEvaluator();
    var resolver = document.createNSResolver( document.documentElement );
    var iterator = evaluater.evaluate( query, document, resolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
    var nodes = [];
    var result;
    while( ( result = iterator.iterateNext() ) != null ){
        nodes.push( result );
    }
    return nodes;
};

the top of my page looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
    <head>
        <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />

It works fine if the header(...) method is commented out, but if it is triggered the xpath queries return no result.

Why is this? What role plays the header for xpath evaluation?

edit:

if I drop the xmlns from the html element an switch the header – method, this appears:

without xmlns

  • 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-06-10T23:56:53+00:00Added an answer on June 10, 2026 at 11:56 pm

    If your (X)HTML document is parsed by the XML parser (and setting the content type to application/xhtml+xml asks the browser or user agent to parse the document with the XML parser) the XML rules apply and XPath works on this document according to XML rules. In that case all the XHTML elements like div are in the XHTML namespace http://www.w3.org/1999/xhtml and for an XPath 1.0 expression to select an element in a namespace you need to use e.g. //pf:div where you bind the used prefix (e.g. pf) to the XHTML namespace http://www.w3.org/1999/xhtml. How you do that depends on the XPath API you use, in the case of the evaluate method and the Javascript API you need e.g.

    document.evaluate('//pf:div', document, function(prefix) { if (prefix === 'pf') return 'http://www.w3.org/1999/xhtml'; else return null; }, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
    

    To explain it the other way, your current path expression //div selects elements with local name div in no namespace and as the elements in the XHTML documents are in the XHTML namespace that path does not select any elements (as long as XML parsing rules apply).

    Of course these days with all the browser vendors having made the move to HTML5 to continue to use text/html instead of switching to XML based parsing I wonder why you want to serve your documents as XML.

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

Sidebar

Related Questions

I am working on an iPhone app at the moment and I am struggling
I'm struggling with grabbing an image at the moment... sounds silly, but check out
I am struggling at the moment with Mallet's ridiculously poor documentation regarding HMMs. I
I've been struggling with this for two days right now. At the moment I
G'day dear stackers, I am really struggling at the moment with a basic test
i'm struggling with a layout issue at the moment. In fact i need 3
I can't get certain jscript functions in Orchard. At this moment I am struggling
I'm actually struggling to word this properly. What I have at the moment is
I'm really struggling with ABAddressBookGetPersonWithRecordID at the moment. I am saving an ID, and
Im learning to code in x86 assembly (32-bit at the moment) and I'm struggling

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.