#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
#![allow(clippy::collapsible_if)]
#![allow(clippy::collapsible_else_if)]
#![allow(clippy::collapsible_match)]
#![allow(clippy::large_enum_variant)]
pub mod compat;
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;
pub use error::{Error, ErrorLevel, ErrorLocation, Result, StructuredError, ValidationErrorType};
pub use document::{DocumentBuilder, XmlDocument};
pub use node::{NodeId, NodeType, XmlNode, XmlRoNode};
pub use namespace::{Namespace, NamespaceResolver};
pub use compact_str::CompactString;
pub use parser::{Parser, ParserOptions, parse_schema_locations};
pub(crate) use parser::parse;
pub use position::PositionTrackingReader;
pub use xpath::context::{XmlContext, XmlSafeContext};
pub use serialize::Printer;
pub use xpath::{AsQuery, Query, QueryExt};
#[cfg(test)]
mod tests {
use super::*;
use crate::compat::*;
#[test]
fn test_basic_parsing() {
let xml = r#"<root attr="value"><child>text</child></root>"#;
let doc = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = Parser::from(xml).parse().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 = crate::schema::xsd::parse_xsd(xsd).unwrap();
assert!(!schema.elements.is_empty() || !schema.types.is_empty());
}
}