def gpx_fields_to_xml(instance, tag, version, custom_attributes=None):
    fields = instance.gpx_10_fields
    if version == '1.1':
        fields = instance.gpx_11_fields

    tag_open = bool(tag)
    body = ''
    if tag:
        body = '\n<' + tag
        if custom_attributes:
            for key, value in custom_attributes.items():
                body += ' %s="%s"' % (key, mod_utils.make_str(value))

    for gpx_field in fields:
        if isinstance(gpx_field, str):
            if tag_open:
                body += '>'
                tag_open = False
            if gpx_field[0] == '/':
                body += '<%s>' % gpx_field
            else:
                body += '\n<%s' % gpx_field
                tag_open = True
        else:
            value = getattr(instance, gpx_field.name)
            if gpx_field.attribute:
                body += ' ' + gpx_field.to_xml(value, version)
            elif value:
                if tag_open:
                    body += '>'
                    tag_open = False
                xml_value = gpx_field.to_xml(value, version)
                if xml_value:
                    body += xml_value

    if tag:
        if tag_open:
            body += '>'
        body += '</' + tag + '>'

    return body
dataset/ETHPy150Open tkrajina/gpxpy/gpxpy/gpxfield.py/gpx_fields_to_xml/original