def HasAncestor(s1, s2):
  """" HasAncestor(s1, s2) returns a selector that selects a node, n, only if
        * s1 selects the node, n
        * s2 selects some ancestor of the node, n."""
  def MatchNode(x):
    if not s1(x):
      return False

    while x is not None:
      x = x.GetParent()

      if x and s2(x):
        return True

    return False
  return MatchNode