Skip to main content

clear_xml_string

Function clear_xml_string 

Source
pub fn clear_xml_string(input: &str, remove_encoding_tag: bool) -> String
Expand description

Clean an XML string by removing namespace artifacts, collapsing inter-tag whitespace, and optionally stripping the <?xml … ?> declaration.

This is a direct port of the PHP Strings::clearXmlString() from sped-common. It performs the following transformations:

  1. Removes the xmlns:default="http://www.w3.org/2000/09/xmldsig#" attribute.
  2. Removes the standalone="no" attribute.
  3. Removes default: namespace prefixes and :default suffixes.
  4. Strips \n, \r, and \t characters.
  5. Collapses whitespace between adjacent XML tags (> < becomes ><).
  6. If remove_encoding_tag is true, removes the <?xml … ?> declaration.

§Examples

use fiscal_core::xml_utils::clear_xml_string;

let xml = "<root>\n  <child>text</child>\n</root>";
assert_eq!(clear_xml_string(xml, false), "<root><child>text</child></root>");

let xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><a>1</a></root>";
assert_eq!(clear_xml_string(xml2, true), "<root><a>1</a></root>");