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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 James Conway
//! XML for Parsec: [XML 1.0 (Fifth Edition)](https://www.w3.org/TR/xml/)
//! and [Namespaces in XML 1.0 (Third Edition)](https://www.w3.org/TR/xml-names/),
//! parsed to a tree with namespace-resolved names and a span on every
//! node and attribute.
//!
//! ```
//! let feed = r#"<?xml version="1.0"?>
//! <feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
//! <entry><title>Ships & ports</title></entry>
//! </feed>"#;
//! let doc = parsec_xml::parse(feed).unwrap();
//! let root = doc.root_element();
//! let name = doc.name(root).unwrap();
//! assert_eq!((name.local, name.namespace), ("feed", "http://www.w3.org/2005/Atom"));
//! assert_eq!(doc.xml_lang(root), Some("en"));
//! assert_eq!(doc.text(root).trim(), "Ships & ports");
//! ```
//!
//! XML is a trust boundary, and the parser treats it as one:
//!
//! - It doesn't validate, and it never fetches or opens anything. The
//! external subset and external entities are left unread, and the
//! document's [`warnings`](Document::warnings) say so, as the spec asks
//! of a processor that doesn't read them.
//! - Entities the internal subset declares are expanded, no deeper than
//! [`Options::max_entity_depth`] and to no more text in all than
//! [`Options::entity_allowance`] plus [`Options::entity_ratio`] bytes per
//! byte of input, so a billion laughs or a quadratic blowup stops with
//! an error. An attribute default counts each time an element gets it,
//! so a big default on many elements stops the same way.
//! - Elements nest no deeper than [`Limits::max_depth`]: 2,048 unless you
//! say otherwise. The parser keeps open elements on a stack of its own,
//! so even a million levels with no limit cost time in proportion and
//! no recursion.
//! - Anything that isn't well-formed is an [`Error`], fatal as the spec
//! says, with a code, a span, and the line and column.
//!
//! ## XPath
//!
//! With the `xpath` feature (`parsec-xml = { version = "0.3", features =
//! ["xpath"] }`), a [`Document`] is a tree `xpath` can query, and
//! `parsec_xml::xpath` is `parsec-xpath` re-exported. No HTML parser
//! comes with it. Names compare exactly, a query's prefixes are bound by
//! the namespaces it's run with, `xmlns` and `xmlns:*` aren't attributes,
//! `id()` finds `xml:id` and attributes the internal subset declares an
//! `ID`, and `lang()` reads `xml:lang`. The namespace axis has the
//! namespaces in scope at each element, from its `xmlns` attributes and
//! its ancestors'.
//!
//! ```
//! # #[cfg(feature = "xpath")] {
//! use parsec_xml::xpath::{Doc, Index, Item, NoExtensions, Value, XPath};
//!
//! let doc = parsec_xml::parse(r#"<feed xmlns="http://www.w3.org/2005/Atom">
//! <entry><title>Ships</title></entry><entry><title>Ports</title></entry>
//! </feed>"#).unwrap();
//! // Build the index once per document; every query reads it.
//! let index = Index::build(&doc, &[0]);
//! let atom = [("a".to_string(), "http://www.w3.org/2005/Atom".to_string())];
//! let query = XPath::new("//a:entry[2]/a:title").unwrap();
//! let Ok(Value::Nodes(hits)) =
//! query.evaluate(Doc::new(&doc, &index), 0, &[], &atom, &NoExtensions)
//! else {
//! panic!("a node-set")
//! };
//! let Item::Node(title) = hits[0] else { panic!("a node") };
//! let title = parsec_xml::NodeId::from_index(title as usize);
//! assert_eq!(doc.text(title), "Ports");
//! # }
//! ```
use fmt;
pub use ;
pub use Encoding;
pub use ENTITY_DEPTH_CEILING;
pub use ;
pub use parsec_xpath as xpath;
pub use ;
/// How elements may nest unless you say otherwise: lxml's limit with
/// `huge_tree`, deeper than any document we know of that isn't an attack.
pub const DEFAULT_MAX_DEPTH: u32 = 2048;
/// How to parse.
/// Why a document isn't well-formed XML, or couldn't be parsed within its
/// limits.
/// Parses a document with the default [`Options`].
/// Parses a document. A byte order mark at the start is skipped; an
/// encoding the XML declaration names is ignored, since the text is
/// decoded already.
///
/// ```
/// let e = parsec_xml::parse("<a>\n <b></a>").unwrap_err();
/// assert_eq!(e.code(), "mismatched-end-tag");
/// assert_eq!(e.to_string(), "2:6: `</a>` closes `<b>` [mismatched-end-tag]");
/// ```