If it works with integers, but not with strings, you probably just need to quote your variable in your query:
$result2= $xpath2->query('/root[@name="gf"]/node1[@id="'. $id2.'"]');
If it works with integers, but not with strings, you probably just need to quote your variable in your query:
$result2= $xpath2->query('/root[@name="gf"]/node1[@id="'. $id2.'"]');