quick-xml icon indicating copy to clipboard operation
quick-xml copied to clipboard

End-of-Line Handling seems not confront to xml specification

Open yorkz1994 opened this issue 1 year ago • 1 comments

Take this xml content for example, note: \r\n is used for line break.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<t>ABC
DEF</t>

And with below code to print Text event:

fn main() {
    let buf = BufReader::new(File::open("a.xml").unwrap());
    let mut read = Reader::from_reader(buf);
    let mut buf = Vec::new();
    loop {
        buf.clear();
        match read.read_event_into(&mut buf) {
            Ok(Event::Eof) => break,
            Ok(Event::Text(t)) => {
                let s = t.unescape().unwrap().to_string();
                println!("{s:?}");
            }
            Err(e) => panic!("{e}"),
            _ => {}
        }
    }
}

Output:

"\r\n"
"ABC\r\nDEF

Expected output should not have \r:

"\n"
"ABC\nDEF

Because as xml specification

2.11 End-of-Line Handling XML parsed entities are often stored in computer files which, for editing convenience, are organized into lines. These lines are typically separated by some combination of the characters CARRIAGE RETURN (#xD) and LINE FEED (#xA).

To simplify the tasks of applications, the XML processor MUST behave as if it normalized all line breaks in external parsed entities (including the document entity) on input, before parsing, by translating both the two-character sequence #xD #xA and any #xD that is not followed by #xA to a single #xA character.

I found golang xml library has below code for handling it.

// We must rewrite unescaped \r and \r\n into \n.
		if b == '\r' {
			d.buf.WriteByte('\n')
		} else if b1 == '\r' && b == '\n' {
			// Skip \r\n--we already wrote \n.
		} else {
			d.buf.WriteByte(b)
		}

yorkz1994 avatar Sep 24 '24 06:09 yorkz1994

Yes, normalization of input does not performed at all. Related issues is #371, #670 and #749

Mingun avatar Sep 24 '24 06:09 Mingun