I propose moving $xpath->query into the function as well so that we can pass $path to it.