Cleaning/sanitizing xpath attributes
问题 I need to dynamically construct an XPath query for an element attribute, where the attribute value is provided by the user. I'm unsure how to go about cleaning or sanitizing this value to prevent the XPath equivalent of a SQL injection attack. For example (in PHP): <?php function xPathQuery($attr) { $xml = simplexml_load_file('example.xml'); return $xml->xpath("//myElement[@content='{$attr}']"); } xPathQuery('This should work fine'); # //myElement[@content='This should work fine'] xPathQuery(