def escape_attribute(value, encoding):
    """Escape an attribute value using the given encoding."""
    if '&' in value:
        value = value.replace('&', '&amp;')
    if '<' in value:
        value = value.replace('<', '&lt;')
    if '"' in value:
        value = value.replace('"', '&quot;')
    return value.encode(encoding, 'xmlcharrefreplace')
