Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for XML schemas and stylesheets #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
noRobots *bool = flag.Bool("no-robots", false, "ignores domains' robots.txt and page nofollow directives")
noLastmod *bool = flag.Bool("no-lastmod", false, "don't include last-modified information in sitemap entries")
indent *bool = flag.Bool("indent", false, "indent the entries with a tab")
stylesheet *string = flag.String("stylesheet", "", "optional URL to a stylesheet that should associated with the sitemap")
)

var (
Expand Down Expand Up @@ -307,9 +308,9 @@ type Url struct {
}

type Urlset struct {
// TODO: How to add Schema links/info?
XMLName xml.Name `xml:"urlset"`
Url []Url `xml:"url"`
XMLName xml.Name `xml:"http://www.sitemaps.org/schemas/sitemap/0.9 urlset"`
XMLSchemaLocation string `xml:"http://www.w3.org/2001/XMLSchema-instance schemaLocation,attr"`
Url []Url `xml:"url"`
}

func Get(url string, ifmod *time.Time) (*http.Response, error) {
Expand Down Expand Up @@ -397,6 +398,8 @@ func generateSitemap(path string, urls []*url.URL) (*Urlset, error) {
}()

sm := new(Urlset)
sm.XMLSchemaLocation = "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"

for {
res, ok := <-ch
if !ok {
Expand Down Expand Up @@ -495,7 +498,13 @@ func main() {
defer f.Close()
}

fmt.Fprintf(f, `<?xml version="1.0" encoding="UTF-8"?>`)
fmt.Fprintln(f, `<?xml version="1.0" encoding="UTF-8"?>`)
if len(*stylesheet) > 0 {
fmt.Fprintf(f, `<?xml-stylesheet type="text/xsl" href="`)
xml.EscapeText(f, []byte(*stylesheet))
fmt.Fprintln(f, `"?>`)
}

enc := xml.NewEncoder(f)
if *indent {
enc.Indent("", "\t")
Expand Down