def _qname_matches(tag, namespace, qname):
    """Logic determines if a QName matches the desired local tag and namespace.

    This is used in XmlElement.get_elements and XmlElement.get_attributes to
    find matches in the element's members (among all expected-and-unexpected
    elements-and-attributes).

    Args:
      expected_tag: string
      expected_namespace: string
      qname: string in the form '{xml_namespace}localtag' or 'tag' if there is
             no namespace.

    Returns:
      boolean True if the member's tag and namespace fit the expected tag and
      namespace.
    """
    if qname is None:
        member_tag = None
        member_namespace = None
    elif qname.startswith('{'):
        member_namespace = qname[1:qname.index('}')]
        member_tag = qname[qname.index('}') + 1:]
    else:
        member_namespace = None
        member_tag = qname
    return (tag is None and namespace is None or namespace is None and 
        member_tag == tag or tag is None and member_namespace == namespace or
        tag is None and namespace == '' and member_namespace is None or tag ==
        member_tag and namespace == member_namespace or tag == member_tag and
        namespace == '' and member_namespace is None)
