sitemap

package module
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-sitemap-parser

codecov Go Go Reference OpenSSF Scorecard Mentioned in Awesome Go

A Go package to parse XML Sitemaps compliant with the Sitemaps.org protocol.

For information on reporting security vulnerabilities, see SECURITY.md.

Features

  • Recursive parsing (sitemap index → sitemaps → URLs)
  • Concurrent (multi-threaded) fetching and parsing
  • Configurable follow rules to filter which sitemaps to parse
  • Configurable URL rules to filter which URLs to include
  • Configurable HTTP response size limit
  • Limits on the sitemaps a call fetches and on the URLs it collects, so that a call comes to an end whatever a server lists
  • Tolerant mode (default): resolves relative URLs in <loc> elements; rejects URLs exceeding 2,048 characters after resolution
  • Strict mode: validates URLs per the sitemaps.org specification
  • A value that cannot be parsed (e.g. a malformed <lastmod>) costs only itself, never the whole sitemap
  • Google Image Sitemap extension (<image:image>)
  • Google News Sitemap extension (<news:news>)
  • Google Video Sitemap extension (<video:video>)
  • XHTML hreflang extension (<xhtml:link>)
  • XML documents in encodings other than UTF-8 (e.g. ISO-8859-1, windows-1252) are transcoded automatically
  • Typed errors: *ConfigError, *NetworkError, *ParseError, *ValidationError — inspectable via errors.As
  • Thread-safe

Formats supported

  • robots.txt
  • XML .xml
  • RSS 2.0
  • Atom 1.0
  • Plain text .txt
  • Gzip compressed files (e.g., .xml.gz, .txt.gz)

XML documents do not have to be UTF-8 encoded, and a document may begin with a UTF-8 byte order mark, see Character encoding.

A document of any of these formats may be gzip compressed. A gzip file is read to the end of its last member: a file that was appended to, or put together from several (cat a.gz b.gz), holds a member for each part, and its content is that of all of them. What follows the last member and is no member, such as a newline, is ignored. Content that cannot be decompressed to its end is not parsed, and is reported once, as a *ParseError (gzip decompression failed: unexpected EOF). See examples/gzip for a runnable example.

A robots.txt is recognised by the URL passed to Parse(): the document is read as one when the last segment of the path of that URL is robots.txt. The letter case makes no difference, and neither does a query or a fragment that follows the path, so https://example.com/robots.txt?v=2 and https://example.com/ROBOTS.TXT are read as a robots.txt too. What follows the path is no part of it: https://example.com/sitemap.xml?from=/robots.txt is the URL of a sitemap, and is parsed as one. It is the URL passed in that decides, not the one a redirect leads to, and only the document passed to Parse() can be a robots.txt: one that a sitemap index lists is parsed as a sitemap.

Of a robots.txt only the Sitemap: lines are read. The name of the directive is not case-sensitive, the line may be indented, and what follows a # is a comment. The sitemaps the lines name are fetched and parsed; how their URLs are checked is described under Strict mode. See examples/robots for a runnable example.

Only the location of a page is taken from a feed or a text sitemap, so LastMod, ChangeFreq and Priority are nil for their URLs:

  • RSS: the <link> of every <item>, see examples/rss
  • Atom: for every <entry>, the first <link> with rel="alternate" or without a rel attribute, see examples/atom
  • Plain text: every line that begins with http:// or https://; empty lines, lines that begin with # and every other line are skipped, see examples/text

Requirements

Go 1.26 or later.

Installation

go get github.com/aafeher/go-sitemap-parser
import "github.com/aafeher/go-sitemap-parser"

Usage

Create instance

To create a new instance with default settings, you can simply call the New() function.

s := sitemap.New()
Configuration defaults
  • userAgent: "go-sitemap-parser (+https://github.com/aafeher/go-sitemap-parser/blob/main/README.md)"
  • fetchTimeout: 3 seconds
  • maxResponseSize: 52428800 (50 MB)
  • maxDepth: 10
  • maxConcurrency: 16
  • maxSitemaps: 50000
  • maxURLs: 10000000
  • multiThread: true
  • strict: false
  • httpClient: nil (a default *http.Client is created per call with the configured fetchTimeout)
Overwrite defaults
User Agent

To set the user agent, use the SetUserAgent() function.

s := sitemap.New()
s = s.SetUserAgent("YourUserAgent")

... or ...

s := sitemap.New().SetUserAgent("YourUserAgent")
Fetch timeout

To set the fetch timeout, use the SetFetchTimeout() function. It should be specified in seconds as a uint16 value (1–65535 seconds). A value of 0 is rejected and a *ConfigError is recorded. Note: when a custom HTTP client is set via SetHTTPClient(), this value has no effect — the client's own Timeout field controls the request deadline.

s := sitemap.New()
s = s.SetFetchTimeout(10)

... or ...

s := sitemap.New().SetFetchTimeout(10)
Max response size

To set the maximum allowed HTTP response size, use the SetMaxResponseSize() function. It should be specified in bytes as an int64 value. The default is 50 MB, matching the sitemaps.org protocol limit. Responses exceeding this limit will result in an error.

The same limit caps the decompressed size of gzip-compressed content, of all the members of a gzip file together, so a small .gz response cannot expand without bound in memory. This applies both to fetched content and to gzip content passed in through the urlContent argument of Parse(). Content that expands beyond the limit is rejected and reported via GetErrors() as a *ParseError. If it is the content of the URL passed to Parse(), the call fails with that error, see Parse.

The value must be greater than 0. Unlike with SetMaxSitemaps() and SetMaxURLs(), 0 does not lift the limit: it is rejected with a *ConfigError and the limit stays as it was. Every value above it is a limit, up to math.MaxInt64, which nothing can exceed.

s := sitemap.New()
s = s.SetMaxResponseSize(10 * 1024 * 1024) // 10 MB

... or ...

s := sitemap.New().SetMaxResponseSize(10 * 1024 * 1024) // 10 MB
Max depth

To set the maximum recursion depth for following sitemap indexes, use the SetMaxDepth() function. A sitemap index may reference other sitemap indexes; this limits how many levels deep the parser will follow. The default is 10.

s := sitemap.New()
s = s.SetMaxDepth(5)

... or ...

s := sitemap.New().SetMaxDepth(5)

With SetMaxDepth(1) the sitemaps listed by the document passed to Parse() are fetched, but if one of them is a sitemap index itself, the sitemaps it lists are not. A robots.txt does not count as a level: the sitemaps it names are treated like the document passed to Parse().

When the limit is reached, a *ParseError is recorded in GetErrors() for every sitemap index whose sitemaps are not followed. It names that sitemap index, by the URL it was served from:

parse "https://example.com/sitemap-index-2.xml": max recursion depth of 1 reached

Reaching the limit does not fail Parse(): the URLs collected up to that depth are returned.

See examples/maxdepth for a runnable example.

Max sitemaps

A sitemap index may list sitemap indexes, which may list further ones, so how many sitemaps a call ends up fetching is up to the server. To limit the number of sitemaps a Parse() / ParseContext() call fetches, use the SetMaxSitemaps() function.

The value is an int:

  • 0: no limit.
  • a positive value: the call fetches at most that many sitemaps. The default is 50000, which is as many sitemaps as a sitemap index may list according to the sitemaps.org protocol.
s := sitemap.New()
s = s.SetMaxSitemaps(1000)

... or ...

s := sitemap.New().SetMaxSitemaps(1000)

The limit is one of the call, not of a sitemap index: the sitemaps a robots.txt or a sitemap index lists count on all levels together.

  • Every sitemap the call requests counts, whether or not the request succeeds.
  • A sitemap that is listed more than once is requested, and counted, once.
  • The document passed to Parse() does not count.

Once the limit is reached, the sitemaps that are left are not fetched, and a *ParseError is recorded in GetErrors(), once for the call. It names the URL passed to Parse():

parse "https://example.com/sitemap-index.xml": limit of 1000 sitemaps reached

Reaching the limit does not fail Parse(): the URLs of the sitemaps fetched until then are returned. With multi-threading off, the sitemaps are fetched in the order they are listed, so it is the first ones that are fetched. With multi-threading on, which ones are fetched depends on how fast the server answers. Nothing is recorded if no sitemap had to be left out.

Negative values are rejected and an error is recorded in GetErrors(). The limit of a call is the one set when the call starts.

See examples/maxsitemaps for a runnable example.

Max URLs

The URLs of a call are kept in memory until the next call, and how many there are is up to the server as well. To limit the number of URLs a Parse() / ParseContext() call collects, use the SetMaxURLs() function.

The value is an int:

  • 0: no limit.
  • a positive value: the call collects at most that many URLs. The default is 10000000.
s := sitemap.New()
s = s.SetMaxURLs(100000)

... or ...

s := sitemap.New().SetMaxURLs(100000)

The limit is one of the call as well: the URLs of all the sitemaps count together. What counts are the URLs that GetURLs() returns: an entry that is not valid does not count, and neither does a URL that the patterns set with SetRules() leave out.

Once the limit is reached, the call stops collecting:

  • what is left of the document that reached the limit is left out unread, so it adds no errors either;
  • the sitemaps that are left are not fetched, and a sitemap that was under way is not parsed;
  • a *ParseError is recorded in GetErrors(), once for the call. It names the URL passed to Parse():
parse "https://example.com/sitemap-index.xml": limit of 100000 URLs reached

Reaching the limit does not fail Parse(): the URLs collected until then are returned. With multi-threading off, they are the first ones in the order the sitemaps list them. With multi-threading on, which ones they are depends on how fast the server answers. Nothing is recorded if nothing had to be left out.

The default is a last resort rather than a tight bound: ten million URLs take up gigabytes of memory. Set a lower limit when the documents are not trusted, see SECURITY.md.

Negative values are rejected and an error is recorded in GetErrors(). The limit of a call is the one set when the call starts.

See examples/maxurls for a runnable example.

Max concurrency

When multi-threaded parsing is enabled, the sitemaps a sitemap index or a robots.txt lists are fetched and parsed concurrently. For very large sitemap indexes this can mean a large number of goroutines, HTTP connections and documents in memory at once. To bound the number of sitemaps that are worked on at the same time across the whole Parse() / ParseContext() call, use the SetMaxConcurrency() function.

The value is an int:

  • 0: unlimited concurrency.
  • a positive value: at most that many sitemaps are being fetched or parsed at any time. The default is 16.

A sitemap counts from the moment its request is sent until its content is parsed, so the limit bounds the connections that are open, the CPU cores that are busy and the documents that are held in memory at once. Parsing a sitemap takes several times the memory of the document itself; a lower limit trades speed for a smaller footprint.

A sitemap that waits for its turn takes up none of these, and no goroutine either: a goroutine is started for a sitemap only when there is a slot for it. However many sitemaps an index lists, the goroutines of the parser are those of the sitemaps being worked on, plus one for every sitemap index whose sitemaps are not finished yet. With SetMaxConcurrency(0) a goroutine is started, and a request sent, for every sitemap listed at once.

Negative values are rejected and an error is recorded in GetErrors().

s := sitemap.New()
s = s.SetMaxConcurrency(8)

... or ...

s := sitemap.New().SetMaxConcurrency(8)

Cancelling the supplied context.Context while sitemaps wait for a slot ends the wait at once. The sitemaps that were still waiting are not fetched, and nothing is recorded for them: that the call was cut short is reported once for the call, see Parse with context.

Multi-threading

By default, the package uses multi-threading to fetch and parse sitemaps concurrently. To set the multi-thread flag on/off, use the SetMultiThread() function.

With multi-threading on, each sitemap is fetched, unzipped and decoded by a goroutine of its own, so the sitemaps of an index are parsed on as many CPU cores as SetMaxConcurrency() allows. The URLs of a sitemap stay together in GetURLs(), in the order the sitemap lists them; which sitemap comes first depends on which one is finished first.

With multi-threading off, the sitemaps are fetched and parsed one at a time and in the order they are listed, so no more than one request is in flight at any time. This goes for the sitemaps a robots.txt names as well as for those of a sitemap index. SetMaxConcurrency() has no effect in this case.

s := sitemap.New()
s = s.SetMultiThread(false)

... or ...

s := sitemap.New().SetMultiThread(false)

See examples/multithread for a runnable example.

Follow rules

To set the follow rules, use the SetFollow() function. It should be specified a []string value. It is a list of regular expressions. Only the sitemaps whose URL matches one of these expressions are fetched and parsed, whether a sitemap index lists them or a robots.txt names them. A sitemap index that is not followed takes the sitemaps it lists with it. The URL passed to Parse() is always fetched. If no follow rules are provided, all sitemaps are followed. Patterns longer than 1,000 characters are rejected and reported via GetErrors().

The expressions are matched against the absolute URL of a sitemap, a relative one being resolved first. In tolerant mode that is the URL as it is requested: with a space or a character that is not in ASCII percent-encoded (m%C3%BCnchen, not münchen), and with an internationalised host name in its Punycode form. They match anywhere in the URL unless they are anchored: example\.com also matches https://example.com.evil.test/sitemap.xml, ^https://example\.com/ does not.

s := sitemap.New()
s.SetFollow([]string{
	`\.xml$`,
	`\.xml\.gz$`,
})

... or ...

s := sitemap.New().SetFollow([]string{
	`\.xml$`,
	`\.xml\.gz$`,
})

See examples/follow for a runnable example.

URL rules

To set the URL rules, use the SetRules() function. It should be specified a []string value. It is a list of regular expressions. Only URLs that match one of these expressions will be included in the final result. The expressions are matched against the URL as GetURLs() returns it. In tolerant mode that is the URL with a space or a character that is not in ASCII percent-encoded (m%C3%BCnchen, not münchen), and with an internationalised host name in its Punycode form. If no rules are provided, all URLs found are included. Patterns longer than 1,000 characters are rejected and reported via GetErrors().

s := sitemap.New()
s.SetRules([]string{
	`product/`,
	`category/`,
})

... or ...

s := sitemap.New().SetRules([]string{
	`product/`,
	`category/`,
})
HTTP client

To use a custom HTTP client for all requests, use the SetHTTPClient() function. This is useful when you need a custom transport, proxy, TLS configuration, or authentication via a custom http.RoundTripper.

When a custom client is provided, SetFetchTimeout has no effect — the client's own Timeout field controls the request deadline. Pass nil to reset to the default client behaviour.

s := sitemap.New()
s = s.SetHTTPClient(&http.Client{
    Timeout: 30 * time.Second,
    Transport: &http.Transport{
        TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12},
    },
})

... or ...

s := sitemap.New().SetHTTPClient(&http.Client{Timeout: 30 * time.Second})

See examples/httpclient for a runnable example.

Strict mode

By default, the parser operates in tolerant mode: relative URLs found in <loc> elements are automatically resolved against the parent sitemap URL. This handles real-world sitemaps that may not fully comply with the specification.

To enable strict mode, use the SetStrict() function. In strict mode, all URL entries are validated per the sitemaps.org protocol:

  • <loc> must be an absolute HTTP or HTTPS URL
  • <loc> must use the same host and protocol as the sitemap file (for a sitemap reached through a redirect: as the URL it was served from, see Redirects); what counts as the same host is described below
  • <loc> must hold neither a space nor a control character (a tab or a line break for one); a URL gives them percent-encoded, a space as %20
  • <loc> must hold none of the characters " < > \ ^ ` { | } either, which have no place in a URL (RFC 3986); a URL gives them percent-encoded as well, a vertical bar as %7C. Square brackets and characters that are not in ASCII are accepted
  • <loc> must not exceed 2,048 characters
  • <changefreq> must be one of the values of the protocol, written as the protocol writes them: always, hourly, daily, weekly, monthly, yearly or never (if present)
  • <priority> must be between 0.0 and 1.0 inclusive (if present)
  • <lastmod> and <priority> must hold a value that can be parsed (if present)
  • The document must be well-formed XML, otherwise it is rejected as a whole and reported as a *ParseError

In tolerant mode (the default):

  • Relative <loc> URLs are resolved against the parent sitemap URL
  • A space in a <loc> URL is percent-encoded (%20) wherever it stands, so that the URL can be requested as it is: https://example.com/search?q=summer sale is returned as https://example.com/search?q=summer%20sale
  • A character that is not in ASCII is percent-encoded the same way, as the bytes it is in UTF-8, in the query as well as in the path and in the fragment: https://example.com/café?q=café is returned as https://example.com/caf%C3%A9?q=caf%C3%A9
  • An internationalised host name in a <loc> URL is returned in its Punycode form, the name a request goes to: https://bücher.example/page is returned as https://xn--bcher-kva.example/page, see Internationalised host names
  • <loc> URLs exceeding 2,048 characters after resolution are rejected; what is percent-encoded counts as the characters it is encoded to
  • The URLs of the extensions (of an image, a video or an alternate link) are percent-encoded like a <loc> URL where they are absolute HTTP or HTTPS URLs, and are returned as the document gives them where they are not, see GetURLs
  • <priority> values outside [0.0, 1.0] are accepted as-is
  • A <lastmod> or <priority> that cannot be parsed is left unset (nil) and reported, the entry itself is kept
  • A <changefreq> value of the protocol that is written in another letter case (Daily, WEEKLY) is read as the value itself (daily, weekly), so that it equals its constant; a value the protocol does not know is kept as the document gives it
  • XML mistakes that can be read past are accepted: an unescaped &, an unknown entity such as &nbsp;, a missing end tag

Entries that fail validation are skipped and reported via GetErrors().

Strict mode takes a URL to be on the host of the sitemap when it names the same host and the same port. The name of a host is not case-sensitive, and a URL that names no port is on the default port of its protocol (80 for HTTP, 443 for HTTPS). In a sitemap at https://example.com/sitemap.xml, https://Example.com/page and https://example.com:443/page are therefore accepted, while a URL on another host, on a subdomain (https://www.example.com/page) or on another port (https://example.com:8443/page) is rejected:

validate "https://example.com:8443/page": strict mode: host "example.com:8443" does not match sitemap host "example.com"

The same goes for the sitemaps a sitemap index lists. An internationalised host name is one host in whichever form it is written, see Internationalised host names.

Strict mode rejects a URL with a character in it that a URL may not hold wherever the character stands, in the path, in the query or in the fragment. The error tells what the URL has to give the character as:

validate "https://example.com/search?tags=summer|sale": strict mode: URL contains the character '|', which it has to give as "%7C"

Tolerant mode accepts such a URL. It returns the character percent-encoded in the path and in the fragment, and as it is in the query.

An entry without a location is one of them in both modes. A <url> or <sitemap> whose <loc> is missing, empty or holds only whitespace is skipped and reported as a *ValidationError. The entry has no URL of its own, so the error names the sitemap it stands in:

validate "https://example.com/sitemap.xml": <loc> of an entry is empty or missing

An RSS <item> without a <link> and an Atom <entry> without a link are skipped without an error, since a feed item is not required to have one.

A location has to name a host in both modes as well: a page is on one, and so is a sitemap. https:///page, https:page and https://:8080/page, which names a port but no host, are skipped and reported as a *ValidationError. A sitemap index entry or a Sitemap: line of a robots.txt with such a URL is not requested:

validate "https://:8080/page": missing host

In tolerant mode this goes for a relative URL too. One that begins with // gives the host itself and takes only the protocol from the sitemap: //cdn.example.com/page is https://cdn.example.com/page in a sitemap served over HTTPS. // and ///page therefore name no host and are rejected the same way. A relative URL that does not resolve to a URL at all is rejected with the reason net/url gives:

validate "//::": parse "https://::": invalid port "::" after host

The Loc of every URL that GetURLs() returns is thus an HTTP or HTTPS URL that names a host and that net/url can parse, in both modes.

The sitemaps a robots.txt names are checked before they are fetched as well. The value of a Sitemap: line has to be an HTTP or HTTPS URL of at most 2,048 characters. Tolerant mode resolves a relative one against the URL of the robots.txt and percent-encodes a space or a character that is not in ASCII in it, strict mode requires an absolute one that holds neither a space nor another character a URL may not hold. The sitemap may be on another host than the robots.txt in both modes, which the protocol allows. A value that is rejected is skipped and reported as a *ValidationError:

validate "ftp://example.com/sitemap.xml": unsupported scheme "ftp"

A value that cannot be parsed never costs more than its own entry. The numbers and dates of the extensions (<video:duration>, <video:rating>, <video:view_count>, <video:expiration_date>, <video:publication_date>, <news:publication_date>) are treated the same way in both modes: the field is left unset (nil) and the entry is kept. Every such value is reported via GetErrors() as a *ValidationError for the page it belongs to:

validate "https://example.com/page": invalid <lastmod> value "2024-01-15 10:30:00"

An empty date is no such value. A <lastmod>, <news:publication_date>, <video:expiration_date> or <video:publication_date> that is empty or holds only whitespace is read as if the element were not there: the field is nil, not the zero time, and nothing is reported in either mode. The exception is the publication date of a news entry, which strict mode requires, see GetURLs. An empty number (<priority>, <video:duration>, <video:rating>, <video:view_count>) reads as 0.

Whitespace around a value is no part of it, in both modes. The content of an element may be indented or stand on a line of its own, and an attribute may be padded: every text value of an entry is returned without the whitespace that surrounds it, those of the extensions included. A value that holds nothing but whitespace is therefore an empty one. A <changefreq> like that is read as if the element were not there, and a value an extension requires is missing then, see GetURLs.

See examples/tolerant for a runnable example of how the two modes treat a sitemap with mistakes in it, an entry without a location, a location without a host and URLs that are not percent-encoded among them, and examples/strict for one of what strict mode requires of a URL and of the images, the videos and the alternate links of an entry.

Internationalised host names

The name of an internationalised host can be written in two forms: in Unicode (bücher.example) and in Punycode (xn--bcher-kva.example), the ASCII form it is looked up by. A sitemap may use either.

Tolerant mode returns a URL with the Punycode form of its host, whichever form the document writes, so that the URLs of one host come back with one name and can be requested as they are:

<loc> returned by tolerant mode
https://bücher.example/page https://xn--bcher-kva.example/page
https://BÜCHER.example/page https://xn--bcher-kva.example/page
https://xn--bcher-kva.example/page https://xn--bcher-kva.example/page
/page in a sitemap at https://bücher.example/sitemap.xml https://xn--bcher-kva.example/page

The name is converted the way net/http converts the host of a URL it requests. This goes for every URL tolerant mode resolves: for the pages of a sitemap of any format, and for the sitemaps a sitemap index lists or a robots.txt names, which are requested by that name. A sitemap that is listed in both forms of its host is therefore fetched once. It goes for the absolute URLs of the extensions too, see GetURLs. A host name in ASCII is returned as it is written, capitals included. So is a name that is no valid domain name, such as bücher_x.example, which comes back the way net/url writes it: percent-encoded, as b%C3%BCcher_x.example.

Strict mode returns a URL the way the document gives it. It takes both forms of the name for the same host: in a sitemap at https://bücher.example/sitemap.xml, https://xn--bcher-kva.example/page is accepted, and so is https://bücher.example/page in a sitemap that was requested by the Punycode form of its host.

See examples/idn for a runnable example.

s := sitemap.New()
s = s.SetStrict(true)

... or ...

s := sitemap.New().SetStrict(true)
Chaining methods

In both cases, the functions return a pointer to the main object of the package, allowing you to chain these setting methods in a fluent interface style:

s := sitemap.New().SetUserAgent("YourUserAgent").SetFetchTimeout(10)
Read configuration

Each configuration setting can be read back via a corresponding Get* method. All getters are thread-safe.

Getter Return type Description
GetUserAgent() string Current user agent string
GetFetchTimeout() uint16 Fetch timeout in seconds
GetMultiThread() bool Whether multi-threaded fetching is enabled
GetMaxResponseSize() int64 Maximum HTTP response size in bytes
GetMaxDepth() int Maximum sitemap index recursion depth
GetMaxConcurrency() int Maximum number of sitemaps fetched or parsed at the same time (0 = unlimited)
GetMaxSitemaps() int Maximum number of sitemaps a call fetches (0 = unlimited)
GetMaxURLs() int Maximum number of URLs a call collects (0 = unlimited)
GetFollow() []string Copy of the follow regex pattern list
GetRules() []string Copy of the URL filter regex pattern list
GetHTTPClient() *http.Client Custom HTTP client, or nil if using the default
GetStrict() bool Whether strict validation mode is enabled

GetFollow() and GetRules() return copies — mutating the returned slice does not affect the parser's internal state.

s := sitemap.New().SetMaxConcurrency(8).SetStrict(true)
fmt.Println(s.GetMaxConcurrency()) // 8
fmt.Println(s.GetStrict())         // true
Thread safety

All public methods on *S are safe to call from multiple goroutines. Internal state (configuration, collected URLs, errors) is protected by a mutex.

The mutex is not held while a sitemap is fetched, unzipped or decoded, only while what the sitemap yielded is added to the results. The getters can therefore be called while Parse() is running without waiting for it: GetURLs(), GetURLCount() and GetErrors() return what has been collected so far. The URLs of a sitemap appear all at once, when that sitemap has been parsed.

However, two important constraints apply:

  • Concurrent Parse() / ParseContext() calls on the same instance are serialised. A second call blocks until the first completes. If you need to parse multiple sitemaps concurrently, create a separate *S instance per goroutine with New().
  • Configure before parsing. Calling a Set* method while Parse() is running on the same instance is safe (the write is mutex-protected), but the outcome is non-deterministic — the new value may or may not be picked up mid-parse. Set all options before calling Parse().

Deadlock note: a goroutine gives its SetMaxConcurrency slot back as soon as its sitemap is fetched and parsed, before the sitemaps that one lists are followed. This prevents a goroutine from holding a slot while it waits for the slots of the child sitemaps, which would otherwise deadlock when a robots.txt or a sitemap index leads to further sitemap indexes.

Parse

Once you have properly initialized and configured your instance, you can parse sitemaps using the Parse() function.

The Parse() function takes in two parameters:

  • url: the URL of the sitemap to be parsed,
    • url can be a robots.txt or sitemapindex or sitemap (urlset)
    • it is taken for that of a robots.txt when its path ends in robots.txt, with or without a query after it and in any letter case, see Formats supported
  • urlContent: an optional string pointer for the content of the URL.

If you wish to provide the content yourself, pass the content as the second parameter. If not, simply pass nil and the function will fetch the content on its own. The Parse() function performs concurrent parsing and fetching optimized by the use of Go's goroutines and sync package, ensuring efficient sitemap handling.

s, err := s.Parse("https://www.sitemaps.org/sitemap.xml", nil)

In this example, sitemap is parsed from "https://www.sitemaps.org/sitemap.xml". The function fetches the content itself, as we passed nil as the urlContent.

The error Parse() returns is about the document at url itself. It is nil if that document was fetched and parsed, otherwise it tells why it was not:

Error returned Meaning
*ValidationError url is not an HTTP or HTTPS URL with a host (checked when the content is to be fetched)
*NetworkError The document could not be fetched
*ParseError The document could not be parsed: it is not a sitemap, it is empty, its XML or gzip content is broken, it expands beyond the size limit
an untyped error A configuration error is outstanding, so nothing was parsed, see Reusing an instance

Except for the last, the error returned is the very one GetErrors() holds about the document.

What goes wrong further on does not fail the call and is reported via GetErrors() only: a sitemap the document lists that cannot be fetched or parsed, an entry that is not valid, a limit being reached (see Max depth, Max sitemaps and Max URLs). A nil error therefore does not mean that nothing was skipped, and GetErrors() is worth checking after every call:

s, err := sitemap.New().Parse("https://www.sitemaps.org/sitemap.xml", nil)
if err != nil {
    // The document at the URL could not be fetched or parsed.
    log.Fatalf("parse error: %v", err)
}
for _, e := range s.GetErrors() {
    // A sitemap it lists, or an entry, was skipped, or a limit was reached.
    log.Printf("warning: %v", e)
}
Parse with context

For new code, prefer ParseContext() so that callers can propagate cancellation and deadlines to every HTTP request issued by the parser (the initial fetch as well as the recursive sitemap-index/urlset fetches). The legacy Parse() is a backward-compatible wrapper around ParseContext() that uses context.Background().

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

s, err := sitemap.New().ParseContext(ctx, "https://www.sitemaps.org/sitemap.xml", nil)

Cancelling ctx aborts in-flight downloads and prevents new ones from starting. Already-parsed URLs accumulated before cancellation remain available via GetURLs().

A call without a deadline ends when the sitemaps are worked through, or when a limit of the call is reached (see Max sitemaps and Max URLs). The limits bound what a call fetches and collects, not how long it takes: a deadline is what bounds the time.

A call that was cut short returns a *ParseError that names the URL passed to ParseContext() and wraps the error of the context, so errors.Is tells why the call ended:

if errors.Is(err, context.DeadlineExceeded) {
    // the deadline passed
}
if errors.Is(err, context.Canceled) {
    // ctx was cancelled
}

The same error is recorded in GetErrors(), once for the call: in multi-threaded and in sequential mode alike, and however many sitemaps were not fetched. Nothing is recorded for the sitemaps the call did not get to. A request that was under way is reported in addition, as the *NetworkError of its sitemap, which wraps the error of the context as well.

When it is the request for the URL passed to ParseContext() that is cut short, the call returns and records that *NetworkError alone.

See examples/context for a runnable example.

Redirects

HTTP redirects are followed. A sitemap that is reached through a redirect is treated as located at the URL it was finally served from, not at the URL that was requested:

  • relative URLs in it are resolved against that URL (tolerant mode),
  • the URLs it lists have to use the host and protocol of that URL (strict mode),
  • the errors about the document name that URL.

So when http://example.com/sitemap.xml redirects to https://www.example.com/sitemap.xml, a <loc>/page</loc> in it yields https://www.example.com/page. Strict mode accepts the URLs on https://www.example.com and rejects those on http://example.com, the sitemap file not being there.

This goes for the URL passed to Parse() and for every sitemap fetched on the way alike: the sitemaps of a sitemap index and those a robots.txt names. When the content is passed in through urlContent, nothing is fetched and the document is located at the URL given.

Which redirects are followed is up to the HTTP client. The default one stops after 10 consecutive requests, as Go's http.Client does by default; a client set with SetHTTPClient() decides in its CheckRedirect. A fetch that fails is reported as a *NetworkError naming the URL that was requested.

See examples/redirect for a runnable example.

Reusing an instance

An instance can be used for any number of Parse() / ParseContext() calls. Every call starts from a clean state: the URLs and errors collected by the previous call are discarded first, so GetURLs() and GetErrors() always describe the most recent call only. This also holds when a call returns early, for example because the input URL is invalid.

s := sitemap.New()

for _, url := range []string{"https://example.com/sitemap.xml", "https://example.org/sitemap.xml"} {
    if _, err := s.Parse(url, nil); err != nil {
        log.Printf("%s: %v", url, err)
        continue
    }
    fmt.Printf("%s: %d URLs, %d errors\n", url, s.GetURLCount(), s.GetErrorsCount())
}

Configuration errors are the exception, because they belong to the instance rather than to a single call. A *ConfigError recorded by a Set* method stays in GetErrors(), and while any of them is outstanding Parse() does not parse anything and returns the error errors occurred before parsing, see GetErrors() for details. Calling the same setter again with a valid value clears its error:

s := sitemap.New().SetMaxDepth(0) // invalid: recorded as a *ConfigError

_, err := s.Parse(url, nil) // err: errors occurred before parsing, see GetErrors() for details

s.SetMaxDepth(5)           // valid: clears the error recorded for maxDepth
_, err = s.Parse(url, nil) // parses normally

Each setter call replaces the errors recorded by the previous call for the same setting, so repeating an invalid value does not accumulate errors.

See examples/reuse for a runnable example.

Character encoding

The sitemaps.org protocol requires sitemaps to be UTF-8 encoded, but XML documents in other encodings are accepted as well. The parser honours the encoding named in the XML declaration and transcodes the document while reading it, so every string it returns is UTF-8:

// "\xe9" is the letter "é" in ISO-8859-1
content := "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
    "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n" +
    "  <url><loc>https://example.com/caf\xe9</loc></url>\n" +
    "</urlset>"

s, err := sitemap.New().Parse("https://example.com/sitemap.xml", &content)
// s.GetURLs()[0].Loc == "https://example.com/caf%C3%A9"

This applies to every XML format (sitemap index, urlset, RSS and Atom), in tolerant and in strict mode alike. The encodings of the WHATWG Encoding Standard are supported, among them the ISO-8859 and windows-125x families, US-ASCII, KOI8-R, Shift_JIS, EUC-JP, EUC-KR, GBK, gb18030 and Big5.

A UTF-8 byte order mark (BOM, the bytes EF BB BF) at the beginning of a document is ignored in every format, robots.txt files and plain text sitemaps included, in both modes: it tells the encoding and is no part of the first line. This goes for a gzip compressed document as well. Only the very beginning of a document counts. Anywhere else the character (U+FEFF) is taken as it stands, so a line of a text sitemap or a robots.txt that begins with it is not recognised.

Limitations:

  • A document that declares any other encoding is not parsed; a *ParseError naming the encoding is reported via GetErrors(), and returned by Parse() when it is the document Parse() was called for.
  • Only the XML declaration is consulted. The charset parameter of the HTTP Content-Type header is ignored, and a document without a declared encoding is read as UTF-8.
  • UTF-16 and UTF-32 documents are not supported.
  • Plain text sitemaps and robots.txt files carry no encoding declaration and are not transcoded.

See examples/encoding for a runnable example.

Results

After parsing, you can retrieve the results using the following methods:

GetURLs

Returns all parsed URLs as a []URL slice.

urls := s.GetURLs()

Each URL struct contains the following fields:

  • Loc (string) — the URL location; never empty, an entry without a <loc> is skipped
  • LastMod (*LastModTime) — last modification time (embeds time.Time), may be nil; also nil when the element is empty or the value cannot be parsed, see Strict mode
  • ChangeFreq (*URLChangeFreq) — change frequency hint, may be nil; also nil when the element is empty. Use the exported constants for comparison: ChangeFreqAlways, ChangeFreqHourly, ChangeFreqDaily, ChangeFreqWeekly, ChangeFreqMonthly, ChangeFreqYearly, ChangeFreqNever. Tolerant mode reads one of them in whatever letter case it is written and keeps a value that is none of them as the document gives it; strict mode skips an entry with either, so a field that is set equals one of the constants there, see Strict mode
  • Priority (*float32) — crawl priority between 0.0 and 1.0, may be nil; also nil when the value cannot be parsed
  • Images ([]Image) — images associated with this URL via the Google Image Sitemap extension, may be nil
  • News (*News) — news metadata associated with this URL via the Google News Sitemap extension, may be nil
  • Videos ([]Video) — videos associated with this URL via the Google Video Sitemap extension, may be nil
  • Hreflangs ([]AlternateLink) — alternate language/region versions of this URL via the XHTML extension, may be nil

No text value of a URL has whitespace around it, see Strict mode. This goes for the fields of the extensions below as well: an Image.Loc or a Video.Title that stands on a line of its own in the document is returned without the line breaks and the indentation.

An image, a video and an alternate link are each known by a URL: Image.Loc, Video.ThumbnailLoc and AlternateLink.Href. It may be on any host. Strict mode requires an absolute HTTP or HTTPS URL that names a host and holds no space, no control character and none of the characters " < > \ ^ ` { | }; an image, a video or an alternate link whose URL is not one is left out and reported, the page itself is kept:

validate "https://cdn.example.com/summer sale.jpg": strict mode: URL contains a space

The other URLs of an image and of a video (Image.License, Video.ContentLoc, Video.PlayerLoc and Video.Uploader.Info) need not be given. Strict mode requires the same of one that is given, and that it is of at most 2,048 characters. One that is not such a URL is reported, and nothing is left out over it: the image or the video is returned, with the URL as the document gives it:

validate "ftp://cdn.example.com/video.mp4": strict mode: unsupported scheme "ftp"

An error names the URL it is about, as these do. An error that is not about a URL names the page the entry is on: that of a video which lacks a field or has one out of range, and that of an image, a video or an alternate link which has no URL at all:

validate "https://example.com/page": strict mode: video <title> is empty
validate "https://example.com/page": strict mode: image <loc> is empty

Tolerant mode asks no more of the URL than that it is there and of at most 2,048 characters. An absolute HTTP or HTTPS URL comes back the way the Loc of the page does, so that it can be requested as it is: with a space or a character that is not in ASCII percent-encoded, and with an internationalised host name in its Punycode form. This goes for every URL of an extension: Image.Loc and Image.License, Video.ThumbnailLoc, Video.ContentLoc, Video.PlayerLoc and Video.Uploader.Info, and AlternateLink.Href.

in the document returned by tolerant mode
https://cdn.example.com/summer sale.jpg https://cdn.example.com/summer%20sale.jpg
https://cdn.example.com/café.jpg?alt=café https://cdn.example.com/caf%C3%A9.jpg?alt=caf%C3%A9
https://bücher.example/cover.jpg https://xn--bcher-kva.example/cover.jpg
https://cdn.example.com/summer%20sale.jpg https://cdn.example.com/summer%20sale.jpg
/images/summer sale.jpg /images/summer sale.jpg

A URL with nothing to encode in it is returned as the document gives it. So is a URL that is no absolute HTTP or HTTPS URL which names a host: a relative one, which is not resolved, one of another protocol, and one that cannot be parsed. So is, finally, a URL that would exceed 2,048 characters percent-encoded. Tolerant mode thus leaves out no image, video or alternate link over the form of its URL. Strict mode returns every URL it accepts as the document gives it.

See examples/tolerant for a runnable example.

Each Image struct contains the following fields (all string):

  • Loc — image URL (required by the spec; images with an empty Loc are silently dropped in tolerant mode, or produce an error in strict mode)
  • Title — image title (optional)
  • Caption — image caption (optional)
  • GeoLocation — geographic location of the image subject (optional)
  • License — URL of the image licence (optional; validated in strict mode if present, see above)

See examples/image for a runnable example.

Each News struct contains:

  • Publication (NewsPublication) — publication metadata:
    • Name (string) — publication name (required in strict mode)
    • Language (string) — BCP 47 language code, e.g. "en" (required in strict mode)
  • PublicationDate (*LastModTime) — article publication date; embeds time.Time, nil if absent or empty (required in strict mode)
  • Title (string) — article title (required in strict mode)

In strict mode, all four required fields (Title, Publication.Name, Publication.Language, PublicationDate) must be present; missing fields are each reported via GetErrors() and the News entry is still included with whatever data was parsed. A <news:publication_date> that is there but empty is reported like a missing one (strict mode: news <publication_date> is empty). In tolerant mode no validation is performed. A PublicationDate that cannot be parsed is left nil and reported in both modes.

See examples/news for a runnable example.

Each AlternateLink struct contains:

  • Rel (string) — relationship, should be "alternate" (strict mode accepts nothing else)
  • Hreflang (string) — language/region code (e.g. "en", "de-ch") (required in strict mode)
  • Href (string) — the URL of the alternate version (required; links with an empty Href are silently dropped in tolerant mode, or produce an error in strict mode)

In strict mode a link whose Rel or Hreflang is not what it has to be is left out and reported, like one whose Href is no URL; the error names the Href of the link.

See examples/hreflang for a runnable example.

Each Video struct contains:

  • ThumbnailLoc (string) — thumbnail image URL (required; videos with an empty ThumbnailLoc are silently dropped in tolerant mode, or produce an error in strict mode)
  • Title (string) — video title (required in strict mode)
  • Description (string) — video description (required in strict mode)
  • ContentLoc (string) — direct URL to the video file (at least one of ContentLoc or PlayerLoc required in strict mode); validated in strict mode if present
  • PlayerLoc (string) — URL of an embedded video player; validated in strict mode if present
  • Duration (*int) — duration in seconds (1–28800); validated in strict mode if present
  • ExpirationDate (*LastModTime) — date after which the video should not be shown; embeds time.Time, nil if absent or empty
  • Rating (*float32) — rating between 0.0 and 5.0; validated in strict mode if present
  • ViewCount (*int) — number of views
  • PublicationDate (*LastModTime) — publication date; embeds time.Time, nil if absent or empty
  • FamilyFriendly (string) — "yes" or "no"
  • Restriction (*VideoRestriction) — country restriction with Relationship ("allow"/"deny") and Value (space-separated country codes)
  • Platform (*VideoPlatform) — platform restriction with Relationship and Value (e.g. "web mobile tv")
  • RequiresSubscription (string) — "yes" or "no"
  • Uploader (*VideoUploader) — uploader name (Value) and optional profile URL (Info); the URL is validated in strict mode if present
  • Live (string) — "yes" or "no"
  • Tags ([]string) — content tags; maximum 32 validated in strict mode

A Duration, Rating, ViewCount, ExpirationDate or PublicationDate that cannot be parsed is left nil and reported via GetErrors() in both modes; the video itself is kept.

In strict mode a video that has a ThumbnailLoc which is a URL is kept whatever else is wrong with it. Every field that is missing, every value that is out of range and every other URL that is no URL is reported via GetErrors(), the first two for the page the video is on, the last for the URL itself.

See examples/video for a runnable example.

GetURLCount

Returns the number of parsed URLs.

count := s.GetURLCount()
GetRandomURLs

Returns a slice of n randomly selected URLs without duplicates. If n exceeds the number of parsed URLs, all of them are returned, in random order. If n is zero or negative, the result is an empty slice.

randomURLs := s.GetRandomURLs(5)
GetErrors

Returns the errors encountered during the most recent Parse() / ParseContext() call, together with any outstanding configuration errors recorded by the Set* methods (see Reusing an instance).

errs := s.GetErrors()

Errors are typed and can be inspected with errors.As:

Type When returned Useful fields
*ConfigError A Set* method received an invalid value Field (setting name), Err (root cause)
*NetworkError An HTTP fetch failed URL (requested URL), Err (root cause)
*ParseError A sitemap document could not be parsed (not a sitemap, empty, broken XML or gzip content, larger than the size limit) URL (sitemap URL; after a redirect, the URL the sitemap was served from), Err (root cause)
*ParseError The depth limit was reached, see Max depth URL (the sitemap index whose sitemaps were not followed), Err (root cause)
*ParseError The limit on the sitemaps or on the URLs of the call was reached, see Max sitemaps and Max URLs URL (the URL passed to Parse()), Err (root cause)
*ParseError The call was cut short by its context, see Parse with context URL (the URL passed to ParseContext()), Err (the error of the context)
*ValidationError A URL or field value failed validation URL (the rejected URL, or the page or sitemap the rejected value belongs to), Err (root cause)

All types implement Unwrap(), enabling errors.Is traversal to the root cause.

The error that Parse() / ParseContext() return is one of these as well, see Parse.

The slice returned is the one the instance keeps, so it must not be modified. It does not change afterwards: errors recorded later are in what a later GetErrors() call returns.

for _, err := range s.GetErrors() {
    var netErr *sitemap.NetworkError
    if errors.As(err, &netErr) {
        fmt.Printf("fetch failed for %s: %v\n", netErr.URL, netErr.Err)
        continue
    }
    var valErr *sitemap.ValidationError
    if errors.As(err, &valErr) {
        fmt.Printf("validation error for %s: %v\n", valErr.URL, valErr.Err)
        continue
    }
}

See examples/errors for a runnable example.

GetErrorsCount

Returns the number of errors GetErrors() returns.

errCount := s.GetErrorsCount()

Examples

Examples can be found in /examples.

Documentation

Overview

Package sitemap fetches and parses sitemaps and collects the URLs they list.

A call to S.Parse or S.ParseContext takes the URL of a robots.txt, of a sitemap index or of a sitemap. It follows the sitemaps a robots.txt names and the ones a sitemap index lists, and collects the pages of every sitemap it reaches:

s := sitemap.New()
if _, err := s.Parse("https://www.sitemaps.org/sitemap.xml", nil); err != nil {
	log.Fatal(err)
}
for _, u := range s.GetURLs() {
	fmt.Println(u.Loc)
}

The document at the URL can be handed to the call in place of nil. It is not fetched then; the sitemaps it lists are.

Formats

A sitemap is a <urlset> of the sitemaps.org protocol, an RSS 2.0 feed, an Atom 1.0 feed, or a plain text file with a URL on a line. A sitemap and a sitemap index may be gzip compressed. An entry of a <urlset> is returned with its <lastmod>, <changefreq> and <priority>, and with what the image, news, video and hreflang extensions say about the page, see URL. Nothing but the location of a page is taken from a feed or a text file.

Configuration

An instance is configured with its Set methods, which can be chained:

s := sitemap.New().
	SetUserAgent("my-crawler/1.0").
	SetFetchTimeout(10).
	SetFollow([]string{`^https://example\.com/sitemaps/`})

A Set method that is called with a value it does not accept records a ConfigError. Nothing is parsed while one is outstanding; calling the method again with a valid value clears it.

The parser is in tolerant mode unless S.SetStrict says otherwise. Tolerant mode makes the best of a document that does not keep to the protocol: a relative URL, for one, is resolved against the URL of the sitemap. Strict mode holds a document to the protocol, and skips and reports the entries that do not keep to it.

What a call may take up is bounded: the size of a response by S.SetMaxResponseSize, the depth of nested sitemap indexes by S.SetMaxDepth, the number of sitemaps it fetches by S.SetMaxSitemaps, the number of URLs it collects by S.SetMaxURLs, and the sitemaps it works on at a time by S.SetMaxConcurrency. S.SetFollow restricts the sitemaps that are fetched, S.SetRules the URLs that are collected.

Errors

Parse and ParseContext return an error when the call fails for the URL it was made for: the URL is not valid, or the document at it cannot be fetched or parsed. What goes wrong further on does not fail the call and is reported by S.GetErrors only: a sitemap that cannot be fetched or parsed, an entry that is not valid, a limit that is reached. A nil error therefore does not mean that nothing was skipped.

The errors are of the types ConfigError, NetworkError, ParseError and ValidationError, to be told apart with errors.As.

Concurrency

The methods of an instance may be called from several goroutines. An instance carries out one Parse or ParseContext call at a time, a second one waits for the first to end; use an instance for each of the calls that are to run side by side. An instance can be used for any number of calls one after the other: its results are those of the most recent one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AlternateLink struct {
	Rel      string `xml:"rel,attr"`
	Hreflang string `xml:"hreflang,attr"`
	Href     string `xml:"href,attr"`
}

AlternateLink represents an alternate version of a page (hreflang) per the XHTML standard used in sitemaps. Reference: https://developers.google.com/search/docs/specialty/international/localized-versions#sitemap

type ConfigError added in v0.9.0

type ConfigError struct {
	// Field is the configuration field name (e.g. "maxDepth", "follow", "rules").
	Field string
	// Err is the underlying validation error.
	Err error
}

ConfigError is returned when a configuration setter receives an invalid value. Callers can inspect the Field to determine which configuration setting caused the error.

Example usage:

var cfgErr *sitemap.ConfigError
if errors.As(err, &cfgErr) {
    fmt.Println("bad config field:", cfgErr.Field)
}

func (*ConfigError) Error added in v0.9.0

func (e *ConfigError) Error() string

Error returns the message of the error, which names the configuration field and tells what is wrong with the value it was given: `config "maxDepth": must be greater than 0, got 0`.

func (*ConfigError) Unwrap added in v0.9.0

func (e *ConfigError) Unwrap() error

Unwrap returns the underlying error, enabling errors.Is / errors.As traversal.

type Image added in v0.7.0

type Image struct {
	Loc         string `xml:"http://www.google.com/schemas/sitemap-image/1.1 loc"`
	Title       string `xml:"http://www.google.com/schemas/sitemap-image/1.1 title"`
	Caption     string `xml:"http://www.google.com/schemas/sitemap-image/1.1 caption"`
	GeoLocation string `xml:"http://www.google.com/schemas/sitemap-image/1.1 geo_location"`
	License     string `xml:"http://www.google.com/schemas/sitemap-image/1.1 license"`
}

Image is a structure of <image:image> in <url>, per the Google Image Sitemap extension. Reference: https://developers.google.com/search/docs/crawling-indexing/sitemaps/image-sitemaps

type LastModTime added in v1.0.0

type LastModTime struct {
	time.Time
}

LastModTime is the date, or the date and time, that a <lastmod> element or a date element of an extension holds. It embeds time.Time.

In the URLs that Parse and ParseContext collect, a field of this type is nil when the element is absent, when it is empty, and when what it holds cannot be parsed. A field that is set therefore holds the date the document gives.

func (*LastModTime) UnmarshalXML added in v1.0.0

func (l *LastModTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements xml.Unmarshaler, for code that decodes sitemap XML with encoding/xml itself. It accepts a date or a date and time in one of the W3C formats the sitemaps.org protocol allows, and fails for anything else.

An element that is empty, or holds only whitespace, is not an error and leaves l as it is. Note that encoding/xml allocates a pointer field for every element it meets, so a *LastModTime field decoded this way is not nil for an empty element: it holds the zero time, which IsZero tells. Parse and ParseContext do not read dates this way; in the URLs they collect, the field of an empty element is nil.

type NetworkError added in v0.9.0

type NetworkError struct {
	// URL is the URL that was being fetched when the error occurred.
	URL string
	// Err is the underlying network or HTTP error.
	Err error
}

NetworkError is returned when an HTTP fetch fails. Callers can inspect URL to determine which resource could not be retrieved.

Example usage:

var netErr *sitemap.NetworkError
if errors.As(err, &netErr) {
    fmt.Println("failed to fetch:", netErr.URL)
}

func (*NetworkError) Error added in v0.9.0

func (e *NetworkError) Error() string

Error returns the message of the error, which names the URL and tells why it could not be fetched: `fetch "https://example.com/sitemap.xml": received HTTP status 404`.

func (*NetworkError) Unwrap added in v0.9.0

func (e *NetworkError) Unwrap() error

Unwrap returns the underlying error, enabling errors.Is / errors.As traversal.

type News added in v0.7.0

type News struct {
	Publication     NewsPublication `xml:"http://www.google.com/schemas/sitemap-news/0.9 publication"`
	PublicationDate *LastModTime    `xml:"http://www.google.com/schemas/sitemap-news/0.9 publication_date"`
	Title           string          `xml:"http://www.google.com/schemas/sitemap-news/0.9 title"`
}

News is a structure of <news:news> in <url>, per the Google News Sitemap extension. Reference: https://developers.google.com/search/docs/crawling-indexing/sitemaps/news-sitemap

type NewsPublication added in v0.7.0

type NewsPublication struct {
	Name     string `xml:"http://www.google.com/schemas/sitemap-news/0.9 name"`
	Language string `xml:"http://www.google.com/schemas/sitemap-news/0.9 language"`
}

NewsPublication is a structure of <news:publication> in <news:news>.

type ParseError added in v0.9.0

type ParseError struct {
	// URL is the sitemap URL that was being parsed when the error occurred. For a sitemap
	// reached through a redirect it is the URL the sitemap was served from.
	// When the depth limit is reached, it is the sitemap index whose sitemaps were not
	// followed. When the limit on the sitemaps or on the URLs of a call is reached, or the
	// call was cut short, it is the URL the call was made for.
	URL string
	// Err is the underlying parse error.
	Err error
}

ParseError is returned when XML or gzip parsing of a sitemap document fails. Callers can inspect URL to determine which sitemap could not be parsed.

It is also the error of a parse that was not carried through for another reason: the sitemaps a sitemap index lists were not followed because the depth limit is reached, sitemaps or URLs were left out because the limit on the sitemaps or on the URLs of a call is reached, or the call was cut short because its context is done. In the latter case Err is the error of the context, so errors.Is matches context.Canceled and context.DeadlineExceeded.

Example usage:

var parseErr *sitemap.ParseError
if errors.As(err, &parseErr) {
    fmt.Println("failed to parse sitemap:", parseErr.URL)
}

func (*ParseError) Error added in v0.9.0

func (e *ParseError) Error() string

Error returns the message of the error, which names the URL and tells why the parse failed or was not carried through: `parse "https://example.com/sitemap.xml": sitemap content is empty`.

func (*ParseError) Unwrap added in v0.9.0

func (e *ParseError) Unwrap() error

Unwrap returns the underlying error, enabling errors.Is / errors.As traversal.

type S

type S struct {
	// contains filtered or unexported fields
}

S is a structure that holds various data related to processing URLs. It contains a cfg field of type `config`, which stores configuration settings. The mainURL field of type string represents the main URL being processed. The robotsTxtSitemapURLs field is a slice of strings that contains the URLs present in the robots.txt file's sitemap directive. The sitemapLocations field is a slice of strings that represents the locations of the sitemap files. The urls field is a slice of URL structs that stores the URLs to be processed. The errs field is a slice of errors that holds any encountered errors during processing.

func New

func New() *S

New creates a new instance of the S structure. It initializes the structure with default configuration values and returns a pointer to the created instance.

func (*S) GetErrors

func (s *S) GetErrors() []error

GetErrors returns the errors of the most recent Parse or ParseContext call, together with the configuration errors that are outstanding: those recorded by a Set method that was called with a value it does not accept, and not again with a valid one since. The errors of a call are discarded when the next call starts; configuration errors are kept until the setting is corrected.

Each error is a *ConfigError, a *NetworkError, a *ParseError or a *ValidationError, to be told apart with errors.As. They are in the order they were recorded in. With multi-threading on, see SetMultiThread, that order is not the same from one call to the next for the errors of different sitemaps.

The error Parse or ParseContext returned is among them, unless it tells that configuration errors are outstanding: then the configuration errors are. A call that returns a nil error may have recorded errors as well, about sitemaps and entries it had to skip and about limits it reached.

While a call is running, GetErrors returns the errors recorded so far; those recorded afterwards are not added to the slice it returned. The slice that is returned must not be modified. It is nil for a nil *S.

func (*S) GetErrorsCount

func (s *S) GetErrorsCount() int64

GetErrorsCount returns the number of errors GetErrors returns. It returns 0 for a nil *S.

func (*S) GetFetchTimeout added in v1.0.0

func (s *S) GetFetchTimeout() uint16

GetFetchTimeout returns the current fetch timeout in seconds.

func (*S) GetFollow added in v1.0.0

func (s *S) GetFollow() []string

GetFollow returns a copy of the current follow regex pattern strings.

func (*S) GetHTTPClient added in v1.0.0

func (s *S) GetHTTPClient() *http.Client

GetHTTPClient returns the custom HTTP client, or nil if the default client behaviour is used.

func (*S) GetMaxConcurrency added in v1.0.0

func (s *S) GetMaxConcurrency() int

GetMaxConcurrency returns the maximum number of sitemaps fetched or parsed at the same time. A value of 0 means unlimited concurrency.

func (*S) GetMaxDepth added in v1.0.0

func (s *S) GetMaxDepth() int

GetMaxDepth returns the maximum recursion depth for following sitemap indexes.

func (*S) GetMaxResponseSize added in v1.0.0

func (s *S) GetMaxResponseSize() int64

GetMaxResponseSize returns the maximum allowed HTTP response size in bytes.

func (*S) GetMaxSitemaps added in v1.2.0

func (s *S) GetMaxSitemaps() int

GetMaxSitemaps returns the maximum number of sitemaps a Parse or ParseContext call fetches. A value of 0 means no limit.

func (*S) GetMaxURLs added in v1.2.0

func (s *S) GetMaxURLs() int

GetMaxURLs returns the maximum number of URLs a Parse or ParseContext call collects. A value of 0 means no limit.

func (*S) GetMultiThread added in v1.0.0

func (s *S) GetMultiThread() bool

GetMultiThread returns whether multi-threaded fetching and parsing is enabled.

func (*S) GetRandomURLs

func (s *S) GetRandomURLs(n int) []URL

GetRandomURLs returns a slice of randomly selected URLs from the S object's URL list. The number of URLs to select is specified by the parameter n. If n exceeds the number of URLs in the list, all of them are returned, in random order. If n is zero or negative, or the S object is nil, an empty slice is returned. The function creates a copy of the original URLs list and randomly selects n URLs from it, removing them to avoid duplicates. The selected URLs are returned as a new slice.

func (*S) GetRules added in v1.0.0

func (s *S) GetRules() []string

GetRules returns a copy of the current URL filter regex pattern strings.

func (*S) GetStrict added in v1.0.0

func (s *S) GetStrict() bool

GetStrict returns whether strict URL validation mode is enabled.

func (*S) GetURLCount

func (s *S) GetURLCount() int64

GetURLCount returns the count of URLs in the S struct.

func (*S) GetURLs

func (s *S) GetURLs() []URL

GetURLs returns the list of parsed URLs.

func (*S) GetUserAgent added in v1.0.0

func (s *S) GetUserAgent() string

GetUserAgent returns the current user agent string used for HTTP requests.

func (*S) Parse

func (s *S) Parse(url string, urlContent *string) (*S, error)

Parse is a method of the S structure. It parses the given URL and its content.

Parse is a backward-compatible wrapper around ParseContext that uses context.Background(). For new code, prefer ParseContext so that callers can propagate cancellation and deadlines.

An instance can be reused: every call first discards the URLs and errors collected by the previous call. Configuration errors recorded by the Set* methods are the exception. They persist across calls, and while any of them is outstanding Parse does not parse anything and returns an error with the message "errors occurred before parsing, see GetErrors() for details". Calling the same setter again with a valid value clears its error.

A sitemap that is reached through a redirect is located at the URL it was served from, not at the URL that was requested: relative URLs in it are resolved against that URL, strict mode compares the URLs it lists with that URL, and the errors about the document name it.

It sets the mainURL field to the given URL. The content is the given URL content or, if that is nil, what the URL serves. If the URL is that of a robots.txt, it parses the robots.txt file and fetches URLs from the sitemap files mentioned in the robots.txt. A URL is that of a robots.txt when the last segment of its path is "robots.txt", in whichever letter case it is written and whether or not a query or a fragment follows it: "https://example.com/robots.txt?v=2" is one. If the URL is not that of a robots.txt, the content is checked and unzipped if necessary, then parsed and fetched. The content of a document is not kept once the document is parsed.

A call fetches no more sitemaps than SetMaxSitemaps allows, and collects no more URLs than SetMaxURLs allows, so that it comes to an end whatever the documents list.

It returns the S structure, and a nil error if the document at the given URL was fetched and parsed. Otherwise the error tells why it was not:

  • a configuration error is outstanding, see above;
  • the URL is not valid: a *ValidationError;
  • the document could not be fetched: a *NetworkError;
  • the document could not be parsed: a *ParseError.

Except for the first, the error returned is the one GetErrors() holds about it. What goes wrong further on is in GetErrors() only and does not fail the call: a sitemap the document lists that cannot be fetched or parsed, a limit that is reached, an entry that is not valid.

func (*S) ParseContext added in v0.4.0

func (s *S) ParseContext(ctx context.Context, url string, urlContent *string) (*S, error)

ParseContext parses the given URL and its content, honoring the supplied context for cancellation and deadlines.

The context is propagated through every HTTP request issued by the parser (both the initial fetch and the recursive sitemap-index/urlset fetches), so cancelling ctx aborts in-flight downloads and prevents new ones from starting. Already-parsed URLs accumulated in s.urls before cancellation remain available via GetURLs().

If ctx is done by the time the call ends, ParseContext returns a *ParseError that names the given URL and wraps the error of ctx, so errors.Is matches context.Canceled and context.DeadlineExceeded. The same error is recorded in the error list, once for the call: whether the sitemaps are fetched concurrently or one at a time, and however many of them were not fetched. A request that was cut short is recorded as the *NetworkError of that sitemap in addition.

A call that fails for the given URL itself, as described at Parse, returns and records that error alone. This is the case when it is the request for the given URL that is cut short: the error is the *NetworkError of that request, which wraps the error of ctx as well.

All other semantics match Parse.

func (*S) SetFetchTimeout

func (s *S) SetFetchTimeout(fetchTimeout uint16) *S

SetFetchTimeout sets the fetch timeout for the Sitemap Parser. The fetch timeout determines how long the parser will wait for an HTTP request to complete. It should be specified in seconds as a uint16 value and must be greater than 0. Invalid values are ignored and a *ConfigError is recorded; a later call with a valid value clears it. Note: when a custom HTTP client is set via SetHTTPClient, this value has no effect. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetFollow added in v0.1.3

func (s *S) SetFollow(regexes []string) *S

SetFollow sets the follow patterns using the provided list of regex strings and compiles them into regex objects. When patterns are set, only the sitemaps whose URL matches one of them are fetched, whether a sitemap index lists them or a robots.txt names them. The URL passed to Parse is always fetched. Patterns longer than maxRegexPatternLength characters are rejected with a *ConfigError. Any errors encountered during compilation are recorded as *ConfigError values. Each call replaces both the patterns and the errors recorded by the previous call. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetHTTPClient added in v0.6.0

func (s *S) SetHTTPClient(client *http.Client) *S

SetHTTPClient sets a custom HTTP client for the Sitemap Parser. When set, the provided client is used for all HTTP requests instead of the internally created default client. This allows callers to configure custom transports, proxies, TLS settings, authentication, or timeout strategies. When a custom client is provided, SetFetchTimeout has no effect; the client's own Timeout field controls the request deadline. Pass nil to reset to the default client behaviour. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetMaxConcurrency added in v0.4.0

func (s *S) SetMaxConcurrency(maxConcurrency int) *S

SetMaxConcurrency sets the maximum number of sitemaps that are worked on at the same time when multi-threaded parsing is enabled. The default is 16. A value of 0 means unlimited concurrency. A positive value caps the number of sitemaps that are being fetched or parsed at any one time across the recursive sitemap-index traversal: a sitemap counts from the moment its request is sent until its content is parsed. That bounds the connections in use and the documents held in memory, which is recommended for very large sitemap indexes to avoid goroutine and connection blow-up. Negative values are rejected and a *ConfigError is recorded; a later call with a valid value clears it. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetMaxDepth added in v0.3.0

func (s *S) SetMaxDepth(maxDepth int) *S

SetMaxDepth sets the maximum recursion depth for following sitemap indexes. A sitemap index may reference other sitemap indexes; this limits how many levels deep the parser will follow. The default is 10. The value must be greater than 0; invalid values are ignored and a *ConfigError is recorded. A later call with a valid value clears it. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetMaxResponseSize added in v0.3.0

func (s *S) SetMaxResponseSize(maxResponseSize int64) *S

SetMaxResponseSize sets the maximum allowed HTTP response size in bytes. Responses exceeding this limit are rejected with a *NetworkError. The same limit caps the decompressed size of gzip-compressed content, of all the members of a gzip file together, whether it was fetched or supplied through the urlContent argument of Parse; content that expands beyond it is rejected with a *ParseError. The default is 50 MB, matching the sitemaps.org protocol limit. The value must be greater than 0; invalid values are ignored and a *ConfigError is recorded. A later call with a valid value clears it. Every value greater than 0 is a limit, math.MaxInt64 included: nothing can exceed that one, so it is the way to have no limit in effect. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetMaxSitemaps added in v1.2.0

func (s *S) SetMaxSitemaps(maxSitemaps int) *S

SetMaxSitemaps sets the maximum number of sitemaps a Parse or ParseContext call fetches. The default is 50,000, which is as many sitemaps as a sitemap index may list according to the sitemaps.org protocol. A value of 0 means no limit.

The sitemaps a robots.txt or a sitemap index lists count, on every level together: each one the call requests, whether or not the request succeeds. A sitemap that is listed more than once is requested and counted once. The document the call is made for does not count.

Once the limit is reached, the sitemaps that are left are not fetched, and a *ParseError naming the URL the call was made for is recorded, once for the call. The call does not fail: what the sitemaps fetched until then yielded is returned. With multi-threading off, the sitemaps are fetched in the order they are listed, so it is the first ones that are fetched; with multi-threading on, which ones are depends on how fast the server answers. Nothing is recorded if no sitemap had to be left out.

The limit bounds the number of requests a document can make the parser send. Without it, a server can keep a call busy for as long as it likes by listing sitemaps that list ever further sitemaps.

Negative values are rejected and a *ConfigError is recorded; a later call with a valid value clears it. The value that applies to a call is the one set when the call starts. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetMaxURLs added in v1.2.0

func (s *S) SetMaxURLs(maxURLs int) *S

SetMaxURLs sets the maximum number of URLs a Parse or ParseContext call collects. The default is 10,000,000. A value of 0 means no limit.

The URLs that GetURLs returns count. An entry that is not valid does not, and neither does a URL that the patterns set with SetRules leave out.

Once the limit is reached, the call stops collecting: what is left of the document that reached the limit is left out unread, so it adds no errors either, the sitemaps that are left are not fetched, and a *ParseError naming the URL the call was made for is recorded, once for the call. The call does not fail: the URLs collected until then are returned. With multi-threading off, they are the first ones in the order the sitemaps list them; with multi-threading on, which ones they are depends on how fast the server answers. Nothing is recorded if nothing had to be left out.

The limit bounds the memory the URLs of a call take up, whatever the documents list. The default is a last resort rather than a tight bound: ten million URLs take up gigabytes. Set a lower limit for documents that are not trusted.

Negative values are rejected and a *ConfigError is recorded; a later call with a valid value clears it. The value that applies to a call is the one set when the call starts. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetMultiThread added in v0.1.2

func (s *S) SetMultiThread(multiThread bool) *S

SetMultiThread sets the multi-threading for the Sitemap Parser. The multi-threading flag determines whether the parser should fetch and parse the sitemaps concurrently using goroutines. When it is off, the sitemaps are fetched one at a time and in the order they are listed, those a robots.txt names as well as those of a sitemap index, and SetMaxConcurrency has no effect. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetRules added in v0.1.3

func (s *S) SetRules(regexes []string) *S

SetRules sets the rules patterns using the provided list of regex strings and compiles them into regex objects. Patterns longer than maxRegexPatternLength characters are rejected with a *ConfigError. Any errors encountered during compilation are recorded as *ConfigError values. Each call replaces both the patterns and the errors recorded by the previous call. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetStrict added in v0.3.0

func (s *S) SetStrict(strict bool) *S

SetStrict enables or disables strict mode for URL validation. In strict mode, all URLs in sitemap <loc> elements must be absolute HTTP(S) URLs on the same host and protocol as the sitemap file, must hold neither a space, nor a control character, nor one of the characters " < > \ ^ ` { | }, which a URL has to give percent-encoded, and must not exceed 2048 characters, as required by the sitemaps.org specification. The host is compared without regard to letter case, and the default port of the protocol is the same as no port. In tolerant mode (default), relative URLs are resolved against the parent sitemap URL, and a space in a URL is percent-encoded. In both modes a URL has to name a host: one that names none is skipped and reported. The function returns a pointer to the S structure to allow method chaining.

func (*S) SetUserAgent

func (s *S) SetUserAgent(userAgent string) *S

SetUserAgent sets the user agent for the Sitemap Parser. The user agent is used for making HTTP requests when parsing and fetching URLs. It should be a string representing the user agent header value. The function returns a pointer to the S structure to allow method chaining.

type URL

type URL struct {
	Loc        string          `xml:"loc"`
	LastMod    *LastModTime    `xml:"lastmod"`
	ChangeFreq *URLChangeFreq  `xml:"changefreq"`
	Priority   *float32        `xml:"priority"`
	Images     []Image         `xml:"http://www.google.com/schemas/sitemap-image/1.1 image"`
	News       *News           `xml:"http://www.google.com/schemas/sitemap-news/0.9 news"`
	Videos     []Video         `xml:"http://www.google.com/schemas/sitemap-video/1.1 video"`
	Hreflangs  []AlternateLink `xml:"http://www.w3.org/1999/xhtml link"`
}

URL is a structure of <url> in <urlset>.

In the URLs that Parse and ParseContext collect, no text value has whitespace around it: the whitespace that surrounds the content of an element or the value of an attribute is not taken for a part of it. This goes for the fields of the extensions as well.

In tolerant mode Loc is a URL that can be requested as it is: a space or a character that is not in ASCII is percent-encoded in it, and an internationalised host name is in its Punycode form. A URL of an extension comes back the same way if it is an absolute HTTP or HTTPS URL, and as the document gives it otherwise: Image.Loc and Image.License, Video.ThumbnailLoc, Video.ContentLoc, Video.PlayerLoc and VideoUploader.Info, and AlternateLink.Href. Strict mode returns every URL as the document gives it.

type URLChangeFreq added in v0.3.0

type URLChangeFreq string

URLChangeFreq represents the frequency at which a URL should be crawled and indexed. Possible values are: "always", "hourly", "daily", "weekly", "monthly", "yearly", and "never".

In the URLs that Parse and ParseContext collect, a field of this type is nil when the <changefreq> element is absent or empty. In tolerant mode one of the values above that is written in another letter case, such as "Daily", is replaced by the value itself, so that it equals its constant, and any other value is kept as the document gives it. Strict mode skips and reports an entry whose value is not one of the values above as they are written here, so a field that is set equals one of the constants there.

const (
	// ChangeFreqAlways represents the "always" value for URLChangeFreq.
	ChangeFreqAlways URLChangeFreq = "always"

	// ChangeFreqHourly represents the "hourly" value for URLChangeFreq.
	ChangeFreqHourly URLChangeFreq = "hourly"

	// ChangeFreqDaily represents the "daily" value for URLChangeFreq.
	ChangeFreqDaily URLChangeFreq = "daily"

	// ChangeFreqWeekly represents the "weekly" value for URLChangeFreq.
	ChangeFreqWeekly URLChangeFreq = "weekly"

	// ChangeFreqMonthly represents the "monthly" value for URLChangeFreq.
	ChangeFreqMonthly URLChangeFreq = "monthly"

	// ChangeFreqYearly represents the "yearly" value for URLChangeFreq.
	ChangeFreqYearly URLChangeFreq = "yearly"

	// ChangeFreqNever represents the "never" value for URLChangeFreq.
	ChangeFreqNever URLChangeFreq = "never"
)

type ValidationError added in v0.9.0

type ValidationError struct {
	// URL is the URL value being validated. When it is not a URL that was rejected, it is the
	// URL the rejected value belongs to: the page for a value of a <url> entry, the sitemap
	// for an entry without a location. For a sitemap reached through a redirect that is the
	// URL the sitemap was served from.
	// May be empty for field-level errors where no specific URL is available.
	URL string
	// Err is the underlying validation error.
	Err error
}

ValidationError is returned when a URL or field value fails validation. Callers can inspect URL to determine which value was rejected.

Example usage:

var valErr *sitemap.ValidationError
if errors.As(err, &valErr) {
    fmt.Println("invalid URL:", valErr.URL)
}

func (*ValidationError) Error added in v0.9.0

func (e *ValidationError) Error() string

Error returns the message of the error, which names the URL and tells what is not valid: `validate "ftp://example.com/page": unsupported scheme "ftp"`.

func (*ValidationError) Unwrap added in v0.9.0

func (e *ValidationError) Unwrap() error

Unwrap returns the underlying error, enabling errors.Is / errors.As traversal.

type Video added in v0.8.0

type Video struct {
	ThumbnailLoc         string            `xml:"http://www.google.com/schemas/sitemap-video/1.1 thumbnail_loc"`
	Title                string            `xml:"http://www.google.com/schemas/sitemap-video/1.1 title"`
	Description          string            `xml:"http://www.google.com/schemas/sitemap-video/1.1 description"`
	ContentLoc           string            `xml:"http://www.google.com/schemas/sitemap-video/1.1 content_loc"`
	PlayerLoc            string            `xml:"http://www.google.com/schemas/sitemap-video/1.1 player_loc"`
	Duration             *int              `xml:"http://www.google.com/schemas/sitemap-video/1.1 duration"`
	ExpirationDate       *LastModTime      `xml:"http://www.google.com/schemas/sitemap-video/1.1 expiration_date"`
	Rating               *float32          `xml:"http://www.google.com/schemas/sitemap-video/1.1 rating"`
	ViewCount            *int              `xml:"http://www.google.com/schemas/sitemap-video/1.1 view_count"`
	PublicationDate      *LastModTime      `xml:"http://www.google.com/schemas/sitemap-video/1.1 publication_date"`
	FamilyFriendly       string            `xml:"http://www.google.com/schemas/sitemap-video/1.1 family_friendly"`
	Restriction          *VideoRestriction `xml:"http://www.google.com/schemas/sitemap-video/1.1 restriction"`
	Platform             *VideoPlatform    `xml:"http://www.google.com/schemas/sitemap-video/1.1 platform"`
	RequiresSubscription string            `xml:"http://www.google.com/schemas/sitemap-video/1.1 requires_subscription"`
	Uploader             *VideoUploader    `xml:"http://www.google.com/schemas/sitemap-video/1.1 uploader"`
	Live                 string            `xml:"http://www.google.com/schemas/sitemap-video/1.1 live"`
	Tags                 []string          `xml:"http://www.google.com/schemas/sitemap-video/1.1 tag"`
}

Video is a structure of <video:video> in <url>, per the Google Video Sitemap extension. Reference: https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps

type VideoPlatform added in v0.8.0

type VideoPlatform struct {
	Relationship string `xml:"relationship,attr"`
	Value        string `xml:",chardata"`
}

VideoPlatform is a structure of <video:platform> in <video:video>. It captures the element text and the required "relationship" attribute.

type VideoRestriction added in v0.8.0

type VideoRestriction struct {
	Relationship string `xml:"relationship,attr"`
	Value        string `xml:",chardata"`
}

VideoRestriction is a structure of <video:restriction> in <video:video>. It captures the element text and the required "relationship" attribute.

type VideoUploader added in v0.8.0

type VideoUploader struct {
	Info  string `xml:"info,attr"`
	Value string `xml:",chardata"`
}

VideoUploader is a structure of <video:uploader> in <video:video>. It captures the uploader name and the optional "info" URL attribute.

Directories

Path Synopsis
examples
advanced command
atom command
context command
encoding command
errors command
follow command
getrandomurls command
geturls command
gzip command
hreflang command
httpclient command
idn command
image command
maxconcurrency command
maxdepth command
maxresponsesize command
maxsitemaps command
maxurls command
multithread command
news command
redirect command
reuse command
robots command
rss command
rules command
simple command
strict command
text command
tolerant command
video command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL