def escape_attribute_value(attrval: str):
    """
    Escapes the special character in an attribute value
    based on RFC 4514.

    :param str attrval: the attribute value.
    :return: The escaped attribute value.
    :rtype: str
    """
    # Order matters.
    chars_to_escape = ("\\", '"', "+", ",", ";", "<", "=", ">")
    for char in chars_to_escape:
        attrval = attrval.replace(char, "\\{0}".format(char))
    if attrval[0] == "#" or attrval[0] == " ":
        attrval = "".join(("\\", attrval))
    if attrval[-1] == " ":
        attrval = "".join((attrval[:-1], "\\ "))
    attrval = attrval.replace("\0", "\\0")
    return attrval