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 (13)

  1. Would this require the HTML to be XHTML?

  2. XHTML is not required in either IE or Mozilla implementation.

  3. Huh? Of course XHTML is required. If you don’t pass well formed HTML into msxml it will throw an exception.

  4. 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.

  5. 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.

  6. Dimitri Glazkov says:

    XHTML is not required in either IE or Mozilla implementation.

  7. Palermo4 says:

    Just read another post by Dare with the XML team at Microsoft. My big question: Does this require the