HUGO

Sitemap.xsl not processable

I would like to process a sitemap.xsl (stylesheet for sitemap.xml) dynamically and render the variable {{ .Site.Params.Sitetitle }} into it.

Therefore I created a sitemap.xsl in layouts/sitemap.xsl which also is the standard directory for the sitemap.xml

Apparently when I run build my page, it will not get touched at all, nor processed. It will nor appear in the public folder.

Normaly sitemap.xsl files are static, but in mind I would like to begin like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                xmlns:html="http://www.w3.org/TR/REC-html40"
                xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
                xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title>{{ .Site.Params.Sitetitle }} XML Sitemap</title>

Which renders the current Sitetitle into the title of the sitemap.xsl. Therefore the XSL file must be processed otherwise it will not update when I update the title, or when this will be used on other sites.
Is there a option I missed to realise this?
Or is this getting done differently here in Hugo?
I could not find the needed info in the Docs

assets/
└── sitemap.xsl

baseof.html

{{ if .IsHome }}
  {{ $sitemap_xsl_template := resources.Get "sitemap.xsl" }}
  {{ $sitemap_xsl := $sitemap_xsl_template | resources.ExecuteAsTemplate "sitemap.xsl" . }}
  {{ $noop := $sitemap_xsl.Permalink }}
{{ end }}

There may be better ways to do this, but this is what came to mind this evening.

1 Like

Another option is using custom outputs: .htaccess, humans.txt with template logic

2 Likes

Thank you both!
I tried it like this:

config.toml

[outputs]
    home = ["HTML", "sitemapxsl"]

[outputFormats]
[outputFormats.sitemapxsl]
    baseName = "sitemap"
    mediaType = "text/plain"

[mediaTypes]
    [mediaTypes."application/xslt+xml"]
        suffixes = "xsl"
layouts/
└── index.sitemapxsl.xsl

But I anyway get the error/warning:

WARN 2021/05/03 04:17:18 found no layout file for "sitemapxsl" for layout "main" for kind "home": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.

Isnt the index.sitemapxsl.xsl the right file with the right name in the right folder here?
How to exactly call that file from my layout β€œmain”?
The link describes it as:

But I somehow cant manage to make it work

The following config, + layouts/index.sitemapxsl.xsl works for me:

[outputs]
  home = ["HTML", "sitemapxsl"]

[outputFormats]
  [outputFormats.sitemapxsl]
    baseName = "sitemap"
    mediaType = "application/xslt+xml" ### Note: this bit
[mediaTypes]
  [mediaTypes."application/xslt+xml"]  ### to correspond to this bit
    suffixes = ["xsl"]

That did the trick. You saved my day, thank you!

I anyway think that a sitemap.xsl probably should be supported by default. like all other standard things aswell, so everyone can just place a sitemap.xsl into the layouts/ folder and are good to go

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.