pub fn replace_unacceptable_characters(input: &str) -> StringExpand description
Replace characters that are valid in XML but rejected by SEFAZ.
This is a SEFAZ-level sanitisation function, distinct from escape_xml.
While escape_xml performs standard XML entity encoding, this function
mirrors the PHP Strings::replaceUnacceptableCharacters from sped-common:
- Remove
<and>. - Replace
&with&(space-padded). - Remove single quotes (
') and double quotes ("). - Collapse multiple consecutive whitespace characters into a single space.
- Encode the remaining
&as&. - Remove carriage return (
\r), tab (\t), and line feed (\n). - Collapse multiple whitespace again (from normalize step).
- Remove ASCII control characters (
0x00–0x1F,0x7F), except space. - Trim leading and trailing whitespace.
The function is designed to be called on user-provided field values
(e.g. xJust, xCorrecao, xPag) before they are placed into the
NF-e XML, so that the SEFAZ web-service will not reject the document
because of forbidden characters.
§Examples
use fiscal_core::xml_utils::replace_unacceptable_characters;
assert_eq!(
replace_unacceptable_characters("Tom & Jerry <cats>"),
"Tom & Jerry cats"
);
assert_eq!(
replace_unacceptable_characters(" hello world "),
"hello world"
);