phpQuery is a really nice tool which has helped me tremendously in the past parse well-formed XHTML and XML documents, but I have recently run into a problem trying to select elements that have colons in their tagname, such as the following:
<isc:thumb><![CDATA[http://example.com/foo_thumb.jpg]]></isc:thumb>
I’ve tried to use the pq() function to select all of these elements:
foreach ( pq("isc:thumb") as $thumbnail ) {
print pq( $thumbnail )->text();
}
Unfortunately this is doing nothing. If I try another element, like a tagname of id, the results pop up as expected.
You are trying to find a
thumbelement which belongs to theiscnamespace (see XML namespace); not a tag namedisc:thumb.phpQuery can happily query for namespaced elements, just not like you are trying to do. Instead, simply provide the tag in the form
namespace|tagname(i.e.isc|thumb). It is also worth noting that the namespace will have to be registered with phpQuery’s XPath handler (which is just aDOMXPathobject) for it to be able to recognise the namespace.Here’s a quick example with a sample XML document (obviously, use your own XML and be sure to provide the correct namespace URI).
Which outputs: