def prepend_xpath(pre, xpath, glue=False):
    """Prepend some xpath to another, properly joining the slashes

    """

    if (not pre) or xpath.startswith('//'):
        # prefix doesn't matter
        return xpath
    if pre.endswith('./'):
        if xpath.startswith('./'):
            return pre[:-2] + xpath
        elif xpath.startswith('/'):  # including '//'
            return pre[:-1] + xpath
    elif pre.endswith('//'):
        return pre + xpath.lstrip('/')
    elif pre.endswith('/') and xpath.startswith('/'):
        return pre[:-1] + xpath
    elif pre.endswith('/') and xpath.startswith('./'):
        return pre + xpath[2:]
    elif pre.endswith('::'):
        if xpath.startswith('.//'):
            return pre + '*/descendant-or-self::' + xpath[3:]
        elif xpath.startswith('./'):
            return pre + xpath[2:]
        else:
            return pre + xpath.lstrip('/')
    elif xpath.startswith('./'):
        return pre + xpath[1:]
    elif glue and not pre.endswith('/') and xpath[0].isalpha():
        if glue is True:
            glue = '/'
        return pre + glue + xpath

    return pre + xpath