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:

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
divare in the XHTML namespacehttp://www.w3.org/1999/xhtmland for an XPath 1.0 expression to select an element in a namespace you need to use e.g.//pf:divwhere you bind the used prefix (e.g.pf) to the XHTML namespacehttp://www.w3.org/1999/xhtml. How you do that depends on the XPath API you use, in the case of theevaluatemethod and the Javascript API you need e.g.To explain it the other way, your current path expression
//divselects elements with local namedivin 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/htmlinstead of switching to XML based parsing I wonder why you want to serve your documents as XML.