Brian Slesinsky's Weblog
 
   

Tuesday, 08 Nov 2005

XPath Checker

[Updated with source code for developers.]

XPath Checker is a Firefox Extension I wrote for testing XPath expressions interactively. You might find it useful if you use xpaths or want to learn how.

If you haven't used xpaths before, they are sort of like regular expressions for web pages. XPaths make it easy to extract content from deeply nested markup in a web page or an XML document. I find them most useful for unit-testing code that generates XML or HTML. They're also used in XSLT and by Firefox extensions such as Greasemonkey.

Here are some examples that you could try out using XPath Checker:

Get the title of a page:

//title/text()

List all the images on a page:

//img

List the images that are inside a link:

//a//img

List the images that have alt tags:

//img[@alt]

List the images that don't have alt tags:

//img[not(@alt)]

Show all the alt tags:

//img/@alt

Show the href for every link:

//a/@href

Get an element with a particular CSS id:

//*[@id='mainContent']

To install XPath Checker, you need at least version 1.0.3 of Firefox. Once you have Firefox running, you can download XPath Checker from addons.mozilla.org and it will automatically install. Then you need to restart Firefox.

To use XPath Checker, right-click (or control-click if you have a Mac) anywhere on any web page and choose "View XPath" in the context menu. An editor window will pop up. The editor will show an XPath pointing to whatever you clicked on appear at the top of the window and the results of executing that XPath underneath. You can edit the XPath and the results automatically update as you type.

For developers: I'm releasing XPath Checker under the GPL. Here's the source code. If you get stuck, send me email and maybe I'll write some documentation.