def normalize_space(data):
    """Implements attribute value normalization

    Returns data normalized according to the further processing rules
    for attribute-value normalization:

        "...by discarding any leading and trailing space (#x20)
        characters, and by replacing sequences of space (#x20)
        characters by a single space (#x20) character"
    """
    result = []
    scount = 2  # 0=no space; 1=add space; 2=don't add space
    for c in data:
        if c == ' ':
            if scount == 0:
                scount = 1
        else:
            if scount == 1:
                result.append(' ')
            result.append(c)
            scount = 0
    return ''.join(result)