{% apply spaceless %}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" {% block namespaces %}xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"{% endblock %}>
    {% set registrableDomain = sulu_util_domain_info(domain).registrableDomain %}

    {% for entry in entries %}
        {% set location = entry.loc %}

        {% if sulu_util_domain_info(location).registrableDomain == registrableDomain %}
            <url>
                {% block url %}
                    <loc>{{ location }}</loc>
                    {% if entry.lastmod %}
                        <lastmod>{{ entry.lastmod|date('Y-m-d') }}</lastmod>
                    {% endif %}
                    {% if entry.changefreq %}
                        <changefreq>{{ entry.changefreq }}</changefreq>
                    {% endif %}
                    {% if entry.priority %}
                        <priority>{{ entry.priority }}</priority>
                    {% endif %}

                    {% set amount = 0 %}
                    {% set defaultHref = null %}
                    {% if entry.alternateLinks|length > 1 %}
                        {% for alternateLink in entry.alternateLinks %}
                            {% set href = alternateLink.href %}
                            {% if href is not empty and sulu_util_domain_info(href).registrableDomain == registrableDomain %}
                                <xhtml:link rel="alternate" hreflang="{{ alternateLink.locale|replace({'_': '-'}) }}" href="{{ href }}"/>
                                {% set amount = amount + 1 %}
                                {% if entry.defaultLocale == alternateLink.locale %}
                                    {% set defaultHref = href %}
                                {% endif %}
                            {% endif %}
                        {% endfor %}
                    {% endif %}

                    {% if defaultHref and amount > 0 %}
                        <xhtml:link rel="alternate" hreflang="x-default" href="{{ defaultHref }}"/>
                    {% endif %}
                {% endblock %}
            </url>
        {% endif %}
    {% endfor %}
</urlset>
{% endapply %}
