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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//! fastxml - Fast, memory-efficient XML library with XPath and XSD validation support.
//!
//! This library provides XML parsing, XPath evaluation, and XSD schema validation
//! with a focus on memory efficiency through streaming processing.
//!
//! # Features
//!
//! - **Fast XML Parsing**: Built on quick-xml for high-performance parsing
//! - **XPath Support**: Evaluate XPath 1.0 expressions
//! - **XSD Validation**: Stream-based schema validation
//! - **Memory Efficient**: Streaming APIs to minimize memory usage
//! - **Thread Safe**: Safe concurrent access through careful design
//! - **Async Support**: Async schema fetching and resolution with tokio
//!
//! # Feature Flags
//!
//! - `ureq`: Enables synchronous HTTP client for schema fetching (recommended)
//! - `tokio`: Enables async HTTP client and schema resolution with reqwest
//! - `async-trait`: Enables async trait support for custom implementations
//! - `profile`: Enables memory profiling utilities
//!
//! # Quick Start
//!
//! ```rust
//! use fastxml::{parse, xpath, get_root_node, get_node_tag};
//!
//! // Parse XML
//! let xml = r#"<root xmlns:gml="http://www.opengis.net/gml">
//!     <gml:name>Hello World</gml:name>
//! </root>"#;
//!
//! let doc = parse(xml).unwrap();
//!
//! // Get root element
//! let root = get_root_node(&doc).unwrap();
//! println!("Root element: {}", get_node_tag(&root));
//!
//! // Evaluate XPath
//! let result = xpath::evaluate(&doc, "//gml:name").unwrap();
//! let texts = xpath::collect_text_values(&result);
//! println!("Found: {:?}", texts);
//! ```
//!
//! # libxml API Compatibility
//!
//! This library provides API compatibility with the libxml crate for easier migration:
//!
//! ```rust
//! use fastxml::{
//!     // Types
//!     XmlDocument, XmlNode, XmlRoNode, XmlContext,
//!     // Parsing
//!     parse,
//!     // XPath
//!     evaluate, create_context, find_nodes_by_xpath,
//!     collect_text_values, collect_text_value,
//!     // Node operations
//!     get_root_node, get_root_readonly_node, get_node_tag,
//!     node_to_xml_string, readonly_node_to_xml_string,
//!     // Schema validation
//!     create_xml_schema_validation_context,
//!     validate_document_by_schema,
//!     parse_schema_locations,
//! };
//! ```
//!
//! # Streaming Processing
//!
//! For large files, use streaming APIs to minimize memory usage:
//!
//! ```rust
//! use fastxml::event::{StreamingParser, XmlEventHandler, XmlEvent};
//! use fastxml::error::Result;
//!
//! struct MyHandler;
//!
//! impl XmlEventHandler for MyHandler {
//!     fn handle(&mut self, event: &XmlEvent) -> Result<()> {
//!         match event {
//!             XmlEvent::StartElement { name, .. } => {
//!                 println!("Start: {}", name);
//!             }
//!             _ => {}
//!         }
//!         Ok(())
//!     }
//!
//!     fn as_any(self: Box<Self>) -> Box<dyn std::any::Any> {
//!         self
//!     }
//! }
//!
//! let xml = "<root><child/></root>";
//! let mut parser = StreamingParser::new(xml.as_bytes());
//! parser.add_handler(Box::new(MyHandler));
//! parser.parse().unwrap();
//! ```
//!
//! # Async Schema Resolution
//!
//! Parse XSD schemas with async import/include resolution (requires `tokio` feature):
//!
//! ```ignore
//! use fastxml::schema::{
//!     AsyncDefaultFetcher,
//!     parse_xsd_with_imports_async,
//! };
//!
//! #[tokio::main]
//! async fn main() -> fastxml::error::Result<()> {
//!     let xsd_content = std::fs::read("schema.xsd")?;
//!     let fetcher = AsyncDefaultFetcher::new()?;
//!
//!     let schema = parse_xsd_with_imports_async(
//!         &xsd_content,
//!         "http://example.com/schema.xsd",
//!         &fetcher,
//!     ).await?;
//!
//!     Ok(())
//! }
//! ```

#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
// Allow some clippy lints for code clarity
#![allow(clippy::collapsible_if)]
#![allow(clippy::collapsible_else_if)]
#![allow(clippy::collapsible_match)]
#![allow(clippy::large_enum_variant)]

pub mod document;
pub mod error;
pub mod event;
pub mod generator;
pub mod namespace;
pub mod node;
pub mod parser;
pub mod position;
pub mod profile;
pub mod schema;
pub mod serialize;
pub mod transform;
pub mod xpath;

// Re-export error types
pub use error::{Error, ErrorLevel, ErrorLocation, Result, StructuredError, ValidationErrorType};

// Re-export document types
pub use document::{DocumentBuilder, XmlDocument};

// Re-export node types
pub use node::{NodeId, NodeType, XmlNode, XmlRoNode};

// Re-export namespace types
pub use namespace::{Namespace, NamespaceResolver};

// Re-export compact_str for use in XmlEvent attributes
pub use compact_str::CompactString;

// Re-export parser functions
pub use parser::{
    ParserOptions, parse, parse_from_bufread, parse_schema_locations, parse_with_options,
};

// Re-export position tracking
pub use position::PositionTrackingReader;

// Re-export XPath context (for libxml compatibility)
pub use xpath::context::{XmlContext, XmlSafeContext};

// ============================================================================
// libxml-compatible API
// ============================================================================

/// Evaluates an XPath expression on a document.
///
/// This is the main XPath evaluation entry point, compatible with libxml's API.
pub fn evaluate<T: AsRef<str>>(
    document: &XmlDocument,
    xpath_expr: T,
) -> Result<xpath::XPathResult> {
    xpath::evaluate(document, xpath_expr.as_ref())
}

/// Creates an XPath context for a document.
///
/// The context automatically registers namespace bindings from the root element.
pub fn create_context(document: &XmlDocument) -> Result<XmlContext> {
    xpath::create_context(document)
}

/// Creates a thread-safe XPath context for a document.
pub fn create_safe_context(document: &XmlDocument) -> Result<XmlSafeContext> {
    xpath::create_safe_context(document)
}

/// Finds nodes by XPath expression relative to a node.
pub fn find_nodes_by_xpath(
    ctx: &XmlContext,
    xpath_expr: &str,
    node: &XmlNode,
) -> Result<Vec<XmlNode>> {
    xpath::find_nodes_by_xpath(ctx, xpath_expr, node)
}

/// Finds read-only nodes by XPath expression.
pub fn find_readonly_nodes_by_xpath(
    ctx: &XmlContext,
    xpath_expr: &str,
    node: &XmlRoNode,
) -> Result<Vec<XmlRoNode>> {
    xpath::find_readonly_nodes_by_xpath(ctx, xpath_expr, node)
}

/// Finds read-only nodes using a thread-safe context.
pub fn find_safe_readonly_nodes_by_xpath(
    ctx: &XmlSafeContext,
    xpath_expr: &str,
    node: &XmlRoNode,
) -> Result<Vec<XmlRoNode>> {
    xpath::find_safe_readonly_nodes_by_xpath(ctx, xpath_expr, node)
}

/// Finds read-only nodes matching element names.
pub fn find_readonly_nodes_in_elements(
    ctx: &XmlContext,
    node: &XmlRoNode,
    elements_to_match: &[&str],
) -> Result<Vec<XmlRoNode>> {
    xpath::find_readonly_nodes_in_elements(ctx, node, elements_to_match)
}

/// Collects text values from an XPath result.
pub fn collect_text_values(xpath_value: &xpath::XPathResult) -> Vec<String> {
    xpath::collect_text_values(xpath_value)
}

/// Collects a single text value from an XPath result.
pub fn collect_text_value(xpath_value: &xpath::XPathResult) -> String {
    xpath::collect_text_value(xpath_value)
}

/// Gets the root element node from a document.
pub fn get_root_node(document: &XmlDocument) -> Result<XmlNode> {
    document.get_root_element()
}

/// Gets the root element as a read-only node.
pub fn get_root_readonly_node(document: &XmlDocument) -> Result<XmlRoNode> {
    document.get_root_element_ro()
}

/// Gets the qualified tag name of a node (prefix:name or just name).
pub fn get_node_tag(node: &XmlNode) -> String {
    node.qname()
}

/// Gets the qualified tag name of a read-only node.
pub fn get_readonly_node_tag(node: &XmlRoNode) -> String {
    node.qname()
}

/// Gets the namespace prefix of a node.
pub fn get_node_prefix(node: &XmlNode) -> String {
    node.get_prefix().unwrap_or_default()
}

/// Gets the namespace prefix of a read-only node.
pub fn get_readonly_node_prefix(node: &XmlRoNode) -> String {
    node.get_prefix().unwrap_or_default()
}

/// Serializes a node to an XML string.
pub fn node_to_xml_string(document: &XmlDocument, node: &mut XmlNode) -> Result<String> {
    serialize::node_to_xml_string(document, node)
}

/// Serializes a read-only node to an XML string.
pub fn readonly_node_to_xml_string(document: &XmlDocument, node: &XmlRoNode) -> Result<String> {
    serialize::readonly_node_to_xml_string(document, node)
}

/// Creates an XSD schema validation context.
pub fn create_xml_schema_validation_context(
    schema_location: String,
) -> Result<schema::XmlSchemaValidationContext> {
    schema::create_xml_schema_validation_context(&schema_location)
}

/// Creates an XSD schema validation context from a buffer.
pub fn create_xml_schema_validation_context_from_buffer(
    schema: &[u8],
) -> Result<schema::XmlSchemaValidationContext> {
    schema::create_xml_schema_validation_context_from_buffer(schema)
}

/// Validates a document against an XSD schema.
pub fn validate_document_by_schema(
    document: &XmlDocument,
    schema_location: String,
) -> Result<Vec<StructuredError>> {
    schema::validate_document_by_schema(document, &schema_location)
}

/// Validates a document using an existing validation context.
pub fn validate_document_by_schema_context(
    document: &XmlDocument,
    ctx: &schema::XmlSchemaValidationContext,
) -> Result<Vec<StructuredError>> {
    schema::validate_document_by_schema_context(document, ctx)
}

/// Parses XSD content and returns a compiled schema.
pub fn parse_xsd(content: &[u8]) -> Result<schema::types::CompiledSchema> {
    schema::parse_xsd(content)
}

/// Parses XSD content with import resolution.
pub fn parse_xsd_with_imports<F: schema::SchemaFetcher>(
    content: &[u8],
    base_uri: &str,
    fetcher: &F,
) -> Result<schema::types::CompiledSchema> {
    schema::parse_xsd_with_imports(content, base_uri, fetcher)
}

/// Parses multiple XSD entry schemas with shared import/include resolution.
///
/// Uses a single resolver so shared dependencies are only fetched once.
/// This is the recommended approach when `xsi:schemaLocation` specifies
/// multiple schema entries.
///
/// # Example
///
/// ```ignore
/// use fastxml::schema::UreqFetcher;
/// use fastxml::parse_xsd_with_imports_multiple;
///
/// let fetcher = UreqFetcher::new();
///
/// let schema = parse_xsd_with_imports_multiple(
///     &[
///         ("http://example.com/a.xsd", &a_content),
///         ("http://example.com/b.xsd", &b_content),
///     ],
///     &fetcher,
/// )?;
/// ```
pub fn parse_xsd_with_imports_multiple<F: schema::SchemaFetcher>(
    entries: &[(&str, &[u8])],
    fetcher: &F,
) -> Result<schema::types::CompiledSchema> {
    schema::parse_xsd_with_imports_multiple(entries, fetcher)
}

/// Parses multiple XSD entry schemas with shared import/include resolution (async).
///
/// This is the async version of [`parse_xsd_with_imports_multiple`].
///
/// # Example
///
/// ```ignore
/// use fastxml::schema::AsyncDefaultFetcher;
/// use fastxml::parse_xsd_with_imports_multiple_async;
///
/// let fetcher = AsyncDefaultFetcher::new()?;
///
/// let schema = parse_xsd_with_imports_multiple_async(
///     &[
///         ("http://example.com/a.xsd", &a_content),
///         ("http://example.com/b.xsd", &b_content),
///     ],
///     &fetcher,
/// ).await?;
/// ```
#[cfg(feature = "tokio")]
pub async fn parse_xsd_with_imports_multiple_async<F: schema::AsyncSchemaFetcher>(
    entries: &[(&str, &[u8])],
    fetcher: &F,
) -> Result<schema::types::CompiledSchema> {
    schema::parse_xsd_with_imports_multiple_async(entries, fetcher).await
}

/// Parses XSD content with async import resolution.
///
/// This is the async version of [`parse_xsd_with_imports`]. It resolves all
/// `xs:import` and `xs:include` dependencies asynchronously.
///
/// # Example
///
/// ```ignore
/// use fastxml::schema::AsyncDefaultFetcher;
/// use fastxml::parse_xsd_with_imports_async;
///
/// let fetcher = AsyncDefaultFetcher::new()?;
///
/// let schema = parse_xsd_with_imports_async(
///     &xsd_content,
///     "http://example.com/schema.xsd",
///     &fetcher,
/// ).await?;
/// ```
#[cfg(feature = "tokio")]
pub async fn parse_xsd_with_imports_async<F: schema::AsyncSchemaFetcher>(
    content: &[u8],
    base_uri: &str,
    fetcher: &F,
) -> Result<schema::types::CompiledSchema> {
    schema::parse_xsd_with_imports_async(content, base_uri, fetcher).await
}

/// Validates a document using schemas referenced in xsi:schemaLocation.
///
/// This function reads the `xsi:schemaLocation` attribute from the document's
/// root element, fetches the referenced schemas, and validates the document.
///
/// # Example
///
/// ```ignore
/// use fastxml::{parse, validate_with_schema_location};
///
/// let xml = r#"<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
///                   xsi:schemaLocation="http://example.com/ns http://example.com/schema.xsd">
///     <child>content</child>
/// </root>"#;
///
/// let doc = parse(xml)?;
/// let errors = validate_with_schema_location(&doc)?;
/// ```
#[cfg(feature = "ureq")]
pub fn validate_with_schema_location(document: &XmlDocument) -> Result<Vec<StructuredError>> {
    schema::validate_with_schema_location(document)
}

/// Validates a document using schemas referenced in xsi:schemaLocation with a custom fetcher.
///
/// This function reads the `xsi:schemaLocation` attribute from the document's
/// root element, fetches the referenced schemas using the provided fetcher,
/// and validates the document.
pub fn validate_with_schema_location_and_fetcher<F: schema::SchemaFetcher>(
    document: &XmlDocument,
    fetcher: &F,
) -> Result<Vec<StructuredError>> {
    schema::validate_with_schema_location_and_fetcher(document, fetcher)
}

/// Gets a compiled schema from xsi:schemaLocation in XML content.
///
/// This function parses the XML to extract xsi:schemaLocation, fetches the
/// referenced schemas, and returns a compiled schema suitable for streaming validation.
///
/// # Example
///
/// ```ignore
/// use fastxml::get_schema_from_schema_location;
/// use fastxml::event::StreamingParser;
/// use fastxml::schema::validator::OnePassSchemaValidator;
/// use std::sync::Arc;
/// use std::io::BufReader;
///
/// let xml_bytes = std::fs::read("document.xml")?;
/// let schema = Arc::new(get_schema_from_schema_location(&xml_bytes)?);
///
/// let mut parser = StreamingParser::new(BufReader::new(xml_bytes.as_slice()));
/// parser.add_handler(Box::new(OnePassSchemaValidator::new(schema)));
/// parser.parse()?;
/// ```
#[cfg(feature = "ureq")]
pub fn get_schema_from_schema_location(
    xml_content: &[u8],
) -> Result<schema::types::CompiledSchema> {
    schema::get_schema_from_schema_location(xml_content)
}

/// Gets a compiled schema from xsi:schemaLocation with a custom fetcher.
pub fn get_schema_from_schema_location_with_fetcher<F: schema::SchemaFetcher>(
    xml_content: &[u8],
    fetcher: &F,
) -> Result<schema::types::CompiledSchema> {
    schema::get_schema_from_schema_location_with_fetcher(xml_content, fetcher)
}

/// Validates XML from a reader using streaming parser with schemas from xsi:schemaLocation.
///
/// This performs true single-pass streaming validation:
/// 1. On the first StartElement, extracts xsi:schemaLocation
/// 2. Fetches and compiles the referenced schemas
/// 3. Continues streaming validation with the fetched schema
///
/// # Example
///
/// ```ignore
/// use fastxml::streaming_validate_with_schema_location;
/// use std::fs::File;
/// use std::io::BufReader;
///
/// let file = File::open("large_document.xml")?;
/// let errors = streaming_validate_with_schema_location(BufReader::new(file))?;
/// ```
#[cfg(feature = "ureq")]
pub fn streaming_validate_with_schema_location<R: std::io::BufRead>(
    reader: R,
) -> Result<Vec<StructuredError>> {
    schema::streaming_validate_with_schema_location(reader)
}

/// Validates XML from a reader using streaming parser with a custom fetcher.
pub fn streaming_validate_with_schema_location_and_fetcher<
    R: std::io::BufRead,
    F: schema::SchemaFetcher + 'static,
>(
    reader: R,
    fetcher: F,
) -> Result<Vec<StructuredError>> {
    schema::streaming_validate_with_schema_location_and_fetcher(reader, fetcher)
}

/// Validates a document using schemas referenced in xsi:schemaLocation asynchronously.
///
/// This is the async version of [`validate_with_schema_location`]. It fetches schemas
/// asynchronously using the default async fetcher (`AsyncDefaultFetcher`).
///
/// # Example
///
/// ```ignore
/// use fastxml::{parse, validate_with_schema_location_async};
///
/// let xml = r#"<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
///                   xsi:schemaLocation="http://example.com/ns http://example.com/schema.xsd">
///     <child>content</child>
/// </root>"#;
///
/// let doc = parse(xml)?;
/// let errors = validate_with_schema_location_async(&doc).await?;
/// ```
#[cfg(feature = "tokio")]
pub async fn validate_with_schema_location_async(
    document: &XmlDocument,
) -> Result<Vec<StructuredError>> {
    schema::validate_with_schema_location_async(document).await
}

/// Validates a document using schemas referenced in xsi:schemaLocation with an async fetcher.
///
/// This is the async version of [`validate_with_schema_location_and_fetcher`].
/// It fetches schemas asynchronously using the provided fetcher.
#[cfg(feature = "tokio")]
pub async fn validate_with_schema_location_with_async_fetcher<F: schema::AsyncSchemaFetcher>(
    document: &XmlDocument,
    fetcher: &F,
) -> Result<Vec<StructuredError>> {
    schema::validate_with_schema_location_with_async_fetcher(document, fetcher).await
}

/// Gets a compiled schema from xsi:schemaLocation asynchronously.
///
/// This is the async version of [`get_schema_from_schema_location`]. It fetches schemas
/// asynchronously using the default async fetcher (`AsyncDefaultFetcher`).
///
/// # Example
///
/// ```ignore
/// use fastxml::get_schema_from_schema_location_async;
/// use fastxml::schema::validator::OnePassSchemaValidator;
/// use std::sync::Arc;
///
/// let xml_bytes = std::fs::read("document.xml")?;
/// let schema = Arc::new(get_schema_from_schema_location_async(&xml_bytes).await?);
/// ```
#[cfg(feature = "tokio")]
pub async fn get_schema_from_schema_location_async(
    xml_content: &[u8],
) -> Result<schema::types::CompiledSchema> {
    schema::get_schema_from_schema_location_async(xml_content).await
}

/// Gets a compiled schema from xsi:schemaLocation with an async fetcher.
///
/// This is the async version of [`get_schema_from_schema_location_with_fetcher`].
/// It fetches schemas asynchronously using the provided fetcher.
#[cfg(feature = "tokio")]
pub async fn get_schema_from_schema_location_with_async_fetcher<F: schema::AsyncSchemaFetcher>(
    xml_content: &[u8],
    fetcher: &F,
) -> Result<schema::types::CompiledSchema> {
    schema::get_schema_from_schema_location_with_async_fetcher(xml_content, fetcher).await
}

#[cfg(test)]
mod tests {
    use super::*;

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

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

    #[test]
    fn test_xpath_evaluation() {
        let xml = r#"<root><Building/><Room/><Window/></root>"#;
        let doc = parse(xml).unwrap();

        let result = evaluate(&doc, "//*[name()='Building']").unwrap();
        let nodes = result.into_nodes();
        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].get_name(), "Building");
    }

    #[test]
    fn test_context_xpath() {
        let xml = r#"<root><a>1</a><a>2</a></root>"#;
        let doc = parse(xml).unwrap();
        let ctx = create_context(&doc).unwrap();
        let root = get_root_readonly_node(&doc).unwrap();

        let nodes = find_readonly_nodes_by_xpath(&ctx, "//a", &root).unwrap();
        assert_eq!(nodes.len(), 2);
    }

    #[test]
    fn test_text_collection() {
        let xml = r#"<root><a>one</a><a>two</a></root>"#;
        let doc = parse(xml).unwrap();

        let result = evaluate(&doc, "/root/a").unwrap();
        let texts = collect_text_values(&result);
        assert_eq!(texts, vec!["one", "two"]);
    }

    #[test]
    fn test_namespaced_xml() {
        let xml = r#"<gml:root xmlns:gml="http://www.opengis.net/gml">
            <gml:name>test</gml:name>
        </gml:root>"#;
        let doc = parse(xml).unwrap();

        let result = evaluate(&doc, "/gml:root/gml:name").unwrap();
        let nodes = result.into_nodes();
        assert_eq!(nodes.len(), 1);
    }

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

        let serialized = node_to_xml_string(&doc, &mut root).unwrap();
        assert!(serialized.contains("<root>"));
        assert!(serialized.contains("<child>text</child>"));
    }

    #[test]
    fn test_create_safe_context() {
        let xml = r#"<root><a>1</a></root>"#;
        let doc = parse(xml).unwrap();
        let ctx = create_safe_context(&doc).unwrap();
        let root = get_root_readonly_node(&doc).unwrap();

        let nodes = find_safe_readonly_nodes_by_xpath(&ctx, "//a", &root).unwrap();
        assert_eq!(nodes.len(), 1);
    }

    #[test]
    fn test_find_nodes_by_xpath() {
        let xml = r#"<root><a>1</a><b>2</b></root>"#;
        let doc = parse(xml).unwrap();
        let ctx = create_context(&doc).unwrap();
        let root = get_root_node(&doc).unwrap();

        let nodes = find_nodes_by_xpath(&ctx, "//a", &root).unwrap();
        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].get_name(), "a");
    }

    #[test]
    fn test_find_readonly_nodes_in_elements() {
        let xml = r#"<root><a>1</a><b>2</b><c>3</c></root>"#;
        let doc = parse(xml).unwrap();
        let ctx = create_context(&doc).unwrap();
        let root = get_root_readonly_node(&doc).unwrap();

        let nodes = find_readonly_nodes_in_elements(&ctx, &root, &["a", "c"]).unwrap();
        assert_eq!(nodes.len(), 2);
    }

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

        let result = evaluate(&doc, "/root/a").unwrap();
        let text = collect_text_value(&result);
        assert_eq!(text, "hello");
    }

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

        let result = evaluate(&doc, "/root/nonexistent").unwrap();
        let text = collect_text_value(&result);
        assert!(text.is_empty());
    }

    #[test]
    fn test_get_readonly_node_tag() {
        let xml = r#"<ns:root xmlns:ns="http://example.com"/>"#;
        let doc = parse(xml).unwrap();
        let root = get_root_readonly_node(&doc).unwrap();

        assert_eq!(get_readonly_node_tag(&root), "ns:root");
    }

    #[test]
    fn test_get_node_prefix() {
        let xml = r#"<ns:root xmlns:ns="http://example.com"/>"#;
        let doc = parse(xml).unwrap();
        let root = get_root_node(&doc).unwrap();

        assert_eq!(get_node_prefix(&root), "ns");
    }

    #[test]
    fn test_get_node_prefix_empty() {
        let xml = r#"<root/>"#;
        let doc = parse(xml).unwrap();
        let root = get_root_node(&doc).unwrap();

        assert_eq!(get_node_prefix(&root), "");
    }

    #[test]
    fn test_get_readonly_node_prefix() {
        let xml = r#"<ns:root xmlns:ns="http://example.com"/>"#;
        let doc = parse(xml).unwrap();
        let root = get_root_readonly_node(&doc).unwrap();

        assert_eq!(get_readonly_node_prefix(&root), "ns");
    }

    #[test]
    fn test_get_readonly_node_prefix_empty() {
        let xml = r#"<root/>"#;
        let doc = parse(xml).unwrap();
        let root = get_root_readonly_node(&doc).unwrap();

        assert_eq!(get_readonly_node_prefix(&root), "");
    }

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

        let serialized = readonly_node_to_xml_string(&doc, &root).unwrap();
        assert!(serialized.contains("<root>"));
        assert!(serialized.contains("<child>text</child>"));
    }

    #[test]
    fn test_evaluate_with_string_ref() {
        let xml = r#"<root><a>1</a></root>"#;
        let doc = parse(xml).unwrap();
        let xpath_str = String::from("//a");

        let result = evaluate(&doc, &xpath_str).unwrap();
        let nodes = result.into_nodes();
        assert_eq!(nodes.len(), 1);
    }

    #[test]
    fn test_parse_xsd_basic() {
        let xsd = br#"<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root" type="xs:string"/>
</xs:schema>"#;

        let schema = parse_xsd(xsd).unwrap();
        assert!(!schema.elements.is_empty() || !schema.types.is_empty());
    }

    #[test]
    fn test_create_xml_schema_validation_context_from_buffer() {
        let xsd = br#"<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root" type="xs:string"/>
</xs:schema>"#;

        let ctx = create_xml_schema_validation_context_from_buffer(xsd).unwrap();
        // Just verify it creates successfully
        let _ = ctx;
    }
}