Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jamaal 6 posts 116 karma points
    Apr 22, 2024 @ 09:19
    Jamaal
    0

    Sitemap rendering issue

    Hello,

    I'm facing an issue with the rendered code in sitemap. I need to render this code : <xhtml:link rel="alternate"... But the :link is creating plain text rather than the proper xml display.

    @helper RenderHrefLangEntries(IPublishedContent node)
    {
        if (node.Cultures != null)
        {
            foreach (var culture in node.Cultures)
            {
                var cultureKey = culture.Key;
                var cultureUrl = EnsureUrlStartsWithDomain(node.Url(cultureKey, UrlMode.Absolute));
                @Html.Raw($"<xhtml:link rel=\"alternate\" hreflang=\"{cultureKey}\" href=\"{cultureUrl}\" />")
            }
        }
    }
    

    I understand that the issue is related to the colon character. Is there an alternative way to display it correctly?

    Thanks

  • Stefan Stankovic 17 posts 154 karma points
    Apr 22, 2024 @ 11:13
    Stefan Stankovic
    0

    Based on the Umbraco official document I made this Service. I hope it will help you:

    public class XmlSiteMapService : IXmlSiteMapService
    {
        public string RenderXmlSiteMap(IPublishedContent node, int? maxSiteMapDepth, string? excludedDocumentTypeList)
        {
            var maxSiteMapDepthValue = maxSiteMapDepth.HasValue ? maxSiteMapDepth.Value : int.MaxValue;
            var excludedDocumentTypes = (!string.IsNullOrEmpty(excludedDocumentTypeList)) ? excludedDocumentTypeList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray() : new string[] { };
            var xmpMapForHomePage = RenderSiteMapUrlEntry(node);
            var xmlMapForChildren = RenderSiteMapUrlEntriesForChildren(node, maxSiteMapDepthValue, excludedDocumentTypes);
            var retVal = new StringBuilder();
            retVal.AppendLine("<urlset " +
                "xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" " +
                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                "xsi:schemalocation=\"http://www.google.com/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" " +
                "xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\">");
            retVal.AppendLine(xmpMapForHomePage);
            retVal.AppendLine(xmlMapForChildren);
            retVal.AppendLine("</urlset>");
            return retVal.ToString();
        }
        private string RenderSiteMapUrlEntry(IPublishedContent node)
        {
            var changeFreq = node.Value("searchEngineChangeFrequency", fallback: Fallback.To(Fallback.Ancestors, Fallback.DefaultValue), defaultValue: "monthly");
            if (changeFreq == null)
            {
                return string.Empty;
            }
            var priority = node.HasValue("searchEngineRelativePriority") ? node.Value<string>("searchEngineRelativePriority") : "0.5";
            var xmlContent = $$"""
                                <url>
                                    <loc>{{node.Url(mode: UrlMode.Absolute)}}</loc>
                                    <lastmod>{{string.Format("{0:s}+00:00", node.UpdateDate)}}</lastmod>
                                    <changefreq>{{changeFreq}}</changefreq>
                                    <priority>{{priority}}</priority>
                                </url>
                                """;
            return xmlContent;
        }
        private string RenderSiteMapUrlEntriesForChildren(IPublishedContent parentPage, int maxSiteMapDepth, string[] excludedDocumentTypes)
        {
            var retVal = new StringBuilder();
            foreach (var page in parentPage.Descendants().Where(x =>
                !excludedDocumentTypes.Contains(x.ContentType.Alias) &&
                x.HasValue("hideFromXmlSiteMap") &&
                !x.Value<bool>("hideFromXmlSiteMap")))
            {
                retVal.AppendLine(RenderSiteMapUrlEntry(page));
                if (page.Level < maxSiteMapDepth && page.Children.Any(x => !x.Value<bool>("hideFromXmlSiteMap")))
                {
                    retVal.AppendLine(RenderSiteMapUrlEntriesForChildren(page, maxSiteMapDepth, excludedDocumentTypes));
                }
            }
            return retVal.ToString();
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft