XPath for the HTML DOM in Internet Explorer
Dimitri Glazkov has produced a JavaScript implementation of DOM Level 3 XPath for Microsoft Internet Explorer. Below are some examples of what using XPath from Javascript looks like with his implementation
Now counting all links on your document is just one XPath query:
var linkCount = document.evaluate( “count(//a[@href])“, document, null, XPathResult.NUMBER_TYPE, null).getNumberValue();
So is getting a list of all images without an alt tag:
var imgIterator = document.evaluate( “//img[not(@alt)]“, document, null, XPathResult.ANY_TYPE, null);
So is finding a first LI element of al UL tags:
var firstLiIterator = document.evaluate( “//ul/li[1]“, document, null, XPathResult.ANY_TYPE, null);
Excellent work. XPath is one of the most powerful XML technologies and getting support for it in clientside HTML scripting should be a great boon to developers who do a lot of HTML processing using Javascript.
Comments
Anonymous
April 20, 2004
Would this require the HTML to be XHTML?Anonymous
April 20, 2004
XHTML is not required in either IE or Mozilla implementation.Anonymous
April 21, 2004
Huh? Of course XHTML is required. If you don't pass well formed HTML into msxml it will throw an exception.Anonymous
April 21, 2004
No, XHTML is not required. If all you are working with HTML then all you need to do is load MSXML. However the purpose of Dimitri's project is to provide XPath over regular HTML.Anonymous
April 21, 2004
After examining the code in further detail, I discovered that Dimitri's code is iterating through the entire HTML DOM, building a well formed XML clone as it progresses. Very ingenious indeed.Anonymous
April 22, 2004
23rAnonymous
June 14, 2004
XHTML is not required in either IE or Mozilla implementation.Anonymous
July 19, 2007
Just read another post by Dare with the XML team at Microsoft. My big question: Does this require theAnonymous
May 29, 2009
PingBack from http://paidsurveyshub.info/story.php?title=dare-obasanjo-s-weblog-xpath-for-the-html-dom-in-internet-explorer