fastxml 0.11.0

A fast, memory-efficient XML library with XPath and XSD validation support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Tests for error handling with malformed XML and validation violations.

use fastxml::error::Error;
use fastxml::parser::error::ParseError;
use fastxml::xpath::error::{XPathEvalError, XPathSyntaxError};
use fastxml::{Parser, ParserOptions};

// =============================================================================
// Malformed XML Tests
// =============================================================================

mod malformed_xml {
    use super::*;

    #[test]
    fn test_unclosed_tag() {
        let xml = "<root><child>";
        let result = Parser::from(xml).parse();
        // An element left unclosed at EOF is not well-formed.
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Expected NotWellFormed for an unclosed element, got: {result:?}"
        );
    }

    #[test]
    fn test_mismatched_tags() {
        let xml = "<root></roo>";
        //         0123456789AB (hex) = positions
        //         "<root>" = 6 bytes, "</roo>" = 6 bytes, total = 12 bytes
        let result = Parser::from(xml).parse();
        match &result {
            Err(Error::Parse(ParseError::AtPosition { message, position })) => {
                assert!(
                    message.contains("expected") || message.contains("mismatch"),
                    "Expected mismatch message, got: {}",
                    message
                );
                // Error detected at end of closing tag (byte 12)
                assert_eq!(
                    *position, 12,
                    "Error position should be at end of closing tag"
                );
            }
            _ => panic!(
                "Expected Parse error with mismatch message, got: {:?}",
                result
            ),
        }
    }

    #[test]
    fn test_mismatched_nested_tags() {
        let xml = "<root><child></root></child>";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_tag_name_starting_with_number() {
        let xml = "<1root/>";
        let result = Parser::from(xml).parse();
        // A digit is a NameChar but not a NameStartChar (XML 1.0 P5), so a name
        // beginning with one is not well-formed.
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for tag name starting with a digit, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_tag_name_illegal_char() {
        // U+00D7 (MULTIPLICATION SIGN) is a legal XML character but falls in a
        // gap of the BaseChar production, so it is not a NameChar.
        let xml = "<a\u{00D7}b/>";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for illegal character in name, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_attribute_name_starting_with_number() {
        let xml = r#"<root 1attr="x"/>"#;
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for attribute name starting with a digit, got: {:?}",
            result
        );
    }

    #[test]
    fn test_valid_unicode_tag_name() {
        // Names built from Letters/Ideographics outside ASCII must be accepted
        // (regression guard against an overly strict NameChar table).
        for xml in ["<日本語/>", "<Élément/>", "<café/>", "<_x.y-z0/>"] {
            let result = Parser::from(xml).parse();
            assert!(
                result.is_ok(),
                "Expected valid Unicode name to parse, xml={:?}, got: {:?}",
                xml,
                result
            );
        }
    }

    #[test]
    fn test_invalid_tag_name_with_space() {
        let xml = "<root element/>";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for tag with space, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_attribute_no_value() {
        let xml = r#"<root attr=>"#;
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for attribute without value, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_attribute_no_quotes() {
        let xml = r#"<root attr=value/>"#;
        let result = Parser::from(xml).parse();
        // quick-xml does NOT accept unquoted attribute values
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for unquoted attribute, got: {:?}",
            result
        );
    }

    #[test]
    fn test_duplicate_attributes() {
        let xml = r#"<root attr="1" attr="2"/>"#;
        let result = Parser::from(xml).parse();
        // quick-xml returns error for duplicate attributes
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for duplicate attributes, got: {:?}",
            result
        );
    }

    #[test]
    fn test_unescaped_ampersand() {
        let xml = "<root>a & b</root>";
        let result = Parser::from(xml).parse();
        // quick-xml error: "Cannot find ';' after '&'"
        assert!(
            matches!(&result, Err(Error::Parse(_))),
            "Expected Parse error for unescaped &, got: {:?}",
            result
        );
    }

    #[test]
    fn test_unescaped_less_than() {
        let xml = "<root>a < b</root>";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for unescaped <, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_entity_reference() {
        let xml = "<root>&invalid;</root>";
        let result = Parser::from(xml).parse();
        // quick-xml returns error for unknown entities
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for invalid entity, got: {:?}",
            result
        );
    }

    #[test]
    fn test_incomplete_entity_reference() {
        let xml = "<root>&amp</root>";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for incomplete entity, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_xml_declaration() {
        let xml = r#"<?xml version="2.0"?><root/>"#;
        let result = Parser::from(xml).parse();
        // VersionNum is '1.' [0-9]+ (XML 1.0 P26); "2.0" is not well-formed.
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Expected NotWellFormed for version 2.0, got: {result:?}"
        );
    }

    #[test]
    fn test_xml_declaration_not_at_start() {
        let xml = " <?xml version=\"1.0\"?><root/>";
        let result = Parser::from(xml).parse();
        // The XML declaration must be the very first thing in the document; even
        // leading white space makes it not well-formed.
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Expected NotWellFormed for a declaration not at the start, got: {result:?}"
        );
    }

    #[test]
    fn test_multiple_root_elements() {
        let xml = "<root1/><root2/>";
        let result = Parser::from(xml).parse();
        // A document may contain only one root element.
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Expected NotWellFormed for a second root element, got: {result:?}"
        );
    }

    #[test]
    fn test_empty_input() {
        let xml = "";
        let result = Parser::from(xml).parse();
        // A well-formed document must contain a root element.
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Empty input should be rejected (no document element), got: {result:?}"
        );
    }

    #[test]
    fn test_whitespace_only() {
        let xml = "   \n\t  ";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Whitespace-only input should be rejected (no document element), got: {result:?}"
        );
    }

    #[test]
    fn test_comment_only() {
        let xml = "<!-- just a comment -->";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Comment-only input should be rejected (no document element), got: {result:?}"
        );
    }

    #[test]
    fn test_unclosed_comment() {
        let xml = "<root><!-- unclosed comment</root>";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for unclosed comment, got: {:?}",
            result
        );
    }

    #[test]
    fn test_double_hyphen_in_comment() {
        let xml = "<root><!-- invalid -- comment --></root>";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for -- in comment, got: {:?}",
            result
        );
    }

    #[test]
    fn test_unclosed_cdata() {
        let xml = "<root><![CDATA[unclosed</root>";
        let result = Parser::from(xml).parse();
        assert!(
            matches!(result, Err(Error::Parse(_))),
            "Expected Parse error for unclosed CDATA, got: {:?}",
            result
        );
    }

    #[test]
    fn test_cdata_end_in_cdata() {
        let xml = "<root><![CDATA[contains ]]> in middle]]></root>";
        let result = Parser::from(xml).parse();
        // The CDATA ends at the first ']]>'; the following ' in middle]]>' is
        // character data, and the literal ']]>' there is not well-formed.
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Expected NotWellFormed for ']]>' in character data, got: {result:?}"
        );
    }

    #[test]
    fn test_invalid_namespace_prefix() {
        let xml = r#"<unknown:root/>"#;
        let result = Parser::from(xml).parse();
        // fastxml is namespace-aware: an element prefix must be bound in scope.
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Expected NotWellFormed for an unbound element prefix, got: {result:?}"
        );
    }

    #[test]
    fn test_xml_reserved_prefix() {
        let xml = r#"<xml:element xmlns:xml="http://wrong.url"/>"#;
        let result = Parser::from(xml).parse();
        // The 'xml' prefix may only be bound to the XML namespace.
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Expected NotWellFormed for a misbound 'xml' prefix, got: {result:?}"
        );
    }

    #[test]
    fn test_deeply_nested_elements() {
        let depth = 1000;
        let open_tags: String = (0..depth).map(|i| format!("<e{}>", i)).collect();
        let close_tags: String = (0..depth).rev().map(|i| format!("</e{}>", i)).collect();
        let xml = format!("{}{}", open_tags, close_tags);

        let result = Parser::from(xml.as_str()).parse();
        // Should succeed - no stack overflow
        assert!(result.is_ok(), "Deep nesting should be handled");
    }

    #[test]
    fn test_extremely_long_tag_name() {
        let long_name = "a".repeat(10000);
        let xml = format!("<{}/>", long_name);

        let result = Parser::from(xml.as_str()).parse();
        assert!(result.is_ok());
        let doc = result.unwrap();
        let root = doc.get_root_element().unwrap();
        assert_eq!(root.get_name().len(), 10000);
    }

    #[test]
    fn test_extremely_long_attribute_value() {
        let long_value = "x".repeat(100000);
        let xml = format!(r#"<root attr="{}"/>"#, long_value);

        let result = Parser::from(xml.as_str()).parse();
        assert!(result.is_ok());
        let doc = result.unwrap();
        let root = doc.get_root_element().unwrap();
        let attr = root.get_attribute("attr");
        assert_eq!(attr.map(|s| s.len()), Some(100000));
    }

    #[test]
    fn test_null_byte_in_content() {
        let xml = "<root>hello\0world</root>";
        let result = Parser::from(xml).parse();
        // A literal NUL is not a legal XML character (Char production).
        assert!(result.is_err(), "NUL in content is not well-formed");
    }

    #[test]
    fn test_control_characters() {
        let xml = "<root>\x01\x02\x03</root>";
        let result = Parser::from(xml).parse();
        // C0 control characters (other than tab/LF/CR) violate the Char
        // production and must be rejected.
        assert!(
            result.is_err(),
            "control characters in content are not well-formed"
        );
    }

    #[test]
    fn test_invalid_utf8() {
        let invalid_bytes: &[u8] = &[
            0x3c, 0x72, 0x6f, 0x6f, 0x74, 0x3e, 0xff, 0xfe, 0x3c, 0x2f, 0x72, 0x6f, 0x6f, 0x74,
            0x3e,
        ];
        let result = Parser::from(invalid_bytes).parse();
        // Invalid UTF-8 should cause an error
        assert!(
            matches!(
                result,
                Err(Error::Parse(_) | Error::Utf8(_) | Error::FromUtf8(_))
            ),
            "Expected encoding error, got: {:?}",
            result
        );
    }
}

// =============================================================================
// Parser Options Tests
// =============================================================================

mod parser_options {
    use super::*;

    #[test]
    fn test_memory_limit_exceeded() {
        let options = ParserOptions {
            max_memory: Some(100),
            ..Default::default()
        };

        let large_xml = format!("<root>{}</root>", "x".repeat(1000));
        let result = Parser::from(large_xml.as_str()).options(options).parse();
        assert!(
            matches!(
                &result,
                Err(Error::Parse(ParseError::MemoryLimitExceeded { .. }))
            ),
            "Expected memory limit error, got: {:?}",
            result
        );
    }

    #[test]
    fn test_memory_limit_not_exceeded() {
        let options = ParserOptions {
            max_memory: Some(1_000_000),
            ..Default::default()
        };

        let xml = "<root>small content</root>";
        let result = Parser::from(xml).options(options).parse();
        assert!(result.is_ok());
    }

    #[test]
    fn test_default_options() {
        let options = ParserOptions::default();
        let xml = "<root><child>text</child></root>";
        let result = Parser::from(xml).options(options).parse();
        assert!(result.is_ok());
    }
}

// =============================================================================
// XPath Error Tests
// =============================================================================

mod xpath_errors {
    use super::*;
    use fastxml::QueryExt;

    #[test]
    fn test_invalid_xpath_unclosed_bracket() {
        let doc = Parser::from("<root/>").parse().unwrap();
        let result = doc.query("/root[");
        assert!(
            matches!(result, Err(Error::XPathSyntax(_))),
            "Expected XPathSyntax error, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_xpath_unclosed_parenthesis() {
        let doc = Parser::from("<root/>").parse().unwrap();
        let result = doc.query("count(/root");
        assert!(
            matches!(result, Err(Error::XPathSyntax(_))),
            "Expected XPathSyntax error, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_xpath_unknown_function() {
        use super::XPathEvalError;
        let doc = Parser::from("<root/>").parse().unwrap();
        let result = doc.query("unknownfn()");
        // Unknown function returns error
        assert!(
            matches!(&result, Err(Error::XPathEval(XPathEvalError::UnknownFunction { name })) if name == "unknownfn"),
            "Expected XPathEval error for unknown function, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_xpath_unknown_axis() {
        use super::XPathSyntaxError;
        let doc = Parser::from("<root/>").parse().unwrap();
        let result = doc.query("unknownaxis::*");
        // Unknown axis returns error
        assert!(
            matches!(&result, Err(Error::XPathSyntax(XPathSyntaxError::UnknownAxis { name })) if name == "unknownaxis"),
            "Expected XPathSyntax error for unknown axis, got: {:?}",
            result
        );
    }

    #[test]
    fn test_invalid_xpath_empty() {
        let doc = Parser::from("<root/>").parse().unwrap();
        let result = doc.query("");
        assert!(
            matches!(result, Err(Error::XPathSyntax(_))),
            "Expected XPathSyntax error for empty expression, got: {:?}",
            result
        );
    }

    #[test]
    fn test_xpath_double_slash_at_end() {
        let doc = Parser::from("<root><child/></root>").parse().unwrap();
        let result = doc.query("/root//");
        // Trailing // matches all descendants of root
        assert!(result.is_ok(), "Expected Ok, got: {:?}", result);
        let nodes = result.unwrap().into_nodes();
        // Returns root and child (all descendants including self)
        assert!(!nodes.is_empty(), "// at end matches all descendants");
    }

    #[test]
    fn test_invalid_xpath_missing_operand() {
        let doc = Parser::from("<root/>").parse().unwrap();
        let result = doc.query("/root +");
        assert!(
            matches!(result, Err(Error::XPathSyntax(_))),
            "Expected XPathSyntax error, got: {:?}",
            result
        );
    }

    #[test]
    fn test_xpath_division_by_zero() {
        let doc = Parser::from("<root/>").parse().unwrap();
        let result = doc.query("1 div 0");
        // XPath 1.0: division by zero returns Infinity, not an error
        assert!(result.is_ok(), "XPath division by zero returns Infinity");
    }

    #[test]
    fn test_xpath_invalid_number() {
        let doc = Parser::from("<root/>").parse().unwrap();
        let result = doc.query("number('not a number') + 1");
        // XPath: number('invalid') returns NaN, arithmetic with NaN is valid
        assert!(result.is_ok(), "XPath NaN arithmetic is valid");
    }

    #[test]
    fn test_xpath_on_empty_document() {
        // An empty document is now rejected at parse time (no root element).
        let result = Parser::from("").parse();
        assert!(
            matches!(result, Err(Error::Parse(ParseError::NotWellFormed { .. }))),
            "Expected NotWellFormed for an empty document, got: {result:?}"
        );
    }
}

// =============================================================================
// Streaming Parser Error Tests
// =============================================================================

mod streaming_errors {
    use fastxml::Parser;

    #[test]
    fn test_streaming_malformed_xml() {
        let xml = "<root><unclosed>";
        let result = Parser::from(xml).for_each_event(|_event| Ok(()));
        // An element left unclosed at EOF is not well-formed.
        assert!(
            result.is_err(),
            "Streaming should reject an unclosed element at EOF"
        );
    }

    #[test]
    fn test_streaming_valid_xml() {
        let xml = "<root><child>text</child></root>";
        let result = Parser::from(xml).for_each_event(|_event| Ok(()));
        assert!(result.is_ok());
    }

    #[test]
    fn test_streaming_empty_input() {
        let xml = "";
        let result = Parser::from(xml).for_each_event(|_event| Ok(()));
        // A well-formed document must contain a root element.
        assert!(
            result.is_err(),
            "Streaming should reject empty input (no document element)"
        );
    }

    #[test]
    fn test_streaming_mismatched_tags() {
        use fastxml::error::Error;
        use fastxml::parser::error::ParseError;

        let xml = "<root></wrong>";
        //         01234567890123 = positions (14 bytes total)
        //         "<root>" = 6 bytes, "</wrong>" = 8 bytes
        let result = Parser::from(xml).for_each_event(|_event| Ok(()));
        match &result {
            Err(Error::Parse(ParseError::AtPosition { message, position })) => {
                assert!(
                    message.contains("expected")
                        || message.contains("mismatch")
                        || message.contains("EndTag"),
                    "Expected mismatch message, got: {}",
                    message
                );
                // Error detected at end of closing tag (byte 14)
                assert_eq!(
                    *position, 14,
                    "Error position should be at end of closing tag"
                );
            }
            _ => panic!(
                "Expected Parse error for mismatched tags, got: {:?}",
                result
            ),
        }
    }
}