Debian Codegraph @

docs/content/en/functions/transform/XMLEscape.md

raw
1 ---
2 title: transform.XMLEscape
3 description: Returns the given string, removing disallowed characters then escaping the result to its XML equivalent.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: string
10 signatures: [transform.XMLEscape INPUT]
11 ---
12
13 The `transform.XMLEscape` function removes [disallowed characters][] as defined in the XML specification, then escapes the result by replacing the following characters with [HTML entities][]:
14
15 - `"` → `"`
16 - `'` → `'`
17 - `&` → `&`
18 - `<` → `&lt;`
19 - `>` → `&gt;`
20 - `\t` → `&#x9;`
21 - `\n` → `&#xA;`
22 - `\r` → `&#xD;`
23
24 For example:
25
26 ```go-html-template
27 {{ transform.XMLEscape "<p>abc</p>" }} → &lt;p&gt;abc&lt;/p&gt;
28 ```
29
30 When using `transform.XMLEscape` in a template rendered by Go's [`html/template`][] package, declare the string to be safe HTML to avoid double escaping. For example, in an RSS template:
31
32 ```xml {file="layouts/rss.xml"}
33 <description>{{ .Summary | transform.XMLEscape | safeHTML }}</description>
34 ```
35
36 [HTML entities]: https://developer.mozilla.org/en-US/docs/Glossary/Entity
37 [`html/template`]: https://pkg.go.dev/html/template
38 [disallowed characters]: https://www.w3.org/TR/xml/#charsets