Function: xml-escape-string
xml-escape-string is an autoloaded and byte-compiled function defined
in xml.el.gz.
Signature
(xml-escape-string STRING &optional NOERROR)
Documentation
Convert STRING into a string containing valid XML character data.
Replace occurrences of &<>'" in STRING with their default XML entity references (e.g., replace each & with &).
XML character data must not contain & or < characters, nor the >
character under some circumstances. The XML spec does not impose
restriction on " or ', but we just substitute for these too
(as is permitted by the spec).
If STRING contains characters that are invalid in XML (as defined
by https://www.w3.org/TR/xml/#charsets), operate depending on the
value of NOERROR: if it is non-nil, remove them; else, signal an
error of type xml-invalid-character.
Source Code
;; Defined in /usr/src/emacs/lisp/xml.el.gz
(defun xml-escape-string (string &optional noerror)
"Convert STRING into a string containing valid XML character data.
Replace occurrences of &<>\\='\" in STRING with their default XML
entity references (e.g., replace each & with &).
XML character data must not contain & or < characters, nor the >
character under some circumstances. The XML spec does not impose
restriction on \" or \\=', but we just substitute for these too
\(as is permitted by the spec).
If STRING contains characters that are invalid in XML (as defined
by https://www.w3.org/TR/xml/#charsets), operate depending on the
value of NOERROR: if it is non-nil, remove them; else, signal an
error of type `xml-invalid-character'."
(with-temp-buffer
(insert string)
(goto-char (point-min))
(while (re-search-forward xml-invalid-characters-re nil t)
(if noerror
(replace-match "")
(signal 'xml-invalid-character
(list (char-before) (match-beginning 0)))))
(dolist (substitution '(("&" . "&")
("<" . "<")
(">" . ">")
("'" . "'")
("\"" . """)))
(goto-char (point-min))
(while (search-forward (car substitution) nil t)
(replace-match (cdr substitution) t t nil)))
(buffer-string)))