What best practice can be recommended for efficiently searching for multiple strings in an XML document using PHP?

When searching for multiple strings in an XML document using PHP, it is best practice to use XPath queries to efficiently locate the desired elements. By constructing XPath expressions that target specific nodes containing the strings, you can streamline the search process and retrieve the necessary data more effectively.

$xml = simplexml_load_file('example.xml');

$strings = ['string1', 'string2', 'string3'];

foreach ($strings as $string) {
    $results = $xml->xpath("//*[contains(text(), '$string')]");
    
    foreach ($results as $result) {
        // Process the matching elements
        echo $result->asXML() . "\n";
    }
}