/// <inheritdoc /> public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context) { StringBuilder sb = new StringBuilder(); sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"); context.ForEach(inputs, input => { // Try to get a SitemapItem object delegateResult = _sitemapItemOrLocation(input, context); SitemapItem sitemapItem = delegateResult as SitemapItem ?? new SitemapItem((delegateResult as string) ?? context.GetLink(input)); // Add a sitemap entry if we got an item and valid location if (!string.IsNullOrWhiteSpace(sitemapItem?.Location)) { string location = sitemapItem.Location; // Apply the location formatter if there is one if (_locationFormatter != null) { location = _locationFormatter(location); } // Apply the hostname if defined (and the location formatter didn't already set a hostname) if (!string.IsNullOrWhiteSpace(location)) { if (!location.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) && !location.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)) { location = context.GetLink(new FilePath(location), true); } } // Location being null signals that this document should not be included in the sitemap if (!string.IsNullOrWhiteSpace(location)) { sb.Append("<url>"); sb.AppendFormat("<loc>{0}</loc>", location); if (sitemapItem.LastModUtc.HasValue) { sb.AppendFormat("<lastmod>{0}</lastmod>", sitemapItem.LastModUtc.Value.ToString("yyyy-MM-ddTHH:mm:ssZ")); } if (sitemapItem.ChangeFrequency.HasValue) { sb.AppendFormat("<changefreq>{0}</changefreq>", ChangeFrequencies[(int)sitemapItem.ChangeFrequency.Value]); } if (sitemapItem.Priority.HasValue) { sb.AppendFormat(CultureInfo.InvariantCulture, "<priority>{0}</priority>", sitemapItem.Priority.Value); } sb.Append("</url>"); } } }); // Always output the sitemap document, even if it's empty sb.Append("</urlset>"); return(new[] { context.GetDocument(context.GetContentStream(sb.ToString())) }); }
public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutionContext context) { StringBuilder sb = new StringBuilder(); sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"); foreach (IDocument input in inputs) { // Try to get a SitemapItem object delegateResult = _sitemapItemOrLocation(input, context); SitemapItem sitemapItem = delegateResult as SitemapItem; if (sitemapItem == null) { string locationDelegateResult = delegateResult as string; if (!string.IsNullOrWhiteSpace(locationDelegateResult)) { sitemapItem = new SitemapItem(locationDelegateResult); } } // Add a sitemap entry if we got an item and valid location if (!string.IsNullOrWhiteSpace(sitemapItem?.Location)) { string location = sitemapItem.Location; // Apply the location formatter if there is one if (_locationFormatter != null) { location = _locationFormatter(location); } // Apply the hostname if defined (and the location formatter didn't already set a hostname) if (!string.IsNullOrWhiteSpace(location)) { if (!location.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase)) { location = context.GetLink(new FilePath(location), true); } } // Location being null signals that this document should not be included in the sitemap if (!string.IsNullOrWhiteSpace(location)) { sb.Append("<url>"); sb.AppendFormat("<loc>{0}</loc>", location); if (sitemapItem.LastModUtc.HasValue) { sb.AppendFormat("<lastmod>{0}</lastmod>", sitemapItem.LastModUtc.Value.ToString("yyyy-MM-ddTHH:mm:ssZ")); } if (sitemapItem.ChangeFrequency.HasValue) { sb.AppendFormat("<changefreq>{0}</changefreq>", ChangeFrequencies[(int)sitemapItem.ChangeFrequency.Value]); } if (sitemapItem.Priority.HasValue) { sb.AppendFormat(CultureInfo.InvariantCulture, "<priority>{0}</priority>", sitemapItem.Priority.Value); } sb.Append("</url>"); } } } // Always output the sitemap document, even if it's empty sb.Append("</urlset>"); return new[] { context.GetDocument(sb.ToString()) }; }