xsd-schema 0.1.0

XML Schema (XSD 1.0/1.1) validator with PSVI and a built-in XPath 2.0 engine
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
//! Streaming matcher for identity-constraint XPath expressions.
//!
//! `ActiveAxis` advances through a compiled `Asttree` as SAX-style element
//! events arrive, reporting matches without buffering the document.

#![allow(dead_code)]

use crate::ids::NameId;

use super::asttree::{AstStep, Asttree};

/// Per-depth matching state for a single path.
#[derive(Clone)]
struct MatchContext {
    /// Step indices waiting to match a child element at the next depth.
    active_steps: Vec<usize>,
    /// Step indices that completed (element match) or are pending attribute check
    /// at this depth. For complete matches the index points at the last `Child`
    /// step; for attribute-pending matches it points at the `Attribute` step.
    matched_here: Vec<usize>,
    /// Whether an attribute tail step is pending (e.g. `foo/@id`).
    awaiting_attribute: bool,
    /// Whether to re-inject `first_real_step` at every depth (`.//` descendant).
    try_from_start: bool,
}

/// State for a single path alternative within the `Asttree` union.
struct PathState {
    /// One context per element depth.
    context_stack: Vec<MatchContext>,
    /// First non-`SelfNode` step index.
    first_real_step: usize,
    /// Cached from [`AstPath::descendant`](super::asttree::AstPath::descendant).
    has_descendant_prefix: bool,
}

/// Streaming matcher that advances through a compiled identity-constraint XPath
/// as element open/close events arrive.
pub(crate) struct ActiveAxis {
    ast: Asttree,
    current_depth: i32,
    active: bool,
    path_states: Vec<PathState>,
    last_entered_match: bool,
    last_exited_match: bool,
    scope_match_flag: bool,
}

/// Advance past consecutive `SelfNode` steps, returning the index of the
/// first non-`SelfNode` step (or `steps.len()` if all remaining are `SelfNode`).
fn skip_self_nodes(steps: &[AstStep], mut idx: usize) -> usize {
    while idx < steps.len() && matches!(steps[idx], AstStep::SelfNode) {
        idx += 1;
    }
    idx
}

impl ActiveAxis {
    /// Create a new matcher from a compiled `Asttree`.
    pub(crate) fn new(ast: Asttree) -> Self {
        let path_states = ast
            .paths
            .iter()
            .map(|_| PathState {
                context_stack: Vec::new(),
                first_real_step: 0,
                has_descendant_prefix: false,
            })
            .collect();

        Self {
            ast,
            current_depth: -1,
            active: false,
            path_states,
            last_entered_match: false,
            last_exited_match: false,
            scope_match_flag: false,
        }
    }

    /// Initialize matching state for the scope element.
    ///
    /// Returns `true` if the expression is a bare `.` (immediate scope match).
    pub(crate) fn activate(&mut self) -> bool {
        self.active = true;
        self.current_depth = 0;
        self.scope_match_flag = false;
        self.last_entered_match = false;
        self.last_exited_match = false;

        for (i, path) in self.ast.paths.iter().enumerate() {
            let first_real = path
                .steps
                .iter()
                .position(|s| !matches!(s, AstStep::SelfNode))
                .unwrap_or(path.steps.len());

            let state = &mut self.path_states[i];
            state.first_real_step = first_real;
            state.has_descendant_prefix = path.descendant;
            state.context_stack.clear();

            if first_real >= path.steps.len() {
                // Bare "." — matches the scope element itself.
                self.scope_match_flag = true;
                state.context_stack.push(MatchContext {
                    active_steps: vec![],
                    matched_here: vec![],
                    awaiting_attribute: false,
                    try_from_start: false,
                });
            } else if matches!(path.steps[first_real], AstStep::Attribute(_)) && !path.descendant {
                // `./@attr` — SelfNode followed by an attribute step (non-descendant).
                // The attribute belongs to the scope element, so mark it
                // awaiting immediately (no `move_to_start_element` needed).
                state.context_stack.push(MatchContext {
                    active_steps: vec![],
                    matched_here: vec![first_real],
                    awaiting_attribute: true,
                    try_from_start: false,
                });
            } else {
                state.context_stack.push(MatchContext {
                    active_steps: vec![first_real],
                    matched_here: vec![],
                    awaiting_attribute: false,
                    try_from_start: path.descendant,
                });
            }
        }

        self.scope_match_flag
    }

    /// Advance depth tracking for an element opening inside a
    /// processContents="skip" wildcard subtree, without attempting any
    /// selector / field matches. Pushes an empty match context so descendant
    /// axes can't expand into the skipped content. Used by IC processing in
    /// XSD 1.1 to honour wildcard attribution: skipped elements are outside
    /// the schema's validation scope and must not contribute to keys/uniques
    /// or keyrefs (see wild101..103).
    pub(crate) fn move_to_skipped_element(&mut self) {
        if !self.active {
            return;
        }
        self.current_depth += 1;
        self.last_entered_match = false;
        for state in &mut self.path_states {
            state.context_stack.push(MatchContext {
                active_steps: Vec::new(),
                matched_here: Vec::new(),
                awaiting_attribute: false,
                try_from_start: false,
            });
        }
    }

    /// Advance matching on an element open event.
    ///
    /// Returns `true` if any path completed (or attribute-pending) a match at
    /// this element.
    pub(crate) fn move_to_start_element(&mut self, local_name: NameId, ns: NameId) -> bool {
        if !self.active {
            return false;
        }

        self.current_depth += 1;
        self.last_entered_match = false;

        for (path_idx, path) in self.ast.paths.iter().enumerate() {
            let state = &mut self.path_states[path_idx];

            // Get candidate steps from parent context.
            let parent_ctx = match state.context_stack.last() {
                Some(ctx) => ctx,
                None => {
                    // Should not happen while active, but be defensive.
                    state.context_stack.push(MatchContext {
                        active_steps: vec![],
                        matched_here: vec![],
                        awaiting_attribute: false,
                        try_from_start: state.has_descendant_prefix,
                    });
                    continue;
                }
            };

            let mut candidates: Vec<usize> = parent_ctx.active_steps.clone();

            // For descendant paths, also try from the first real step (dedup).
            if parent_ctx.try_from_start && !candidates.contains(&state.first_real_step) {
                candidates.push(state.first_real_step);
            }

            let mut new_active = Vec::new();
            let mut new_matched = Vec::new();
            let mut new_awaiting = false;

            for &s in &candidates {
                if s >= path.steps.len() {
                    continue;
                }
                match &path.steps[s] {
                    AstStep::SelfNode => {
                        // SelfNode transparently matches the current node.
                        // Advance to the next real step.
                        let next = skip_self_nodes(&path.steps, s + 1);
                        if next >= path.steps.len() {
                            new_matched.push(s);
                        } else {
                            match &path.steps[next] {
                                AstStep::Attribute(_) => {
                                    new_awaiting = true;
                                    new_matched.push(next);
                                }
                                _ => {
                                    if !new_active.contains(&next) {
                                        new_active.push(next);
                                    }
                                }
                            }
                        }
                    }
                    AstStep::Child(name_test) => {
                        if name_test.matches(ns, local_name) {
                            // Skip any trailing SelfNode steps after this match.
                            let next = skip_self_nodes(&path.steps, s + 1);
                            if next >= path.steps.len() {
                                // Complete element match — no more steps.
                                new_matched.push(s);
                            } else {
                                match &path.steps[next] {
                                    AstStep::Attribute(_) => {
                                        new_awaiting = true;
                                        new_matched.push(next);
                                    }
                                    _ => {
                                        new_active.push(next);
                                    }
                                }
                            }
                        }
                    }
                    AstStep::Attribute(_) => {
                        // Attribute steps are not matched against elements.
                    }
                }
            }

            if !new_matched.is_empty() {
                self.last_entered_match = true;
            }

            state.context_stack.push(MatchContext {
                active_steps: new_active,
                matched_here: new_matched,
                awaiting_attribute: new_awaiting,
                try_from_start: state.has_descendant_prefix,
            });
        }

        self.last_entered_match
    }

    /// Pop matching context on an element close event.
    ///
    /// Returns `true` if the element being closed had any matches.
    /// Deactivates when the scope element closes (depth goes below 0).
    pub(crate) fn end_element(&mut self) -> bool {
        if !self.active {
            return false;
        }

        self.last_exited_match = false;

        for state in &mut self.path_states {
            if let Some(ctx) = state.context_stack.pop() {
                // Only count element-complete matches.  Contexts with
                // `awaiting_attribute` hold unconsumed attribute steps —
                // those must not trigger `exited_match` because the
                // attribute was never actually present on the element.
                if !ctx.matched_here.is_empty() && !ctx.awaiting_attribute {
                    self.last_exited_match = true;
                }
            }
        }

        self.current_depth -= 1;
        if self.current_depth < 0 {
            // Exiting the scope element itself.
            if self.scope_match_flag {
                self.last_exited_match = true;
            }
            self.active = false;
        }

        self.last_exited_match
    }

    /// Check whether the given attribute matches a pending attribute step.
    ///
    /// Call this after `move_to_start_element` returned `true` when the
    /// compiled expression ends with an attribute axis (e.g. `foo/@id`).
    pub(crate) fn matches_attribute(&self, local_name: NameId, ns: NameId) -> bool {
        if !self.active {
            return false;
        }

        for (path_idx, path) in self.ast.paths.iter().enumerate() {
            let state = &self.path_states[path_idx];
            if let Some(ctx) = state.context_stack.last() {
                if ctx.awaiting_attribute {
                    for &step_idx in &ctx.matched_here {
                        if step_idx < path.steps.len() {
                            if let AstStep::Attribute(name_test) = &path.steps[step_idx] {
                                if name_test.matches(ns, local_name) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }

        false
    }

    /// Reset for reuse with a new scope element.
    pub(crate) fn reactivate(&mut self) {
        self.active = false;
        self.current_depth = -1;
        self.scope_match_flag = false;
        self.last_entered_match = false;
        self.last_exited_match = false;
        for state in &mut self.path_states {
            state.context_stack.clear();
        }
    }

    /// Whether the matcher is within the constraint scope.
    pub(crate) fn is_active(&self) -> bool {
        self.active
    }

    /// Whether the last `move_to_start_element` produced a match.
    pub(crate) fn entered_match(&self) -> bool {
        self.last_entered_match
    }

    /// Whether the last `end_element` left a matched scope.
    pub(crate) fn exited_match(&self) -> bool {
        self.last_exited_match
    }

    /// Whether the expression is a bare `.` that matches the scope element.
    pub(crate) fn scope_match(&self) -> bool {
        self.scope_match_flag
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::namespace::context::NamespaceContextSnapshot;
    use crate::namespace::table::{well_known, NameTable};
    use crate::schema::model::XsdVersion;
    use crate::validation::asttree::Asttree;

    fn compile_sel(xpath: &str, table: &NameTable) -> Asttree {
        let snap = NamespaceContextSnapshot::default();
        Asttree::compile_selector(xpath, &snap, table, None, None, None, XsdVersion::V1_0).unwrap()
    }

    fn compile_fld(xpath: &str, table: &NameTable) -> Asttree {
        let snap = NamespaceContextSnapshot::default();
        Asttree::compile_field(xpath, &snap, table, None, None, None, XsdVersion::V1_0).unwrap()
    }

    fn compile_sel_with_ns(
        xpath: &str,
        table: &NameTable,
        snap: &NamespaceContextSnapshot,
    ) -> Asttree {
        Asttree::compile_selector(xpath, snap, table, None, None, None, XsdVersion::V1_0).unwrap()
    }

    /// `foo/bar`: enter `<foo>` (no match), enter `<bar>` (match), exit both.
    #[test]
    fn simple_path() {
        let table = NameTable::new();
        let ast = compile_sel("foo/bar", &table);
        let mut axis = ActiveAxis::new(ast);

        let foo = table.add("foo");
        let bar = table.add("bar");

        assert!(!axis.activate());

        // Enter <foo> — partial match only
        assert!(!axis.move_to_start_element(foo, well_known::EMPTY));
        // Enter <bar> — complete match
        assert!(axis.move_to_start_element(bar, well_known::EMPTY));

        // Exit </bar> — exiting a matched element
        assert!(axis.end_element());
        assert!(axis.exited_match());
        // Exit </foo> — no match at this level
        assert!(!axis.end_element());
        // Exit scope
        axis.end_element();
        assert!(!axis.is_active());
    }

    /// `.//bar`: match at depth 1 and depth 3.
    #[test]
    fn descendant_any_depth() {
        let table = NameTable::new();
        let ast = compile_sel(".//bar", &table);
        let mut axis = ActiveAxis::new(ast);

        let a = table.add("a");
        let bar = table.add("bar");

        assert!(!axis.activate());

        // Match at depth 1
        assert!(axis.move_to_start_element(bar, well_known::EMPTY));
        axis.end_element();

        // Match at depth 3: <a><a><bar/>
        assert!(!axis.move_to_start_element(a, well_known::EMPTY));
        assert!(!axis.move_to_start_element(a, well_known::EMPTY));
        assert!(axis.move_to_start_element(bar, well_known::EMPTY));
        axis.end_element(); // </bar>
        axis.end_element(); // </a>
        axis.end_element(); // </a>
    }

    /// `.//a/b` with `<a><a><b/>`: nested `<a>` both produce step-1 candidates.
    #[test]
    fn overlapping_descendant() {
        let table = NameTable::new();
        let ast = compile_sel(".//a/b", &table);
        let mut axis = ActiveAxis::new(ast);

        let a = table.add("a");
        let b = table.add("b");

        assert!(!axis.activate());

        // <a> — partial match
        assert!(!axis.move_to_start_element(a, well_known::EMPTY));
        // <a> (nested) — starts a new a/b candidate via descendant
        assert!(!axis.move_to_start_element(a, well_known::EMPTY));
        // <b> — matches the inner <a><b> path
        assert!(axis.move_to_start_element(b, well_known::EMPTY));

        axis.end_element(); // </b>
        axis.end_element(); // </a>
        axis.end_element(); // </a>
    }

    /// `a|b|c`: each alternative matches independently.
    #[test]
    fn union_paths() {
        let table = NameTable::new();
        let ast = compile_sel("a|b|c", &table);
        let mut axis = ActiveAxis::new(ast);

        let a = table.add("a");
        let b = table.add("b");
        let c = table.add("c");
        let x = table.add("x");

        assert!(!axis.activate());

        assert!(axis.move_to_start_element(a, well_known::EMPTY));
        axis.end_element();

        assert!(axis.move_to_start_element(b, well_known::EMPTY));
        axis.end_element();

        assert!(axis.move_to_start_element(c, well_known::EMPTY));
        axis.end_element();

        assert!(!axis.move_to_start_element(x, well_known::EMPTY));
        axis.end_element();
    }

    /// `foo/@id`: element NOT final, `matches_attribute("id")` returns true.
    #[test]
    fn attribute_field() {
        let table = NameTable::new();
        let ast = compile_fld("foo/@id", &table);
        let mut axis = ActiveAxis::new(ast);

        let foo = table.add("foo");
        let id = table.add("id");
        let other = table.add("other");

        assert!(!axis.activate());

        // Enter <foo> — has pending attribute
        assert!(axis.move_to_start_element(foo, well_known::EMPTY));

        // Attribute @id matches
        assert!(axis.matches_attribute(id, well_known::EMPTY));
        // Attribute @other does not
        assert!(!axis.matches_attribute(other, well_known::EMPTY));

        axis.end_element();
    }

    /// `foo/@*`: any attribute matches after `<foo>`.
    #[test]
    fn attribute_wildcard() {
        let table = NameTable::new();
        let ast = compile_fld("foo/@*", &table);
        let mut axis = ActiveAxis::new(ast);

        let foo = table.add("foo");
        let anything = table.add("anything");
        let other = table.add("other");

        assert!(!axis.activate());

        assert!(axis.move_to_start_element(foo, well_known::EMPTY));

        // Any attribute matches @*
        assert!(axis.matches_attribute(anything, well_known::EMPTY));
        assert!(axis.matches_attribute(other, well_known::EMPTY));

        axis.end_element();
    }

    /// `.`: `activate()` returns true, `scope_match()` is true.
    #[test]
    fn self_selector() {
        let table = NameTable::new();
        let ast = compile_sel(".", &table);
        let mut axis = ActiveAxis::new(ast);

        assert!(axis.activate());
        assert!(axis.scope_match());
    }

    /// `./foo`: equivalent to `foo`, matches child.
    #[test]
    fn self_then_child() {
        let table = NameTable::new();
        let ast = compile_sel("./foo", &table);
        let mut axis = ActiveAxis::new(ast);

        let foo = table.add("foo");
        let bar = table.add("bar");

        assert!(!axis.activate());

        // ./foo is equivalent to foo
        assert!(axis.move_to_start_element(foo, well_known::EMPTY));
        axis.end_element();

        assert!(!axis.move_to_start_element(bar, well_known::EMPTY));
        axis.end_element();
    }

    /// Prefixed name: match only with correct namespace.
    #[test]
    fn namespace_match() {
        let table = NameTable::new();
        let ns = table.add("http://example.com");
        let prefix = table.add("p");
        let snap = NamespaceContextSnapshot {
            default_ns: None,
            bindings: vec![(prefix, ns)],
        };
        let ast = compile_sel_with_ns("p:foo", &table, &snap);
        let mut axis = ActiveAxis::new(ast);

        let foo = table.add("foo");
        let other_ns = table.add("http://other.com");

        assert!(!axis.activate());

        // Correct namespace — match
        assert!(axis.move_to_start_element(foo, ns));
        axis.end_element();

        // Wrong namespace — no match
        assert!(!axis.move_to_start_element(foo, other_ns));
        axis.end_element();

        // No namespace — no match
        assert!(!axis.move_to_start_element(foo, well_known::EMPTY));
        axis.end_element();
    }

    /// `*`: any element matches.
    #[test]
    fn wildcard_match() {
        let table = NameTable::new();
        let ast = compile_sel("*", &table);
        let mut axis = ActiveAxis::new(ast);

        let any_name = table.add("anything");
        let ns = table.add("http://example.com");

        assert!(!axis.activate());

        assert!(axis.move_to_start_element(any_name, well_known::EMPTY));
        axis.end_element();

        assert!(axis.move_to_start_element(any_name, ns));
        axis.end_element();
    }

    /// After exiting scope element, `is_active()` is false.
    #[test]
    fn scope_deactivation() {
        let table = NameTable::new();
        let ast = compile_sel("foo", &table);
        let mut axis = ActiveAxis::new(ast);

        let foo = table.add("foo");

        assert!(!axis.activate());
        assert!(axis.is_active());

        // Enter and exit a child
        assert!(axis.move_to_start_element(foo, well_known::EMPTY));
        axis.end_element();

        // Exit the scope element
        axis.end_element();
        assert!(!axis.is_active());
    }

    /// `a/b`: enter `<x><y><z>` — no matches.
    #[test]
    fn no_match_deep() {
        let table = NameTable::new();
        let ast = compile_sel("a/b", &table);
        let mut axis = ActiveAxis::new(ast);

        let x = table.add("x");
        let y = table.add("y");
        let z = table.add("z");

        assert!(!axis.activate());

        assert!(!axis.move_to_start_element(x, well_known::EMPTY));
        assert!(!axis.move_to_start_element(y, well_known::EMPTY));
        assert!(!axis.move_to_start_element(z, well_known::EMPTY));

        axis.end_element();
        axis.end_element();
        axis.end_element();
    }

    /// After deactivation, `reactivate()` + `activate()` gives fresh state.
    #[test]
    fn reactivate_reuse() {
        let table = NameTable::new();
        let ast = compile_sel("foo", &table);
        let mut axis = ActiveAxis::new(ast);

        let foo = table.add("foo");

        // First use
        assert!(!axis.activate());
        assert!(axis.move_to_start_element(foo, well_known::EMPTY));
        axis.end_element();
        axis.end_element(); // exit scope
        assert!(!axis.is_active());

        // Reactivate for a new scope
        axis.reactivate();
        assert!(!axis.is_active());

        assert!(!axis.activate());
        assert!(axis.is_active());

        // Should match again
        assert!(axis.move_to_start_element(foo, well_known::EMPTY));
        axis.end_element();
    }

    /// `foo/./bar`: mid-path SelfNode is transparent, equivalent to `foo/bar`.
    #[test]
    fn mid_path_self_node() {
        let table = NameTable::new();
        let ast = compile_sel("foo/./bar", &table);
        let mut axis = ActiveAxis::new(ast);

        let foo = table.add("foo");
        let bar = table.add("bar");

        assert!(!axis.activate());

        // Enter <foo> — partial match
        assert!(!axis.move_to_start_element(foo, well_known::EMPTY));
        // Enter <bar> — complete match (SelfNode skipped)
        assert!(axis.move_to_start_element(bar, well_known::EMPTY));

        axis.end_element(); // </bar>
        axis.end_element(); // </foo>
    }

    /// `foo/.`: trailing SelfNode, equivalent to `foo`.
    #[test]
    fn trailing_self_node() {
        let table = NameTable::new();
        let ast = compile_sel("foo/.", &table);
        let mut axis = ActiveAxis::new(ast);

        let foo = table.add("foo");

        assert!(!axis.activate());

        // Enter <foo> — complete match (trailing SelfNode consumed)
        assert!(axis.move_to_start_element(foo, well_known::EMPTY));

        axis.end_element();
    }

    /// `.`: exiting the scope element signals `exited_match`.
    #[test]
    fn self_selector_exit() {
        let table = NameTable::new();
        let ast = compile_sel(".", &table);
        let mut axis = ActiveAxis::new(ast);

        assert!(axis.activate());
        assert!(axis.scope_match());

        // Exit the scope element — should signal exit match
        assert!(axis.end_element());
        assert!(axis.exited_match());
        assert!(!axis.is_active());
    }

    /// `.//a`: matches `<a>` at multiple depths.
    #[test]
    fn descendant_single() {
        let table = NameTable::new();
        let ast = compile_sel(".//a", &table);
        let mut axis = ActiveAxis::new(ast);

        let a = table.add("a");
        let x = table.add("x");

        assert!(!axis.activate());

        // <a> at depth 1
        assert!(axis.move_to_start_element(a, well_known::EMPTY));
        // <a> at depth 2 (nested under first <a>)
        assert!(axis.move_to_start_element(a, well_known::EMPTY));
        axis.end_element(); // </a> inner
                            // <x> at depth 2 — no match
        assert!(!axis.move_to_start_element(x, well_known::EMPTY));
        axis.end_element(); // </x>
        axis.end_element(); // </a> outer
    }
}