code: protected Node popNode(Stack<Node> stack, Class<? extends Node> cls, String uri) {
          Node top = stack.isEmpty() ? null : stack.peek();
  
          if (top != null) {
              if (nodeMatches(top, cls, uri)) {
                  Node node = stack.pop();
                  poppedNodes.push(node);
                  return node;
              } else {
                  // Scan for potential match, from -2 so don't repeat
                  // check of top node
                  for (int i = stack.size() - 2; i >= 0; i--) {
                      if (nodeMatches(stack.get(i), cls, uri)) {
                          Node node = stack.remove(i);
                          poppedNodes.push(node);
                          return node;
                      }
                  }
              }
          }
  
          return null;
      }
docs: This method pops a node of the defined class and optional uri from the stack.
  If the uri is not defined, then the latest node of the approach class will
  be chosen.
  
  @param stack The stack
  @param cls The node type
  @param uri The optional uri to match
  @return The node, or null if no suitable candidate is found