Simplify handling of different iterator types.

locate_node_by_label now works with subtree iterators and returns
the correct type.  The checking of terminal-ness is farmed out
to another function to hide the ugly cast.