Dare Obasanjo's WebLog

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.  

Published Tuesday, April 20, 2004 11:38 AM by DareObasanjo

Comments

 

J. Michael Palermo IV said:

Would this require the HTML to be XHTML?
April 20, 2004 1:34 PM
 

Dimitri Glazkov said:

XHTML is not required in either IE or Mozilla implementation.
April 20, 2004 4:29 PM
 

Palermo4's Weblog said:

April 20, 2004 6:15 PM
 

Brian R. James said:

Huh? Of course XHTML is required. If you don't pass well formed HTML into msxml it will throw an exception.
April 21, 2004 10:36 AM
 

Dare Obasanjo said:

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.
April 21, 2004 10:47 AM
 

Brian R. James said:

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.
April 21, 2004 11:52 AM
 

Lance's Whiteboard said:

April 22, 2004 12:20 PM
 

wef said:

23r
April 23, 2004 3:20 AM
 

Palermo4's Weblog said:

May 23, 2004 1:39 AM
 

Dimitri Glazkov said:

XHTML is not required in either IE or Mozilla implementation.
June 15, 2004 4:30 AM
 

Lance's Whiteboard said:

September 1, 2004 3:48 PM
 

Palermo4 said:

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

July 19, 2007 10:33 AM
 

Dare Obasanjo s WebLog XPath for the HTML DOM in Internet Explorer | Paid Surveys said:

May 29, 2009 7:15 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker