What are some potential pitfalls of using xPath in PHP to manipulate XML documents?

One potential pitfall of using xPath in PHP to manipulate XML documents is the risk of injection attacks if the xPath query is constructed using user input. To prevent this, it is important to properly sanitize and validate any user input before using it in an xPath query.

// Sanitize and validate user input before using it in an xPath query
$userInput = $_POST['user_input'];
$sanitizedInput = filter_var($userInput, FILTER_SANITIZE_STRING);

// Use the sanitized input in the xPath query
$xml = new DOMDocument();
$xml->load('file.xml');
$xpath = new DOMXPath($xml);
$query = "//node[@attribute='$sanitizedInput']";
$results = $xpath->query($query);

// Process the results
foreach ($results as $result) {
    // Do something with the result
}