XPath
-
Using PHP Functions in XPath Expressions
Disclaimer: this article expects familiarity with using the DOM1 extension and XPath2 expressions.
The (
currently undocumentednow documented3)DOMXPath::registerPHPFunctionsmethod is available as of PHP 5.3.0 (it was added to the code base back in December 2006) and allows the use of PHP functions (and static methods) within XPath queries to complement the normal set of XPath functions2. -
XPath for CSS classes
I was playing around with YQL today and was scraping a HTML document (no details, that’s not the point of this blog entry). Part of the processes meant that I needed to check for a specific CSS class attached to some elements.
Easy enough, XPath contains (pardon the pun, see later) some nifty functions which can act on nodes and their values to be used in conditional checks like I needed. I’ve been using the technique outlined in this post for a very long while (usually with XPath in PHP) and originally stumbled upon the basic idea from How to map CSS selectors to XPath queries (Thanks ever so much!) Today’s use of it prompted this blog post.