What are the potential pitfalls of using XPath to navigate and manipulate a DOMDocument in PHP?

Potential pitfalls of using XPath to navigate and manipulate a DOMDocument in PHP include performance issues when dealing with large XML documents, complexity in writing and maintaining XPath queries, and potential security vulnerabilities if user input is directly incorporated into XPath queries without proper validation. To mitigate these pitfalls, it is recommended to carefully optimize XPath queries, cache results where possible, and sanitize user input before using it in XPath queries.

// Example of sanitizing user input before using it in an XPath query
$userInput = $_GET['query'];
$cleanedInput = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');
$query = "//node[@attribute='$cleanedInput']";