Urs told me about Carrubbers.org’s XPath class for PHP. It’s a standalone class which works with a stock build of PHP, you don’t need the LIBXML2 extensions, just the built in XML support.
The class supports loading an XML document from a string, file, or URL. Then you can query the document with XPath and get a list of matching nodes back. You can also update, insert, and remove nodes specified by an XPath query.
One useful feature is getting the text node of an element by reference. You can bind the reference to a variable, and update the node by assigning a new value to the variable. I put together a prototype for editing an XML document through a form using that trick this afternoon.
One drawback of the class is that inserts, deletitions, and updates require what the writers refer to as an absolute XPath to the node. So /foo/bar/baz, even if it selects a unique node, is not sufficient, you need /foo[1]/bar[1]/baz[1].
Update: no, that’s not the case. See the updated notes on the class.
The LIBXML2 extension will be faster, of course, since it’s a C++ library, but if you need the functionality in a stock build of PHP, or can’t change the one you get from your web host, this is a good choice.
Possibly Related posts (machine generated):