Skip to main content

replace_unacceptable_characters

Function replace_unacceptable_characters 

Source
pub fn replace_unacceptable_characters(input: &str) -> String
Expand 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:

  1. Remove < and >.
  2. Replace & with & (space-padded).
  3. Remove single quotes (') and double quotes (").
  4. Collapse multiple consecutive whitespace characters into a single space.
  5. Encode the remaining & as &amp;.
  6. Remove carriage return (\r), tab (\t), and line feed (\n).
  7. Collapse multiple whitespace again (from normalize step).
  8. Remove ASCII control characters (0x000x1F, 0x7F), except space.
  9. 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 &amp; Jerry cats"
);
assert_eq!(
    replace_unacceptable_characters("  hello   world  "),
    "hello world"
);