Use XPath with PHP's SimpleXML to find nodes containing a String

playmaker4747

New Member
I try to use SimpleXML in combination with XPath to find nodes which contain a certain string.\[code\]<?php$xhtml = <<<EOC<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Test</title> </head> <body> <p>Find me!</p> <p> <br /> Find me! <br /> </p> </body></html>EOC;$xml = simplexml_load_string($xhtml);$xml->registerXPathNamespace('xhtml', 'http://www.w3.org/1999/xhtml');$nodes = $xml->xpath("//*[contains(text(), 'Find me')]");echo count($nodes);\[/code\]Expected output: 2Actual output: 1When I change the xhtml of the second paragraph to\[code\]<p> Find me! <br /> </p>\[/code\]then it works like expected. How has my XPath expression has to look like to match all nodes containing 'Find me' no matter where they are?Using PHP's DOM-XML is an option, but not desired.Thank's in advance!
 
Back
Top