xsd-parser-types 0.2.1

Types used by the code generated by xsd-parser
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
use std::borrow::Cow;
use std::fmt::{Debug, Formatter, Result as FmtResult};
use std::mem::replace;
use std::slice::Iter;
use std::str::from_utf8;

#[cfg(feature = "quick-xml")]
use quick_xml::{
    events::{attributes::Attribute, BytesEnd, BytesStart},
    name::QName,
};

use crate::misc::{format_utf8_slice, Namespace, NamespacePrefix};

#[cfg(feature = "quick-xml")]
use crate::quick_xml::{
    DeserializeHelper, Deserializer, DeserializerArtifact, DeserializerEvent, DeserializerOutput,
    DeserializerResult, Error, Event, SerializeHelper, Serializer, WithDeserializer,
    WithSerializer,
};

use super::{
    attributes::{Key as AttribKey, Value as AttribValue},
    Attributes, NamespacesShared, Value,
};

/// Represents a unstructured XML element.
#[derive(Default, Clone, Eq, PartialEq)]
pub struct Element<'a> {
    /// Name of the element.
    pub name: Cow<'a, [u8]>,

    /// Child values of this element.
    pub values: Vec<Value<'a>>,

    /// Attributes of this element.
    pub attributes: Attributes<'a>,

    /// List of valid namespaces for this element.
    pub namespaces: NamespacesShared<'a>,
}

/// Represents a list of unstructured XML elements.
pub type Elements<'a> = Vec<Element<'a>>;

/// Helper type for an element with static lifetime
pub type AnyElement = Element<'static>;

/// Helper type for elements with static lifetime
pub type AnyElements = Elements<'static>;

impl<'a> Element<'a> {
    /// Create a new [`Element`] instance.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Return the name of the element as [`QName`].
    #[must_use]
    #[cfg(feature = "quick-xml")]
    pub fn qname(&self) -> QName<'_> {
        QName(&self.name)
    }

    /// Set the name of the element.
    #[must_use]
    pub fn name<N>(mut self, name: N) -> Self
    where
        N: Into<Cow<'a, [u8]>>,
    {
        self.name = name.into();

        self
    }

    /// Add an attribute to the element.
    #[must_use]
    pub fn attribute<K, V>(mut self, name: K, value: V) -> Self
    where
        K: Into<Cow<'a, [u8]>>,
        V: Into<Cow<'a, [u8]>>,
    {
        self.attributes.insert(name, value);

        self
    }

    /// Add a child value to the element.
    #[must_use]
    pub fn child(mut self, value: Value<'a>) -> Self {
        self.values.push(value);

        self
    }

    /// Add a namespace to the namespace context of this element.
    ///
    /// This will not add a namespace attribute to the element itself. It only
    /// tells the serializer that this namespace must be valid in the context of
    /// this element. If the namespace is not already declared in a parent element,
    /// a suitable `xmlns` attribute will be added automatically.
    ///
    /// If you want to add a namespace declaration attribute to the element in
    /// any case, use the [`Element::attribute`] method instead.
    #[must_use]
    pub fn namespace<P, N>(mut self, prefix: P, namespace: N) -> Self
    where
        P: Into<Cow<'a, [u8]>>,
        N: Into<Cow<'a, [u8]>>,
    {
        let mut namespaces = self.namespaces.into_owned();
        namespaces.insert(prefix.into().into(), namespace.into().into());

        self.namespaces = namespaces.into_shared();

        self
    }
}

impl Debug for Element<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        struct Name<'a>(&'a [u8]);

        impl Debug for Name<'_> {
            fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
                write!(f, "\"")?;
                format_utf8_slice(self.0, f)?;
                write!(f, "\"")?;

                Ok(())
            }
        }

        f.debug_struct("Element")
            .field("name", &Name(&self.name))
            .field("values", &self.values)
            .field("attributes", &self.attributes)
            .field("namespaces", &self.namespaces)
            .finish()
    }
}

#[cfg(feature = "quick-xml")]
impl<'el> WithSerializer for Element<'el> {
    type Serializer<'x>
        = ElementSerializer<'x, 'el>
    where
        'el: 'x;

    fn serializer<'ser>(
        &'ser self,
        name: Option<&'ser str>,
        is_root: bool,
    ) -> Result<Self::Serializer<'ser>, Error> {
        let _is_root = is_root;

        Ok(ElementSerializer::new(self, name))
    }
}

#[cfg(feature = "quick-xml")]
impl WithDeserializer for Element<'static> {
    type Deserializer = ElementDeserializer;
}

#[derive(Debug)]
#[cfg(feature = "quick-xml")]
pub enum ElementSerializer<'ser, 'el> {
    Start {
        name: Option<&'ser str>,
        element: &'ser Element<'el>,
    },
    End {
        name: Option<&'ser str>,
        element: &'ser Element<'el>,
    },
    NextValue {
        name: Option<&'ser str>,
        element: &'ser Element<'el>,
        values: Iter<'ser, Value<'el>>,
    },
    SubElement {
        name: Option<&'ser str>,
        element: &'ser Element<'el>,
        values: Iter<'ser, Value<'el>>,
        serializer: Box<ElementSerializer<'ser, 'el>>,
    },
    Done,
}

#[cfg(feature = "quick-xml")]
impl<'ser, 'el> ElementSerializer<'ser, 'el> {
    fn new(element: &'ser Element<'el>, name: Option<&'ser str>) -> Self {
        Self::Start { name, element }
    }

    #[allow(clippy::too_many_lines)]
    fn next_item(&mut self, helper: &mut SerializeHelper) -> Result<Option<Event<'ser>>, Error> {
        loop {
            match replace(self, Self::Done) {
                Self::Start { name, element } => {
                    let element_name = name.map_or_else(|| from_utf8(&element.name), Ok)?;
                    let mut start = BytesStart::new(element_name);

                    helper.begin_ns_scope();
                    for (prefix, ns) in &**element.namespaces {
                        let ns = Namespace::new(ns.0.clone().into_owned());
                        let prefix = if prefix.0.is_empty() {
                            None
                        } else {
                            Some(NamespacePrefix::new(prefix.0.clone().into_owned()))
                        };
                        helper.write_xmlns(&mut start, prefix.as_ref(), &ns);
                    }

                    // The deserializer stores xmlns declarations in both
                    // `element.namespaces` and `element.attributes`. Skip xmlns
                    // attributes that already have a corresponding namespaces entry
                    // (emitted above via write_xmlns) to avoid duplicates.
                    let attributes = element
                        .attributes
                        .iter()
                        .filter(|(k, _)| {
                            if *k.0 == b"xmlns"[..] {
                                !element.namespaces.contains_key(&b""[..])
                            } else if let Some(prefix) = k.0.strip_prefix(b"xmlns:") {
                                !element.namespaces.contains_key(prefix)
                            } else {
                                true
                            }
                        })
                        .map(|(k, v)| Attribute {
                            key: QName(k),
                            value: Cow::Borrowed(&v.0),
                        });
                    start.extend_attributes(attributes);

                    let event = if element.values.is_empty() {
                        helper.end_ns_scope();

                        Event::Empty(start)
                    } else {
                        let values = element.values.iter();

                        *self = Self::NextValue {
                            name,
                            element,
                            values,
                        };

                        Event::Start(start)
                    };

                    return Ok(Some(event));
                }
                Self::End { name, element } => {
                    let element_name = name.map_or_else(|| from_utf8(&element.name), Ok)?;
                    let end = BytesEnd::new(element_name);
                    let event = Event::End(end);

                    helper.end_ns_scope();

                    return Ok(Some(event));
                }
                Self::NextValue {
                    name,
                    element,
                    mut values,
                } => match values.next() {
                    None => *self = Self::End { name, element },
                    Some(Value::Element(sub)) => {
                        let serializer = Box::new(Self::new(sub, None));

                        *self = Self::SubElement {
                            name,
                            element,
                            values,
                            serializer,
                        };
                    }
                    Some(Value::Comment(comment)) => {
                        *self = Self::NextValue {
                            name,
                            element,
                            values,
                        };

                        return Ok(Some(Event::Comment(comment.borrow())));
                    }
                    Some(Value::CData(cdata)) => {
                        *self = Self::NextValue {
                            name,
                            element,
                            values,
                        };

                        return Ok(Some(Event::CData(cdata.borrow())));
                    }
                    Some(Value::Text(text)) => {
                        *self = Self::NextValue {
                            name,
                            element,
                            values,
                        };

                        return Ok(Some(Event::Text(text.borrow())));
                    }
                },
                Self::SubElement {
                    name,
                    element,
                    values,
                    mut serializer,
                } => match serializer.next(helper) {
                    None => {
                        *self = Self::NextValue {
                            name,
                            element,
                            values,
                        }
                    }
                    Some(event) => {
                        *self = Self::SubElement {
                            name,
                            element,
                            values,
                            serializer,
                        };

                        return event.map(Some);
                    }
                },
                Self::Done => return Ok(None),
            }
        }
    }
}

#[cfg(feature = "quick-xml")]
impl<'ser> Serializer<'ser> for ElementSerializer<'ser, '_> {
    fn next(&mut self, helper: &mut SerializeHelper) -> Option<Result<Event<'ser>, Error>> {
        self.next_item(helper).transpose()
    }
}

#[derive(Debug)]
#[cfg(feature = "quick-xml")]
pub struct ElementDeserializer {
    element: Element<'static>,
    sub: Box<Option<ElementDeserializer>>,
}

#[cfg(feature = "quick-xml")]
impl ElementDeserializer {
    fn new(element: Element<'static>) -> Self {
        Self {
            element,
            sub: Box::new(None),
        }
    }

    fn create_element(
        start: &BytesStart<'_>,
        namespaces: NamespacesShared<'static>,
    ) -> Result<Element<'static>, Error> {
        let name = Cow::Owned(start.name().0.to_owned());
        let attributes = start
            .attributes()
            .map(|item| match item {
                Ok(Attribute { key, value }) => {
                    let key = Cow::Owned(key.0.to_owned());
                    let value = Cow::Owned(value.into_owned());

                    Ok((AttribKey(key), AttribValue(value)))
                }
                Err(error) => Err(error),
            })
            .collect::<Result<Attributes<'static>, _>>()?;

        Ok(Element {
            name,
            attributes,
            namespaces,
            values: Vec::new(),
        })
    }
}

#[cfg(feature = "quick-xml")]
impl<'de> Deserializer<'de, Element<'static>> for ElementDeserializer {
    fn init(
        helper: &mut DeserializeHelper,
        event: Event<'de>,
    ) -> DeserializerResult<'de, Element<'static>> {
        match event {
            Event::Start(start) => {
                let namespaces = helper.namespaces();
                let element = Self::create_element(&start, namespaces)?;
                let deserializer = Self::new(element);

                Ok(DeserializerOutput {
                    artifact: DeserializerArtifact::Deserializer(deserializer),
                    event: DeserializerEvent::None,
                    allow_any: true,
                })
            }
            Event::Empty(start) => {
                let namespaces = helper.namespaces();
                let element = Self::create_element(&start, namespaces)?;

                Ok(DeserializerOutput {
                    artifact: DeserializerArtifact::Data(element),
                    event: DeserializerEvent::None,
                    allow_any: true,
                })
            }
            event => Ok(DeserializerOutput {
                artifact: DeserializerArtifact::None,
                event: DeserializerEvent::Continue(event),
                allow_any: true,
            }),
        }
    }

    fn next(
        mut self,
        helper: &mut DeserializeHelper,
        event: Event<'de>,
    ) -> DeserializerResult<'de, Element<'static>> {
        macro_rules! handle_output {
            ($output:expr) => {{
                let output = $output;

                match output.artifact {
                    DeserializerArtifact::None => (),
                    DeserializerArtifact::Data(element) => {
                        let value = Value::Element(element);

                        self.element.values.push(value);
                    }
                    DeserializerArtifact::Deserializer(sub) => {
                        *self.sub = Some(sub);
                    }
                }

                match output.event {
                    DeserializerEvent::None => None,
                    DeserializerEvent::Break(event) => {
                        return Ok(DeserializerOutput {
                            artifact: DeserializerArtifact::Deserializer(self),
                            event: DeserializerEvent::Break(event),
                            allow_any: true,
                        })
                    }
                    DeserializerEvent::Continue(event) => Some(event),
                }
            }};
        }

        let event = if let Some(sub) = self.sub.take() {
            let output = sub.next(helper, event)?;

            handle_output!(output)
        } else {
            Some(event)
        };

        let event = match event {
            None => None,
            Some(event @ (Event::Start(_) | Event::Empty(_))) => {
                let output = Self::init(helper, event)?;

                handle_output!(output)
            }
            Some(Event::End(_)) => {
                return Ok(DeserializerOutput {
                    artifact: DeserializerArtifact::Data(self.element),
                    event: DeserializerEvent::None,
                    allow_any: true,
                })
            }
            Some(Event::Text(text)) => {
                let value = Value::Text(text.into_owned());

                self.element.values.push(value);

                None
            }
            Some(Event::CData(cdata)) => {
                let value = Value::CData(cdata.into_owned());

                self.element.values.push(value);

                None
            }
            Some(Event::Comment(comment)) => {
                let value = Value::Comment(comment.into_owned());

                self.element.values.push(value);

                None
            }
            event => event,
        };

        Ok(DeserializerOutput {
            artifact: DeserializerArtifact::Deserializer(self),
            event: event.map_or(DeserializerEvent::None, DeserializerEvent::Break),
            allow_any: true,
        })
    }

    #[allow(clippy::only_used_in_recursion)]
    fn finish(mut self, helper: &mut DeserializeHelper) -> Result<Element<'static>, Error> {
        if let Some(sub) = self.sub.take() {
            let element = sub.finish(helper)?;
            let value = Value::Element(element);

            self.element.values.push(value);
        }

        Ok(self.element)
    }
}

#[cfg(all(test, feature = "quick-xml"))]
mod tests {
    use std::str::from_utf8;
    use std::sync::Arc;

    use quick_xml::{events::BytesText, Writer};

    use crate::quick_xml::{DeserializeSync, SerializeSync, SliceReader};
    use crate::xml::Value;

    use super::Element;

    macro_rules! assert_entry {
        ($map:expr, $key:expr, $val:expr) => {
            assert_eq!($map.get(&$key[..]).unwrap().as_ref(), $val);
        };
    }

    macro_rules! assert_element {
        ($value:expr) => {
            if let Value::Element(element) = $value {
                element
            } else {
                panic!("Unexpected value")
            }
        };
    }

    macro_rules! assert_text {
        ($value:expr, $text:expr) => {
            assert!(matches!($value, Value::Text(x) if &**x == $text));
        };
    }

    #[test]
    fn serialize() {
        let mut root = Element::new();
        root.name = b"root".into();

        root.values.push(Value::Text(BytesText::new("\n    ")));

        let mut element = Element::new();
        element.name = b"name".into();
        element.attributes.insert(b"xmlns:ns", b"test");
        element.attributes.insert(b"ns:first", b"bob");
        element.attributes.insert(b"last", b"jones");
        root.values.push(Value::Element(element));

        root.values.push(Value::Text(BytesText::new("\n    ")));

        let mut element = Element::new();
        element.name = b"name".into();
        element.attributes.insert(b"first", b"elizabeth");
        element.attributes.insert(b"last", b"smith");
        root.values.push(Value::Element(element));

        root.values.push(Value::Text(BytesText::new("\n")));

        let mut buffer = Vec::new();
        let mut writer = Writer::new(&mut buffer);
        root.serialize("names", &mut writer).unwrap();

        let xml = from_utf8(&buffer).unwrap();
        assert_eq!(xml, XML.trim());
    }

    #[test]
    fn deserialize() {
        let mut reader = SliceReader::new(XML.trim());
        let root = Element::deserialize(&mut reader).unwrap();

        assert_eq!(root.name.as_ref(), b"names");
        assert_eq!(root.values.len(), 5);
        assert!(root.attributes.is_empty());
        assert!(root.namespaces.is_empty());

        let mut iter = root.values.iter();

        let value = iter.next().unwrap();
        assert_text!(value, b"\n    ");

        let value = iter.next().unwrap();
        let element = assert_element!(value);
        assert_eq!(element.name.as_ref(), b"name");
        assert!(element.values.is_empty());
        assert_eq!(element.attributes.len(), 3);
        assert_entry!(element.attributes, b"xmlns:ns", b"test");
        assert_entry!(element.attributes, b"ns:first", b"bob");
        assert_entry!(element.attributes, b"last", b"jones");
        assert_eq!(element.namespaces.len(), 1);
        assert_entry!(element.namespaces, b"ns", b"test");

        let value = iter.next().unwrap();
        assert_text!(value, b"\n    ");

        let value = iter.next().unwrap();
        let element = assert_element!(value);
        assert_eq!(element.name.as_ref(), b"name");
        assert!(element.values.is_empty());
        assert_eq!(element.attributes.len(), 2);
        assert_entry!(element.attributes, b"first", b"elizabeth");
        assert_entry!(element.attributes, b"last", b"smith");
        assert!(element.namespaces.is_empty());
        assert!(Arc::ptr_eq(&root.namespaces.0, &element.namespaces.0));

        let value = iter.next().unwrap();
        assert_text!(value, b"\n");
    }

    const XML: &str = r#"
<names>
    <name xmlns:ns="test" ns:first="bob" last="jones"/>
    <name first="elizabeth" last="smith"/>
</names>
"#;

    #[test]
    fn roundtrip_with_default_namespace() {
        const INPUT: &str = r#"<root xmlns="http://example.com/ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><child><value>hello</value></child></root>"#;

        let mut reader = SliceReader::new(INPUT);
        let doc1 = Element::deserialize(&mut reader).unwrap();

        let mut buffer = Vec::new();
        let mut writer = Writer::new(&mut buffer);
        doc1.serialize("root", &mut writer).unwrap();
        let output = from_utf8(&buffer).unwrap();

        assert!(
            !output.contains("xmlns:=\""),
            "must not produce invalid xmlns:= attribute, got: {output}"
        );
        assert_eq!(
            output.matches("xmlns=").count(),
            1,
            "default namespace xmlns= should appear exactly once, got: {output}"
        );
        assert_eq!(
            output.matches("xmlns:xsi=").count(),
            1,
            "xmlns:xsi= should appear exactly once, got: {output}"
        );

        let mut reader2 = SliceReader::new(output);
        let doc2 = Element::deserialize(&mut reader2).unwrap();
        assert_eq!(doc1, doc2, "round-trip should preserve element equality");
    }

    #[test]
    fn roundtrip_without_namespaces() {
        const INPUT: &str =
            r#"<root><child first="alice" last="jones"/><child first="bob"/></root>"#;

        let mut reader = SliceReader::new(INPUT);
        let doc1 = Element::deserialize(&mut reader).unwrap();

        let mut buffer = Vec::new();
        let mut writer = Writer::new(&mut buffer);
        doc1.serialize("root", &mut writer).unwrap();
        let output = from_utf8(&buffer).unwrap();

        let mut reader2 = SliceReader::new(output);
        let doc2 = Element::deserialize(&mut reader2).unwrap();
        assert_eq!(doc1, doc2);
    }

    #[test]
    fn roundtrip_with_prefixed_namespace_only() {
        const INPUT: &str =
            r#"<root xmlns:ns="http://example.com/ns"><ns:child>text</ns:child></root>"#;

        let mut reader = SliceReader::new(INPUT);
        let doc1 = Element::deserialize(&mut reader).unwrap();

        let mut buffer = Vec::new();
        let mut writer = Writer::new(&mut buffer);
        doc1.serialize("root", &mut writer).unwrap();
        let output = from_utf8(&buffer).unwrap();

        assert!(
            !output.contains("xmlns:=\""),
            "must not produce invalid xmlns:= attribute, got: {output}"
        );
        assert_eq!(
            output.matches("xmlns:ns=").count(),
            1,
            "xmlns:ns= should appear exactly once, got: {output}"
        );

        let mut reader2 = SliceReader::new(output);
        let doc2 = Element::deserialize(&mut reader2).unwrap();
        assert_eq!(doc1, doc2);
    }

    /// XML Namespaces 1.0 §6.2: `xmlns=""` undeclares the default namespace.
    #[test]
    fn roundtrip_default_namespace_undeclaration() {
        const INPUT: &str = r#"<root xmlns="http://example.com/ns"><child xmlns=""><value>plain</value></child></root>"#;

        let mut reader = SliceReader::new(INPUT);
        let doc1 = Element::deserialize(&mut reader).unwrap();

        let mut buffer = Vec::new();
        let mut writer = Writer::new(&mut buffer);
        doc1.serialize("root", &mut writer).unwrap();
        let output = from_utf8(&buffer).unwrap();

        assert!(
            !output.contains("xmlns:=\""),
            "must not produce invalid xmlns:= attribute, got: {output}"
        );

        let mut reader2 = SliceReader::new(output);
        let doc2 = Element::deserialize(&mut reader2).unwrap();
        assert_eq!(doc1, doc2);
    }

    /// XML Namespaces 1.0 §6.1: inner declaration shadows outer with same prefix.
    #[test]
    fn roundtrip_prefix_rebinding() {
        const INPUT: &str = r#"<root xmlns:ns="http://ns1"><ns:child xmlns:ns="http://ns2"><ns:value>text</ns:value></ns:child></root>"#;

        let mut reader = SliceReader::new(INPUT);
        let doc1 = Element::deserialize(&mut reader).unwrap();

        let mut buffer = Vec::new();
        let mut writer = Writer::new(&mut buffer);
        doc1.serialize("root", &mut writer).unwrap();
        let output = from_utf8(&buffer).unwrap();

        assert!(
            output.contains(r#"xmlns:ns="http://ns1""#),
            "parent ns binding must be present, got: {output}"
        );
        assert!(
            output.contains(r#"xmlns:ns="http://ns2""#),
            "child ns rebinding must be present, got: {output}"
        );

        let mut reader2 = SliceReader::new(output);
        let doc2 = Element::deserialize(&mut reader2).unwrap();
        assert_eq!(doc1, doc2);
    }

    /// Inherited namespaces must not be re-declared on child elements.
    #[test]
    fn roundtrip_inherited_namespace_not_redeclared() {
        const INPUT: &str = r#"<root xmlns:ns="http://example.com"><ns:child><ns:value>text</ns:value></ns:child></root>"#;

        let mut reader = SliceReader::new(INPUT);
        let doc1 = Element::deserialize(&mut reader).unwrap();

        let mut buffer = Vec::new();
        let mut writer = Writer::new(&mut buffer);
        doc1.serialize("root", &mut writer).unwrap();
        let output = from_utf8(&buffer).unwrap();

        assert_eq!(
            output.matches(r"xmlns:ns=").count(),
            1,
            "xmlns:ns= should appear only on root, not redeclared on children, got: {output}"
        );

        let mut reader2 = SliceReader::new(output);
        let doc2 = Element::deserialize(&mut reader2).unwrap();
        assert_eq!(doc1, doc2);
    }

    /// Xmlns declarations added only via `.attribute()` (without a
    /// corresponding `.namespace()` entry) must be emitted verbatim.
    #[test]
    fn attribute_only_xmlns_is_preserved() {
        let el = Element::new()
            .name(b"root".as_ref())
            .attribute(b"xmlns:foo".as_ref(), b"http://foo.example".as_ref())
            .attribute(b"foo:bar".as_ref(), b"value".as_ref());

        let mut buffer = Vec::new();
        let mut writer = Writer::new(&mut buffer);
        el.serialize("root", &mut writer).unwrap();
        let output = from_utf8(&buffer).unwrap();

        assert!(
            output.contains(r#"xmlns:foo="http://foo.example""#),
            "xmlns:foo must be preserved, got: {output}"
        );
        assert!(
            output.contains(r#"foo:bar="value""#),
            "foo:bar attribute must be preserved, got: {output}"
        );
    }
}