If the query() function seems to ignore your $contextnode, and instead returns all the tags in the document, try to use a relative path (use a . in front of the query):
<?php
$xml = "<?xml version='1.0' encoding='UTF-8'?>
<test>
<tag1>
<uselesstag>
<tag2>test</tag2>
</uselesstag>
</tag1>
<tag2>test2</tag2>
</test>";
$dom = new DomDocument();
$dom->loadXML($xml);
$xpath = new DomXPath($dom);
$tag1 = $dom->getElementsByTagName("tag1")->item(0);
echo $xpath->query("//tag2")->length; echo $xpath->query("//tag2", $tag1)->length; echo $xpath->query(".//tag2", $tag1)->length; ?>
See that i couldn't use $xpath->query("tag2", $tag1) as per the documentation, since "tag2" is not a direct child of "tag1".
I don't know why this note was deleted, i just tested it and it's correct.
It's not a bug, it's simply not written in the documentation.