@@ -96,35 +96,169 @@ impl<R> NsReader<R> {
96
96
97
97
/// Getters
98
98
impl < R > NsReader < R > {
99
- /// Resolves a potentially qualified **event name** into (namespace name, local name).
99
+ /// Resolves a potentially qualified **element name** or **attribute name**
100
+ /// into (namespace name, local name).
100
101
///
101
- /// *Qualified* attribute names have the form `prefix:local-name` where the`prefix` is defined
102
- /// on any containing XML element via `xmlns:prefix="the:namespace:uri"`. The namespace prefix
103
- /// can be defined on the same element as the attribute in question.
102
+ /// *Qualified* names have the form `prefix:local-name` where the `prefix`
103
+ /// is defined on any containing XML element via `xmlns:prefix="the:namespace:uri"`.
104
+ /// The namespace prefix can be defined on the same element as the name in question.
104
105
///
105
- /// *Unqualified* event inherits the current *default namespace*.
106
+ /// The method returns following results depending on the `name` shape,
107
+ /// `attribute` flag and the presence of the default namespace:
108
+ ///
109
+ /// |attribute|`xmlns="..."`|QName |ResolveResult |LocalName
110
+ /// |---------|-------------|-------------------|-----------------------|------------
111
+ /// |`true` |Not defined |`local-name` |[`Unbound`] |`local-name`
112
+ /// |`true` |Defined |`local-name` |[`Unbound`] |`local-name`
113
+ /// |`true` |_any_ |`prefix:local-name`|[`Bound`] / [`Unknown`]|`local-name`
114
+ /// |`false` |Not defined |`local-name` |[`Unbound`] |`local-name`
115
+ /// |`false` |Defined |`local-name` |[`Bound`] (default) |`local-name`
116
+ /// |`false` |_any_ |`prefix:local-name`|[`Bound`] / [`Unknown`]|`local-name`
117
+ ///
118
+ /// If you want to clearly indicate that name that you resolve is an element
119
+ /// or an attribute name, you could use [`resolve_attribute()`] or [`resolve_element()`]
120
+ /// methods.
106
121
///
107
122
/// # Lifetimes
108
123
///
109
- /// - `'n`: lifetime of an element name
124
+ /// - `'n`: lifetime of a name. Returned local name will be bound to the same
125
+ /// lifetime as the name in question.
126
+ /// - returned namespace name will be bound to the reader itself
127
+ ///
128
+ /// [`Bound`]: ResolveResult::Bound
129
+ /// [`Unbound`]: ResolveResult::Unbound
130
+ /// [`Unknown`]: ResolveResult::Unknown
131
+ /// [`resolve_attribute()`]: Self::resolve_attribute()
132
+ /// [`resolve_element()`]: Self::resolve_element()
110
133
#[ inline]
111
- pub fn event_namespace < ' n > ( & self , name : QName < ' n > ) -> ( ResolveResult , LocalName < ' n > ) {
134
+ pub fn resolve < ' n > ( & self , name : QName < ' n > , attribute : bool ) -> ( ResolveResult , LocalName < ' n > ) {
135
+ self . ns_resolver . resolve ( name, & self . buffer , !attribute)
136
+ }
137
+
138
+ /// Resolves a potentially qualified **element name** into (namespace name, local name).
139
+ ///
140
+ /// *Qualified* element names have the form `prefix:local-name` where the
141
+ /// `prefix` is defined on any containing XML element via `xmlns:prefix="the:namespace:uri"`.
142
+ /// The namespace prefix can be defined on the same element as the element
143
+ /// in question.
144
+ ///
145
+ /// *Unqualified* elements inherits the current *default namespace*.
146
+ ///
147
+ /// The method returns following results depending on the `name` shape and
148
+ /// the presence of the default namespace:
149
+ ///
150
+ /// |`xmlns="..."`|QName |ResolveResult |LocalName
151
+ /// |-------------|-------------------|-----------------------|------------
152
+ /// |Not defined |`local-name` |[`Unbound`] |`local-name`
153
+ /// |Defined |`local-name` |[`Bound`] (default) |`local-name`
154
+ /// |_any_ |`prefix:local-name`|[`Bound`] / [`Unknown`]|`local-name`
155
+ ///
156
+ /// # Lifetimes
157
+ ///
158
+ /// - `'n`: lifetime of an element name. Returned local name will be bound
159
+ /// to the same lifetime as the name in question.
160
+ /// - returned namespace name will be bound to the reader itself
161
+ ///
162
+ /// # Examples
163
+ ///
164
+ /// This example shows how you can resolve qualified name into a namespace.
165
+ /// Note, that in the code like this you do not need to do that manually,
166
+ /// because the namespace resolution result returned by the [`read_event()`].
167
+ ///
168
+ /// ```
169
+ /// # use pretty_assertions::assert_eq;
170
+ /// use quick_xml::events::Event;
171
+ /// use quick_xml::name::{Namespace, QName, ResolveResult::*};
172
+ /// use quick_xml::NsReader;
173
+ ///
174
+ /// let mut reader = NsReader::from_str("<tag xmlns='root namespace'/>");
175
+ ///
176
+ /// match reader.read_event().unwrap() {
177
+ /// Event::Empty(e) => assert_eq!(
178
+ /// reader.resolve_element(e.name()),
179
+ /// (Bound(Namespace(b"root namespace")), QName(b"tag").into())
180
+ /// ),
181
+ /// _ => unreachable!(),
182
+ /// }
183
+ /// ```
184
+ ///
185
+ /// [`Bound`]: ResolveResult::Bound
186
+ /// [`Unbound`]: ResolveResult::Unbound
187
+ /// [`Unknown`]: ResolveResult::Unknown
188
+ /// [`read_event()`]: Self::read_event
189
+ #[ inline]
190
+ pub fn resolve_element < ' n > ( & self , name : QName < ' n > ) -> ( ResolveResult , LocalName < ' n > ) {
112
191
self . ns_resolver . resolve ( name, & self . buffer , true )
113
192
}
114
193
115
194
/// Resolves a potentially qualified **attribute name** into (namespace name, local name).
116
195
///
117
- /// *Qualified* attribute names have the form `prefix:local-name` where the`prefix` is defined
118
- /// on any containing XML element via `xmlns:prefix="the:namespace:uri"`. The namespace prefix
119
- /// can be defined on the same element as the attribute in question.
196
+ /// *Qualified* attribute names have the form `prefix:local-name` where the
197
+ /// `prefix` is defined on any containing XML element via `xmlns:prefix="the:namespace:uri"`.
198
+ /// The namespace prefix can be defined on the same element as the attribute
199
+ /// in question.
120
200
///
121
201
/// *Unqualified* attribute names do *not* inherit the current *default namespace*.
122
202
///
203
+ /// The method returns following results depending on the `name` shape and
204
+ /// the presence of the default namespace:
205
+ ///
206
+ /// |`xmlns="..."`|QName |ResolveResult |LocalName
207
+ /// |-------------|-------------------|-----------------------|------------
208
+ /// |Not defined |`local-name` |[`Unbound`] |`local-name`
209
+ /// |Defined |`local-name` |[`Unbound`] |`local-name`
210
+ /// |_any_ |`prefix:local-name`|[`Bound`] / [`Unknown`]|`local-name`
211
+ ///
123
212
/// # Lifetimes
124
213
///
125
- /// - `'n`: lifetime of an attribute
214
+ /// - `'n`: lifetime of an attribute name. Returned local name will be bound
215
+ /// to the same lifetime as the name in question.
216
+ /// - returned namespace name will be bound to the reader itself
217
+ ///
218
+ /// # Examples
219
+ ///
220
+ /// ```
221
+ /// # use pretty_assertions::assert_eq;
222
+ /// use quick_xml::events::Event;
223
+ /// use quick_xml::events::attributes::Attribute;
224
+ /// use quick_xml::name::{Namespace, QName, ResolveResult::*};
225
+ /// use quick_xml::NsReader;
226
+ ///
227
+ /// let mut reader = NsReader::from_str("
228
+ /// <tag one='1'
229
+ /// p:two='2'
230
+ /// xmlns='root namespace'
231
+ /// xmlns:p='other namespace'/>
232
+ /// ");
233
+ /// reader.trim_text(true);
234
+ ///
235
+ /// match reader.read_event().unwrap() {
236
+ /// Event::Empty(e) => {
237
+ /// let mut iter = e.attributes();
238
+ ///
239
+ /// // Unlike elements, attributes without explicit namespace
240
+ /// // not bound to any namespace
241
+ /// let one = iter.next().unwrap().unwrap();
242
+ /// assert_eq!(
243
+ /// reader.resolve_attribute(one.key),
244
+ /// (Unbound, QName(b"one").into())
245
+ /// );
246
+ ///
247
+ /// let two = iter.next().unwrap().unwrap();
248
+ /// assert_eq!(
249
+ /// reader.resolve_attribute(two.key),
250
+ /// (Bound(Namespace(b"other namespace")), QName(b"two").into())
251
+ /// );
252
+ /// }
253
+ /// _ => unreachable!(),
254
+ /// }
255
+ /// ```
256
+ ///
257
+ /// [`Bound`]: ResolveResult::Bound
258
+ /// [`Unbound`]: ResolveResult::Unbound
259
+ /// [`Unknown`]: ResolveResult::Unknown
126
260
#[ inline]
127
- pub fn attribute_namespace < ' n > ( & self , name : QName < ' n > ) -> ( ResolveResult , LocalName < ' n > ) {
261
+ pub fn resolve_attribute < ' n > ( & self , name : QName < ' n > ) -> ( ResolveResult , LocalName < ' n > ) {
128
262
self . ns_resolver . resolve ( name, & self . buffer , false )
129
263
}
130
264
}
@@ -133,7 +267,7 @@ impl<R: BufRead> NsReader<R> {
133
267
/// Reads the next event into given buffer.
134
268
///
135
269
/// This method manages namespaces but doesn't resolve them automatically.
136
- /// You should call [`event_namespace ()`] if you want to get a namespace.
270
+ /// You should call [`resolve_element ()`] if you want to get a namespace.
137
271
///
138
272
/// You also can use [`read_resolved_event_into()`] instead if you want to resolve
139
273
/// namespace as soon as you get an event.
@@ -161,7 +295,7 @@ impl<R: BufRead> NsReader<R> {
161
295
/// match reader.read_event_into(&mut buf).unwrap() {
162
296
/// Event::Start(e) => {
163
297
/// count += 1;
164
- /// let (ns, local) = reader.event_namespace (e.name());
298
+ /// let (ns, local) = reader.resolve_element (e.name());
165
299
/// match local.as_ref() {
166
300
/// b"tag1" => assert_eq!(ns, Bound(Namespace(b"www.xxxx"))),
167
301
/// b"tag2" => assert_eq!(ns, Bound(Namespace(b"www.yyyy"))),
@@ -180,7 +314,7 @@ impl<R: BufRead> NsReader<R> {
180
314
/// assert_eq!(txt, vec!["Test".to_string(), "Test 2".to_string()]);
181
315
/// ```
182
316
///
183
- /// [`event_namespace ()`]: Self::event_namespace
317
+ /// [`resolve_element ()`]: Self::resolve_element
184
318
/// [`read_resolved_event_into()`]: Self::read_resolved_event_into
185
319
#[ inline]
186
320
pub fn read_event_into < ' b > ( & mut self , buf : & ' b mut Vec < u8 > ) -> Result < Event < ' b > > {
@@ -268,7 +402,7 @@ impl<'i> NsReader<&'i [u8]> {
268
402
/// Reads the next event, borrow its content from the input buffer.
269
403
///
270
404
/// This method manages namespaces but doesn't resolve them automatically.
271
- /// You should call [`event_namespace ()`] if you want to get a namespace.
405
+ /// You should call [`resolve_element ()`] if you want to get a namespace.
272
406
///
273
407
/// You also can use [`read_resolved_event()`] instead if you want to resolve namespace
274
408
/// as soon as you get an event.
@@ -295,7 +429,7 @@ impl<'i> NsReader<&'i [u8]> {
295
429
/// match reader.read_event().unwrap() {
296
430
/// Event::Start(e) => {
297
431
/// count += 1;
298
- /// let (ns, local) = reader.event_namespace (e.name());
432
+ /// let (ns, local) = reader.resolve_element (e.name());
299
433
/// match local.as_ref() {
300
434
/// b"tag1" => assert_eq!(ns, Bound(Namespace(b"www.xxxx"))),
301
435
/// b"tag2" => assert_eq!(ns, Bound(Namespace(b"www.yyyy"))),
@@ -313,7 +447,7 @@ impl<'i> NsReader<&'i [u8]> {
313
447
/// assert_eq!(txt, vec!["Test".to_string(), "Test 2".to_string()]);
314
448
/// ```
315
449
///
316
- /// [`event_namespace ()`]: Self::event_namespace
450
+ /// [`resolve_element ()`]: Self::resolve_element
317
451
/// [`read_resolved_event()`]: Self::read_resolved_event
318
452
#[ inline]
319
453
pub fn read_event ( & mut self ) -> Result < Event < ' i > > {
0 commit comments