Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« rsync to remote server via ssh
Shipping Software is fun »

Writing an XPath expression evaluator

March 3rd, 2005

I’ve been interested in XPath lately. I am investigating using XPath to query ‘Sloppy’ HTML documents instead of XML documents for the purpose of writing web tests. I’ve been using a CSS like syntax cobbled together with nasty regular expressions that don’t work in all cases. For example:

 
$this->assertTextInElement('div.Status', 'The Category has been added to the database.');
 

I’ve found that there is a certain synergy between the tests and the CSS syntax. The things that you want to test also tend to be the things that you want to style. I’m not sure that the XPath syntax will be as well suited to this purpose. On the other hand, XPath is certainly more available and more capable.

So today, as a learning exercise, I hacked together a toy interpreter for XPath expressions in native PHP. Mostly to familiarize myself with the Specification. (Using time I probably should have used for something else, I might add).

It uses the HTML parser from WACT, which is tolerant of errors and not as restrictive as an XML parser. It builds a simple DOM. Following the HTML convention, it auto closes open tags, allowing stuff like this:

 
<ul>
<li>item
</ul>
 

Its extremely limited. It only supports location paths. It only supports root, element, and text nodes. It only supports the child, descendant-or-self, self, descendant, parent, ancestor, and ancestor-or-self axis. It doesn’t support predicate syntax. It supports the *, text(), node() and name node tests.

My test cases and examples are based on these excellent examples.

Its not much, but here it is:

xpath.tar.gz

My next step is to familiarize myself with the other XPath options under PHP.

I might experiment with a CSS Selector evaluator using the same simple DOM.

Anyway, its very late now and I’m going to bed. :)

Filed Under

  • PHP

Related Posts

  • programming has warped my mind
  • The Problem with Markup Languages
  • php|architect Test Pattern
  • php | architect back issue bargains
  • Richard Thomas
You can leave a response, or trackback from your own site.

10 Responses to “Writing an XPath expression evaluator”

  1. Nelson Menezes says:
    3/3/2005 at 5:31 am

    Interesting idea, but I find this probably solves the original problem better:

    http://dean.edwards.name/my/#cssQuery.js

  2. Ryan Brooks says:
    3/3/2005 at 7:01 am

    Excellent! Thanks for sharing, we’ve been considering using XPath for our software, but if you’re finding it limited we may look into a custom development.

    Danke!

    -Ryan

  3. Kip Groot says:
    1/28/2011 at 1:51 pm

    Im happy I found this blog site, I couldnt locate any information on this subject prior to. I also operate a niche site and if you wish to ever serious in doing a bit of visitor writing for me please feel free to let me know, im always look for people to check out my weblog. Please stop by and leave a comment sometime!

  4. "hot yoga" says:
    12/10/2011 at 5:26 am

    Are you an official and proud yogi? Or are you a newbie who would want to be taught more about yoga? There are a number of yogis roaming around the planet, quietly spreading the word about this effective and full form of exercise and meditation. With the growing number of yogis, the recognition of yoga, aren’t you a bit confused on what it really is all about? How do you start in case you are a newbie? How do you keep up with new breathing exercises and positions without having to spend on lessons and instructors? The best solution: study yoga online. This is why and the way you are able to do it.

  5. teeth falling out dream says:
    12/14/2011 at 1:06 am

    To be a better person we must read valuable info like this

  6. Alva Sisk says:
    12/27/2011 at 4:02 am

    What i don’t understood is actually how you’re not actually much more well-liked than you might be right now. You are so intelligent. You realize therefore significantly relating to this subject, produced me personally consider it from a lot of varied angles. Its like women and men aren’t fascinated unless it is one thing to do with Lady gaga! Your own stuffs outstanding. Always maintain it up!

  7. JDM Cars says:
    2/2/2012 at 10:04 pm

    I’ve been gone for some time, but now I remember why I used to love this blog. Thank you, I’ll try and check back more often. How often do you update your site?

  8. underfloor heating systems cost says:
    3/25/2012 at 10:47 pm

    I’ve recently started a website, and the info you provide on this web site has helped me tremendously. Thank you for all of your time & work.

  9. Lanelle Dory says:
    4/8/2012 at 5:45 pm

    Was sind die Ersten Dementia Symptome zumal ist Dementia Kurabel kundig welche hinauf dieser Internetseite etliche. http://demenz27.onsugar.com/Wie-auch-gibt-es-berhaupt-Demenz-Symptome-22554350

  10. Britni Barbagallo says:
    4/24/2012 at 3:09 pm

    Mad Cow, thank you. The way some mothers I know talk, you’d think their children never throw tantrums (surely that can’t be true?) Glad to know there is at least one other mother out there who feels like me. And thanks to your readers for their comments above

Leave a Reply

Click here to cancel reply.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

    Subscribe Feed
    Share Subscribe to this blog…
    Share Bookmark or share this page…
  • About

    My name is Jeff Moore. I'm a PHP programmer living in San Francico and working for a startup.

    More about me…

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (18)
    • Open Source (14)
    • PHP (99)
    • Software Design (29)
    • Usability (14)
    • Web Design (20)
  • Recent Comments

    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  258
      Tuan Lal, Lavagem de estofados, Edward L. Kind [...]
    • php | tek 2008  36
      how to mend ice machine, Akademija Debelih, Odbacena [...]
    • goto in PHP  59
      kasor, Thomas Valdivieso, Murray Ziadie [...]
    • Firefox Extensions for Web Developers  33
      kasor, Website Design Toronto, mobila bistrita [...]
    • Why PHP is easier to learn than Java  68
      kasor, Justina Calvery, Guy Lipton [...]
    • Meta Tag Refresh Faux Paux  43
      html email templates, E-Juice Reviews, image [...]
    • Improved Error Messages in PHP 5  49
      Carroll Tina, Przeprowadzka, Emery Harari [...]
    • Benchmarking PHP's Magic Methods  33
      kayu oyunlar?,dora,oyun,oyna, Benjamin Bejjani, paypal website [...]
    • Microbenchmarks of single and double qouting.  24
      kefir grains minneapolis, sexshop dildo, tuim688 [...]
    • PEAR Templates  17
      Kandice Sansing, car insurance estimates for teenagers, Dale Brence [...]
  • Recent Posts

    • Richard Thomas
    • ZendCon: Writing Maintainable PHP Code
    • Looking Towards the Cloud
    • Holiday Tech Support
    • Closures are coming to PHP
    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
  • Site

    • Archives
    • Log in
  • Search