code: public Node getRootNode(Node n)
    {
      int nt = n.getNodeType();
      return ( (Node.DOCUMENT_NODE == nt) || (Node.DOCUMENT_FRAGMENT_NODE == nt) ) 
             ? n : n.getOwnerDocument();
    }
docs: Get the root node of the document tree, regardless of
  whether or not the node passed in is a document node.
  <p>
  TODO: This doesn't handle DocumentFragments or "orphaned" subtrees
  -- it's currently returning ownerDocument even when the tree is
  not actually part of the main Document tree. We should either
  rewrite the description to say that it finds the Document node,
  or change the code to walk up the ancestor chain.
  
  
  @param n Node to be examined
  
  @return the Document node. Note that this is not the correct answer
  if n was (or was a child of) a DocumentFragment or an orphaned node,
  as can arise if the DOM has been edited rather than being generated
  by a parser.