HUGO
Menu
GitHub 90221 stars Mastodon

transform.XMLEscape

Returns the given string, removing disallowed characters then escaping the result to its XML equivalent.
Syntax
transform.XMLEscape INPUT
Returns
string

Usage

The transform.XMLEscape function removes disallowed characters as defined in the XML specification, then escapes the result by replacing these characters with HTML entities:

  • " → "
  • ' → '
  • & → &
  • < → &lt;
  • > → &gt;
  • \t → &#x9;
  • \n → &#xA;
  • \r → &#xD;

The function returns a string. Go’s html/template package escapes strings in most contexts, which escapes the entities in the result again. To render the result as is, pass it through the safe.HTML function.

Examples

{{ transform.XMLEscape "<p>abc</p>" }} → &lt;p&gt;abc&lt;/p&gt;

Comparing the result with and without safe.HTML, showing the raw HTML output:

{{ transform.XMLEscape "<p>abc</p>" }} → &amp;lt;p&amp;gt;abc&amp;lt;/p&amp;gt;
{{ transform.XMLEscape "<p>abc</p>" | safeHTML }} → &lt;p&gt;abc&lt;/p&gt;

In an RSS template:

layouts/rss.xml
<description>{{ .Summary | transform.XMLEscape | safeHTML }}</description>