HUGO
Menu
GitHub 90265 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;

When you print the result in an HTML template, Hugo escapes the entities again. To prevent this, pass the result through the safe.HTML function. See safe types for details.

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>