import re


def enclosed_in_tag(source_text, tag, loc):
    """
    Determine whether within the `source_text`, the element present at `loc` is enclosed within the XML `tag`.

    :param source_text: a string that possibly contains some XML markup.
    :type source_text: :class:`str`
    :param tag: a string specifying an XML tag.
    :type tag: :class:`str`
    :param loc: the location to test for enclosure.
    :type loc: :class:`int`

    :return: a boolean indicating whether `loc` is enclosed in the specified `tag`.
    :rtype: :class:`bool`

    Example:
    ::
        source_text = '<foo>bar</foo>'
        tag = 'foo'
        loc = 6
        enclosed_in_tag(source_text, tag, loc)
        True
    """
    trailing_text = source_text[loc:]
    close_tag = '</{}>'.format(tag)
    first_open_tag = re.search('<[^\/].*?>', trailing_text)
    first_closed_tag = re.search('<\/.*?>', trailing_text)
    if not first_closed_tag:
        return False
    else:
        if first_open_tag is not None and first_open_tag.start() < first_closed_tag.start():
            return False
        elif first_closed_tag.group(0) == close_tag:
            return True
        else:
            return False