Choosing custom XML tags with phpQuery

phpQuery is a really good tool that has helped me a lot in the past to parse well-formed XHTML and XML documents, but I recently ran into a problem trying to select elements that have colons in their tag, for example:

<isc:thumb><![CDATA[http://example.com/foo_thumb.jpg]]></isc:thumb>

I tried using a function pq()to select all of these items:

foreach ( pq("isc:thumb") as $thumbnail ) {
  print pq( $thumbnail )->text();
}

Unfortunately, this does nothing. If I try another element, such as a tag id, the results will appear as expected.

+3
source share
1 answer

thumb, isc (. XML); isc:thumb.

phpQuery , , . namespace|tagname (.. isc|thumb). , phpQuery XPath ( DOMXPath), .

XML- (, XML , URI ).

phpQuery::newDocumentXML('<root xmlns:isc="urn.example.isc">
  <isc:thumb><![CDATA[http://example.com/foo_thumb.jpg]]></isc:thumb>
  <isc:thumb><![CDATA[http://example.com/bar_thumb.jpg]]></isc:thumb>
</root>
');
phpQuery::getDocument()->xpath->registerNamespace('isc', 'urn.example.isc');
foreach ( pq("isc|thumb") as $thumbnail ) {
    echo pq( $thumbnail )->text() . PHP_EOL;
}

:

http://example.com/foo_thumb.jpg
http://example.com/bar_thumb.jpg
+5

Source: https://habr.com/ru/post/1742611/


All Articles