fastxml 0.8.1

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
//! Integration tests for XML parsing.

mod common;

use fastxml::{NodeType, get_node_tag, get_root_node, parse};

#[test]
fn test_parse_simple_xml() {
    let xml = r#"<root><child>text</child></root>"#;
    let doc = parse(xml).unwrap();

    let root = get_root_node(&doc).unwrap();
    assert_eq!(get_node_tag(&root), "root");

    let children = root.get_child_elements();
    assert_eq!(children.len(), 1);
    assert_eq!(children[0].get_name(), "child");
    assert_eq!(children[0].get_content(), Some("text".to_string()));

    // Compare with libxml
    compare_with_libxml!(parse: xml, &doc);
}

#[test]
fn test_parse_with_attributes() {
    let xml = r#"<root id="1" name="test"><child type="element"/></root>"#;
    let doc = parse(xml).unwrap();

    let root = get_root_node(&doc).unwrap();
    assert_eq!(root.get_attribute("id"), Some("1".to_string()));
    assert_eq!(root.get_attribute("name"), Some("test".to_string()));

    let children = root.get_child_elements();
    assert_eq!(
        children[0].get_attribute("type"),
        Some("element".to_string())
    );

    // Compare with libxml
    compare_with_libxml!(parse: xml, &doc);
}

#[test]
fn test_parse_namespaced_xml() {
    let xml = r#"<gml:root xmlns:gml="http://www.opengis.net/gml" xmlns:bldg="http://www.opengis.net/citygml/building/2.0">
        <gml:featureMember>
            <bldg:Building gml:id="bldg_001">
                <bldg:measuredHeight>15.5</bldg:measuredHeight>
            </bldg:Building>
        </gml:featureMember>
    </gml:root>"#;

    let doc = parse(xml).unwrap();
    let root = get_root_node(&doc).unwrap();

    assert_eq!(root.get_name(), "root");
    assert_eq!(root.get_prefix(), Some("gml".to_string()));
    assert_eq!(root.qname(), "gml:root");

    let ns_decls = root.get_namespace_declarations();
    assert_eq!(ns_decls.len(), 2);

    // Compare with libxml
    compare_with_libxml!(parse: xml, &doc);
}

#[test]
fn test_parse_mixed_content() {
    let xml = r#"<root>text before<child/>text after</root>"#;
    let doc = parse(xml).unwrap();

    let root = get_root_node(&doc).unwrap();
    let children = root.get_child_nodes();

    // Should have: text, element, text
    assert!(children.len() >= 2);

    compare_with_libxml!(parse: xml, &doc);
}

#[test]
fn test_parse_cdata() {
    let xml = r#"<root><![CDATA[<not xml> & special chars]]></root>"#;
    let doc = parse(xml).unwrap();

    let root = get_root_node(&doc).unwrap();
    let content = root.get_content().unwrap();
    assert!(content.contains("<not xml>"));
    assert!(content.contains("& special"));

    compare_with_libxml!(parse: xml, &doc);
}

#[test]
fn test_parse_comments() {
    let xml = r#"<root><!-- this is a comment --><child/></root>"#;
    let doc = parse(xml).unwrap();

    let root = get_root_node(&doc).unwrap();
    let children = root.get_child_nodes();
    assert!(!children.is_empty());

    // Verify comment node is in AST
    let comment_nodes: Vec<_> = children
        .iter()
        .filter(|n| n.get_type() == NodeType::Comment)
        .collect();
    assert_eq!(comment_nodes.len(), 1);
    assert_eq!(
        comment_nodes[0].get_content(),
        Some(" this is a comment ".to_string())
    );

    compare_with_libxml!(parse: xml, &doc);
}

#[test]
fn test_parse_empty_elements() {
    let xml = r#"<root><empty1/><empty2></empty2></root>"#;
    let doc = parse(xml).unwrap();

    let root = get_root_node(&doc).unwrap();
    let children = root.get_child_elements();
    assert_eq!(children.len(), 2);

    compare_with_libxml!(parse: xml, &doc);
}

#[test]
fn test_parse_deeply_nested() {
    let xml = r#"<a><b><c><d><e><f>deep</f></e></d></c></b></a>"#;
    let doc = parse(xml).unwrap();

    let root = get_root_node(&doc).unwrap();
    assert_eq!(root.get_name(), "a");

    // Navigate down
    let mut current = root;
    let expected = ["b", "c", "d", "e", "f"];
    for name in expected {
        let children = current.get_child_elements();
        assert_eq!(children.len(), 1);
        assert_eq!(children[0].get_name(), name);
        current = children[0].clone();
    }

    assert_eq!(current.get_content(), Some("deep".to_string()));

    compare_with_libxml!(parse: xml, &doc);
}

#[test]
fn test_parse_special_characters() {
    let xml = r#"<root attr="&lt;value&gt;">&amp; &lt; &gt; &quot; &apos;</root>"#;
    let doc = parse(xml).unwrap();

    let root = get_root_node(&doc).unwrap();
    let attr = root.get_attribute("attr").unwrap();
    assert_eq!(attr, "<value>");

    compare_with_libxml!(parse: xml, &doc);
}

#[test]
fn test_node_count() {
    let xml = r#"<root><a/><b/><c/></root>"#;
    let doc = parse(xml).unwrap();

    // Document node + root + 3 children = 5 nodes minimum
    assert!(doc.node_count() >= 4);
}

// =============================================================================
// HTML Parsing Tests
// =============================================================================

/// Test parsing XHTML (XML-compatible HTML)
#[test]
fn test_parse_xhtml_with_doctype() {
    // XHTML with DOCTYPE - DOCTYPE is skipped by the parser
    let html = r#"<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test Page</title>
        <meta charset="UTF-8"/>
    </head>
    <body>
        <h1>Hello World</h1>
        <p>This is a paragraph.</p>
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();

    assert_eq!(root.get_name(), "html");

    let children = root.get_child_elements();
    assert_eq!(children.len(), 2); // head and body

    let head = &children[0];
    let body = &children[1];
    assert_eq!(head.get_name(), "head");
    assert_eq!(body.get_name(), "body");

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML5 DOCTYPE
#[test]
fn test_parse_html5_doctype() {
    let html = r#"<!DOCTYPE html>
<html>
    <head><title>HTML5</title></head>
    <body><p>Content</p></body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();
    assert_eq!(root.get_name(), "html");

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML with comments
#[test]
fn test_parse_html_with_comments() {
    let html = r#"<!DOCTYPE html>
<html>
    <!-- This is a header comment -->
    <head>
        <title>Test</title>
        <!-- Meta tags would go here -->
    </head>
    <body>
        <!-- Main content starts -->
        <div>
            <!-- Nested comment -->
            <p>Hello</p>
        </div>
        <!-- Main content ends -->
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();
    assert_eq!(root.get_name(), "html");

    // Verify comments are preserved in AST
    let root_comments: Vec<_> = root
        .get_child_nodes()
        .into_iter()
        .filter(|n| n.get_type() == NodeType::Comment)
        .collect();
    assert_eq!(root_comments.len(), 1);
    assert!(
        root_comments[0]
            .get_content()
            .unwrap()
            .contains("header comment")
    );

    let body = root
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "body")
        .unwrap();

    // Body should have 2 comments
    let body_comments: Vec<_> = body
        .get_child_nodes()
        .into_iter()
        .filter(|n| n.get_type() == NodeType::Comment)
        .collect();
    assert_eq!(body_comments.len(), 2);

    let div = body
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "div")
        .unwrap();

    // Div should have 1 comment
    let div_comments: Vec<_> = div
        .get_child_nodes()
        .into_iter()
        .filter(|n| n.get_type() == NodeType::Comment)
        .collect();
    assert_eq!(div_comments.len(), 1);
    assert!(
        div_comments[0]
            .get_content()
            .unwrap()
            .contains("Nested comment")
    );

    let p = div.get_child_elements();
    assert_eq!(p.len(), 1);
    assert_eq!(p[0].get_name(), "p");

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML with self-closing tags (XHTML style)
#[test]
fn test_parse_html_self_closing_tags() {
    let html = r#"<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <link rel="stylesheet" href="style.css"/>
    </head>
    <body>
        <img src="image.png" alt="Test"/>
        <br/>
        <hr/>
        <input type="text" name="field"/>
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();
    assert_eq!(root.get_name(), "html");

    let body = root
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "body")
        .unwrap();
    let elements = body.get_child_elements();

    // Should have img, br, hr, input
    let names: Vec<_> = elements.iter().map(|e| e.get_name()).collect();
    assert!(names.contains(&"img".to_string()));
    assert!(names.contains(&"br".to_string()));
    assert!(names.contains(&"hr".to_string()));
    assert!(names.contains(&"input".to_string()));

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML with various attributes
#[test]
fn test_parse_html_attributes() {
    let html = r#"<!DOCTYPE html>
<html lang="en">
    <head><title>Attrs</title></head>
    <body>
        <div id="main" class="container" data-value="123">
            <a href="https://example.com" target="_blank" rel="noopener">Link</a>
            <button disabled="disabled" onclick="alert('hi')">Click</button>
        </div>
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();

    assert_eq!(root.get_attribute("lang"), Some("en".to_string()));

    let body = root
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "body")
        .unwrap();
    let div = body.get_child_elements()[0].clone();

    assert_eq!(div.get_attribute("id"), Some("main".to_string()));
    assert_eq!(div.get_attribute("class"), Some("container".to_string()));
    assert_eq!(div.get_attribute("data-value"), Some("123".to_string()));

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML with script and style tags containing special characters
#[test]
fn test_parse_html_with_cdata_content() {
    // Using CDATA for script content to handle special chars
    let html = r#"<!DOCTYPE html>
<html>
    <head>
        <style><![CDATA[
            body { color: red; }
            .class > child { margin: 0; }
        ]]></style>
    </head>
    <body>
        <script><![CDATA[
            if (a < b && c > d) {
                console.log("test");
            }
        ]]></script>
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();
    assert_eq!(root.get_name(), "html");

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML table structure
#[test]
fn test_parse_html_table() {
    let html = r#"<!DOCTYPE html>
<html>
    <body>
        <table>
            <thead>
                <tr><th>Name</th><th>Value</th></tr>
            </thead>
            <tbody>
                <tr><td>Item 1</td><td>100</td></tr>
                <tr><td>Item 2</td><td>200</td></tr>
            </tbody>
        </table>
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();

    let body = root
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "body")
        .unwrap();
    let table = body.get_child_elements()[0].clone();
    assert_eq!(table.get_name(), "table");

    let sections = table.get_child_elements();
    assert_eq!(sections.len(), 2); // thead, tbody

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML form elements
#[test]
fn test_parse_html_form() {
    let html = r#"<!DOCTYPE html>
<html>
    <body>
        <form action="/submit" method="post">
            <label for="name">Name:</label>
            <input type="text" id="name" name="name"/>
            <select name="option">
                <option value="1">One</option>
                <option value="2" selected="selected">Two</option>
            </select>
            <textarea name="comment">Default text</textarea>
            <button type="submit">Submit</button>
        </form>
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();

    let body = root
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "body")
        .unwrap();
    let form = body.get_child_elements()[0].clone();

    assert_eq!(form.get_name(), "form");
    assert_eq!(form.get_attribute("action"), Some("/submit".to_string()));
    assert_eq!(form.get_attribute("method"), Some("post".to_string()));

    compare_with_libxml!(parse: html, &doc);
}

/// Test XHTML strict with namespaces
#[test]
fn test_parse_xhtml_strict() {
    let html = r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>XHTML Strict</title>
    </head>
    <body>
        <p>Valid XHTML 1.0 Strict document.</p>
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();

    assert_eq!(root.get_name(), "html");
    assert_eq!(root.get_attribute("lang"), Some("en".to_string()));

    // Check namespace declaration
    let ns_decls = root.get_namespace_declarations();
    assert!(!ns_decls.is_empty());

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML with entities
#[test]
fn test_parse_html_entities() {
    let html = r#"<!DOCTYPE html>
<html>
    <body>
        <p>Less than: &lt; Greater than: &gt;</p>
        <p>Ampersand: &amp; Quote: &quot; Apos: &apos;</p>
        <p attr="value with &quot;quotes&quot;">Text</p>
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();

    let body = root
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "body")
        .unwrap();
    let paragraphs = body.get_child_elements();

    // First paragraph should contain decoded entities
    let content = paragraphs[0].get_content().unwrap();
    assert!(content.contains('<'));
    assert!(content.contains('>'));

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML with multiple comments in sequence
#[test]
fn test_parse_html_multiple_comments() {
    let html = r#"<!DOCTYPE html>
<html>
    <body>
        <!-- First comment -->
        <!-- Second comment -->
        <p>Between comments</p>
        <!-- Third comment -->
        <!-- Fourth comment with special chars: <>&"' -->
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();

    let body = root
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "body")
        .unwrap();

    // All 4 comments should be preserved in AST
    let comments: Vec<_> = body
        .get_child_nodes()
        .into_iter()
        .filter(|n| n.get_type() == NodeType::Comment)
        .collect();
    assert_eq!(comments.len(), 4);

    // Verify comment contents
    assert!(comments[0].get_content().unwrap().contains("First"));
    assert!(comments[1].get_content().unwrap().contains("Second"));
    assert!(comments[2].get_content().unwrap().contains("Third"));
    assert!(comments[3].get_content().unwrap().contains("Fourth"));
    // Special chars in comment should be preserved
    assert!(comments[3].get_content().unwrap().contains("<>&"));

    // The paragraph should still be parseable
    let p = body
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "p")
        .unwrap();
    assert_eq!(p.get_content(), Some("Between comments".to_string()));

    compare_with_libxml!(parse: html, &doc);
}

/// Test minimal HTML document
#[test]
fn test_parse_minimal_html() {
    let html = r#"<!DOCTYPE html><html><body>Hello</body></html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();
    assert_eq!(root.get_name(), "html");

    compare_with_libxml!(parse: html, &doc);
}

/// Test HTML with deeply nested divs
#[test]
fn test_parse_html_deeply_nested() {
    let html = r#"<!DOCTYPE html>
<html>
    <body>
        <div class="l1">
            <div class="l2">
                <div class="l3">
                    <div class="l4">
                        <div class="l5">
                            <span>Deep content</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>"#;

    let doc = parse(html).unwrap();
    let root = get_root_node(&doc).unwrap();

    // Navigate to the deepest element
    let body = root
        .get_child_elements()
        .into_iter()
        .find(|e| e.get_name() == "body")
        .unwrap();

    let mut current = body.get_child_elements()[0].clone();
    for level in 2..=5 {
        assert_eq!(
            current.get_attribute("class"),
            Some(format!("l{}", level - 1))
        );
        current = current.get_child_elements()[0].clone();
    }

    let span = current.get_child_elements()[0].clone();
    assert_eq!(span.get_name(), "span");
    assert_eq!(span.get_content(), Some("Deep content".to_string()));

    compare_with_libxml!(parse: html, &doc);
}