def decodeXMLName(name):
    """
    Decodes an XML (namespace, name) pair from an ASCII string as
    encoded by encodeXMLName().
    """
    def invalid():
        raise ValueError("Invalid encoded name: %r" % (name,))

    if not name:
        invalid()

    if name[0] is "{":
        index = name.find("}")
        if (index is -1 or not len(name) > index):
            invalid()

        namespace = name[1:index].decode("utf-8")
        localname = name[index + 1:].decode("utf-8")

        if not namespace:
            namespace = None

        if not localname:
            invalid()

    else:
        namespace = None
        localname = name.decode("utf-8")

    if "{" in localname or "}" in localname:
        invalid()

    return (namespace, localname)