import re


def _remove_trailing_whitespace(xstr):
    """Remove trailing white space.
    
    There is a lot of white space between </x:xmpmeta> and <?xpacket end="w"?>,
    but we don't need that for presentation purposes.
    """
    regex = re.compile(r"""</x:xmpmeta>
                           (?P<whitespace>\s*)
                           <\?xpacket\s
                           end="w"\?>""", re.UNICODE | re.VERBOSE)
    match = regex.search(xstr)
    if match is not None:
        # Ok we matched up to the whitespace, get rid of it.
        ws_start, ws_end = match.span('whitespace')
        xstr = xstr[0:ws_start] + '\n' + xstr[ws_end:]

    return xstr