sup-xml-xslt 1.1.0

XSLT 1.0 transformation engine built on sup-xml's XPath core
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
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
//! ISO Schematron validator — native implementation.
//!
//! Schematron is a rule-based validation language defined by
//! ISO/IEC 19757-3.  A schema is a collection of patterns; each
//! pattern groups rules; each rule fires for nodes matching its
//! `context` (an XSLT match pattern); inside a rule, `<assert>`
//! and `<report>` carry XPath tests against the matched node.
//!
//! The canonical libxslt implementation runs the schema through a
//! 3-step XSLT pipeline (`iso_dsdl_include` →
//! `iso_abstract_expand` → `iso_svrl_for_xslt1`) and applies the
//! resulting stylesheet to the instance, then parses SVRL output.
//! We bypass the meta-XSLT pipeline by interpreting the schema
//! directly: every Schematron construct corresponds to a Rust
//! struct and the same XPath / pattern-matching machinery the
//! XSLT engine uses.
//!
//! Coverage:
//!
//! | Construct          | Status      |
//! |--------------------|-------------|
//! | `schema`           | implemented |
//! | `ns` (prefix decl) | implemented |
//! | `pattern`          | implemented |
//! | `rule` (`context`) | implemented |
//! | `assert` (`test`)  | implemented |
//! | `report` (`test`)  | implemented |
//! | inline `<value-of select="…"/>` in assert/report text | implemented |
//! | inline `<name path="…"/>` in assert/report text       | implemented |
//! | `let` / variable bindings        | implemented (top-level + rule-level) |
//! | `include` (schema/pattern/rule)  | implemented via [`Schematron::compile_str_with_loader`] |
//! | `abstract` patterns + `is-a` / `param` | implemented (XPath-source substitution) |
//! | `abstract` rules + `extends`     | implemented (rule-content splicing)    |
//! | `phase` / `active` filtering     | implemented via [`Schematron::validate_with_phase`] |
//! | `function` (Schematron QuickFix) | out of scope (Schematron 1.5+      |
//! |                                              with sch:fix-only-extension) |
//!
//! Both Schematron namespace URIs are accepted: the ISO version
//! (`http://purl.oclc.org/dsdl/schematron`) and the older 1.5
//! flavour (`http://www.ascc.net/xml/schematron`).

use std::collections::HashMap;

use sup_xml_core::xpath::eval::{
    eval_expr, EvalCtx, StaticContext, Value, XPathBindings,
};
use sup_xml_core::xpath::{parse_xpath, DocIndex, DocIndexLike, Expr, NodeId, XPathNodeKind};
use sup_xml_tree::dom::{Document, Node, NodeKind};

use crate::error::XsltError;

type Result<T> = std::result::Result<T, XsltError>;

pub const SCH_NS_ISO: &str = "http://purl.oclc.org/dsdl/schematron";
pub const SCH_NS_1_5: &str = "http://www.ascc.net/xml/schematron";

fn is_schematron_element(node: &Node) -> bool {
    if !node.is_element() { return false; }
    let uri = node.namespace.get().map(|ns| ns.href()).unwrap_or("");
    uri == SCH_NS_ISO || uri == SCH_NS_1_5
}

// ── compiled schema AST ───────────────────────────────────────────

/// A compiled Schematron schema, ready to validate instances.
#[derive(Debug)]
pub struct Schematron {
    /// Schema-level `<ns prefix="…" uri="…"/>` declarations.
    namespaces: HashMap<String, String>,
    /// Schema-level `<let name="…" value="…"/>` bindings.
    lets:       Vec<Let>,
    patterns:   Vec<Pattern>,
    /// `<sch:phase>` declarations, keyed by phase id.  Each phase
    /// names the subset of patterns active during a `phase`-scoped
    /// validation run.  Empty when the schema declares no phases
    /// — every validation runs all patterns in that case.
    phases:     HashMap<String, Vec<String>>,
    /// Value of `<schema defaultPhase="…">`, if present.  Used by
    /// [`Schematron::validate_with_phase`] when the caller passes
    /// `"#DEFAULT"`.
    default_phase: Option<String>,
}

#[derive(Debug)]
struct Pattern {
    id:    Option<String>,
    /// `<pattern name="…">` — captured for forward-compat with
    /// older Schematron 1.5 schemas that used `name` instead of
    /// `id`.  Not currently surfaced in `Finding`; available for
    /// callers via introspection.
    #[allow(dead_code)]
    name:  Option<String>,
    rules: Vec<Rule>,
}

#[derive(Debug)]
struct Rule {
    context: Expr,
    lets:    Vec<Let>,
    asserts: Vec<Assertion>,
}

#[derive(Debug)]
struct Let {
    name:  String,
    value: Expr,
}

#[derive(Debug)]
struct Assertion {
    kind:    AssertKind,
    test:    Expr,
    /// Mixed-content message — literal text plus inline
    /// `<value-of>`/`<name>` substitutions that consult the
    /// matched node.
    message: Vec<MessagePart>,
    id:      Option<String>,
    role:    Option<String>,
}

#[derive(Debug, Clone, Copy)]
enum AssertKind { Assert, Report }

#[derive(Debug)]
enum MessagePart {
    Text(String),
    /// `<value-of select="…"/>` — evaluate XPath and emit string.
    ValueOf(Expr),
    /// `<name path="…"/>` — XPath result's name (defaults to `.`).
    Name(Expr),
}

// ── compile ───────────────────────────────────────────────────────

impl Schematron {
    /// Compile a parsed Schematron schema document.  The document
    /// MUST have been parsed in namespace-aware mode — Schematron
    /// is namespace-driven (rule contexts and assertion tests use
    /// prefixes declared by `<sch:ns>` elements).
    ///
    /// Returns `XsltError::InvalidStylesheet` for structural
    /// problems (no `<schema>` root, missing `test=` on assert,
    /// etc.) — reusing `XsltError` keeps the public surface small.
    pub fn compile(schema_doc: &Document) -> Result<Self> {
        Self::compile_with_loader(schema_doc, &crate::loader::NullLoader, None)
    }

    /// Compile a Schematron schema, resolving any `<sch:include>`
    /// references via `loader`.  `base` is the URI of the containing
    /// schema (used by [`crate::loader::Loader::load`] for relative
    /// href resolution); pass `None` when relative resolution isn't
    /// needed.
    ///
    /// `<sch:include href="other.sch"/>` is supported at schema-,
    /// pattern-, and rule-level: the referenced document's content
    /// is spliced in as if it had been inlined at the include site.
    /// Fragment IDs (`href="other.sch#frag"`) are honoured — when
    /// present, the element in the loaded doc whose `id="frag"`
    /// supplies the spliced content; without a fragment, the loaded
    /// doc's root supplies it.
    pub fn compile_with_loader(
        schema_doc: &Document,
        loader:     &dyn crate::loader::Loader,
        base:       Option<&str>,
    ) -> Result<Self> {
        let root = schema_doc.root();
        if !is_schematron_element(root) || root.local_name() != "schema" {
            return Err(XsltError::InvalidStylesheet(
                "Schematron root element must be sch:schema".into(),
            ));
        }

        let mut s = Schematron {
            namespaces:    HashMap::new(),
            lets:          Vec::new(),
            patterns:      Vec::new(),
            phases:        HashMap::new(),
            default_phase: attr(root, "defaultPhase").map(|s| s.to_string()),
        };
        // First pass: index abstract patterns (referenced by
        // `<pattern is-a="…">`) and abstract rules (referenced by
        // `<sch:extends rule="…"/>`) by their `id=` attribute.  The
        // index borrows from `schema_doc`, which the caller owns
        // for the duration of this call.
        let mut abs_patterns: HashMap<String, &Node> = HashMap::new();
        let mut abs_rules:    HashMap<String, &Node> = HashMap::new();
        collect_abstract_patterns(root, &mut abs_patterns);
        collect_abstract_rules(root, &mut abs_rules);
        let abstracts = Abstracts { patterns: abs_patterns, rules: abs_rules };
        process_schema_children(root, loader, base, &abstracts, &mut s)?;
        // libxml2's `xmlSchematronParse` rejects a schema that defines no
        // rules at all — an empty `<schema/>` or one whose patterns carry
        // no `<rule>` — so a consumer (lxml) raises SchematronParseError
        // rather than silently accepting a no-op schema.  Abstract patterns
        // and rules (templates referenced via `is-a` / `extends`) count:
        // an abstract-only schema is well-formed even though its concrete
        // pattern/rule lists are empty.
        let has_concrete_rule = s.patterns.iter().any(|p| !p.rules.is_empty());
        let has_abstract = !abstracts.patterns.is_empty() || !abstracts.rules.is_empty();
        if !has_concrete_rule && !has_abstract {
            return Err(XsltError::InvalidStylesheet(
                "Schematron schema defines no rules".into(),
            ));
        }
        Ok(s)
    }

    /// Compile a Schematron schema directly from its text.
    /// Convenience wrapper.
    pub fn compile_str(text: &str) -> Result<Self> {
        let opts = sup_xml_core::ParseOptions {
            namespace_aware: true, ..Default::default()
        };
        let doc = sup_xml_core::parse_str(text, &opts).map_err(XsltError::from)?;
        Self::compile(&doc)
    }

    /// Compile a Schematron schema from its text, resolving any
    /// `<sch:include>` references via `loader`.  See
    /// [`Schematron::compile_with_loader`] for include semantics.
    pub fn compile_str_with_loader(
        text:   &str,
        loader: &dyn crate::loader::Loader,
        base:   Option<&str>,
    ) -> Result<Self> {
        let opts = sup_xml_core::ParseOptions {
            namespace_aware: true, ..Default::default()
        };
        let doc = sup_xml_core::parse_str(text, &opts).map_err(XsltError::from)?;
        Self::compile_with_loader(&doc, loader, base)
    }

    /// Compile via the official ISO Schematron XSLT pipeline:
    /// run the schema through `iso_dsdl_include.xsl` →
    /// `iso_abstract_expand.xsl` → `iso_svrl_for_xslt1.xsl`, then
    /// use the generated SVRL-emitting validator stylesheet as
    /// the actual validator.  Output: a [`Schematron`] whose
    /// `validate` method runs the validator stylesheet against
    /// the instance and parses the SVRL findings.
    ///
    /// `loader` resolves `xsl:import` references inside the
    /// pipeline stylesheets (`iso_svrl_for_xslt1.xsl` imports
    /// `iso_schematron_skeleton_for_xslt1.xsl`).  `base_dir` is
    /// where the pipeline xsl files live; use the host's local
    /// copy of lxml's `isoschematron/resources/xsl/iso-schematron-xslt1/`.
    ///
    /// Compared to [`Schematron::compile_str`] (our native
    /// implementation), the ISO pipeline understands `<abstract>`
    /// patterns, `<extends>`, and `<include>` references — at the
    /// cost of being slower and depending on those vendored XSLT
    /// files on disk.  Use compile_str for simple schemas; use
    /// `compile_iso` when you need full Schematron 1.6 features.
    pub fn compile_iso(
        schema_text: &str,
        base_dir:    &str,
        loader:      &dyn crate::loader::Loader,
    ) -> Result<IsoSchematronValidator> {
        let svrl_path = format!("{base_dir}/iso_svrl_for_xslt1.xsl");
        let svrl_text = loader.load(&svrl_path, None)?;
        let svrl = crate::Stylesheet::compile_str_with_loader(
            &svrl_text, loader, Some(&svrl_path))?;
        let opts = sup_xml_core::ParseOptions {
            namespace_aware: true, ..Default::default()
        };
        let schema_doc = sup_xml_core::parse_str(schema_text, &opts)
            .map_err(XsltError::from)?;
        let validator_xsl = svrl.apply(&schema_doc)?.to_string()?;
        let validator = crate::Stylesheet::compile_str_with_loader(
            &validator_xsl, loader, Some(&svrl_path))?;
        Ok(IsoSchematronValidator { validator })
    }
}

/// Validator produced by the ISO Schematron XSLT pipeline.
/// Applying it to an instance produces SVRL (Schematron
/// Validation Report Language) output — the structured
/// machine-readable validation report.
pub struct IsoSchematronValidator {
    validator: crate::Stylesheet,
}

impl IsoSchematronValidator {
    /// Validate `instance_doc` and return the raw SVRL output as
    /// a string.  Parsing SVRL into structured findings is the
    /// caller's job (or a future helper in this module).
    pub fn validate_to_svrl(
        &self,
        instance_doc: &sup_xml_tree::dom::Document,
    ) -> Result<String> {
        let result = self.validator.apply(instance_doc)?;
        Ok(result.to_string()?)
    }

    /// Validate an instance from source text.  Convenience wrapper.
    pub fn validate_str(&self, instance_text: &str) -> Result<String> {
        let opts = sup_xml_core::ParseOptions {
            namespace_aware: true, ..Default::default()
        };
        let doc = sup_xml_core::parse_str(instance_text, &opts)
            .map_err(XsltError::from)?;
        self.validate_to_svrl(&doc)
    }
}

/// Look-up table for `<pattern abstract="true">` and
/// `<rule abstract="true">` by id, populated in a pre-pass before
/// concrete patterns are compiled.  Lives as long as the source
/// schema Document.
struct Abstracts<'a> {
    patterns: HashMap<String, &'a Node<'a>>,
    rules:    HashMap<String, &'a Node<'a>>,
}

/// Param substitutions active during compilation of an abstract
/// pattern instantiation.  `None` outside abstract instantiation.
/// Maps `name` → raw XPath fragment to splice in at each `$name`
/// occurrence inside the abstract pattern's XPath strings.
type Params<'a> = Option<&'a HashMap<String, String>>;

/// Pre-pass that walks `parent` (typically `<schema>`) and any
/// `<pattern abstract="true">` / `<rule abstract="true">` it
/// contains, indexing them by `id`.  Schemas referenced via
/// `<include>` aren't visited here — top-level abstracts only.
fn collect_abstract_patterns<'a>(parent: &'a Node<'a>, out: &mut HashMap<String, &'a Node<'a>>) {
    walk_abstracts(parent, out, "pattern");
}

fn collect_abstract_rules<'a>(parent: &'a Node<'a>, out: &mut HashMap<String, &'a Node<'a>>) {
    walk_abstracts(parent, out, "rule");
}

fn walk_abstracts<'a>(
    parent: &'a Node<'a>,
    out:    &mut HashMap<String, &'a Node<'a>>,
    want:   &str,
) {
    for child in parent.children() {
        if !is_schematron_element(child) { continue; }
        let local = child.local_name();
        if local == want && attr(child, "abstract") == Some("true") {
            if let Some(id) = attr(child, "id") {
                out.insert(id.to_string(), child);
            }
        }
        // Recurse into <pattern> so abstract <rule>s inside concrete
        // patterns get picked up too.  Don't descend into abstract
        // patterns when looking for rules — those rules belong to
        // the abstract, not the schema's flat rule pool.
        if local == "pattern" && attr(child, "abstract") != Some("true") {
            walk_abstracts(child, out, want);
        }
    }
}

/// Replace `$NCName` references in an XPath source string with the
/// matching param's value.  Used for abstract-pattern instantiation
/// per ISO Schematron §6.4 — the `<param value="…">` text is
/// itself raw XPath, so this is plain textual substitution.
fn substitute_params(s: &str, params: Params) -> String {
    let Some(params) = params else { return s.to_string(); };
    if params.is_empty() { return s.to_string(); }
    let mut out = String::with_capacity(s.len());
    let mut iter = s.char_indices().peekable();
    while let Some((_, c)) = iter.next() {
        if c == '$' {
            if let Some(&(name_start, first)) = iter.peek() {
                if is_ncname_start(first) {
                    iter.next();
                    let mut name_end = name_start + first.len_utf8();
                    while let Some(&(j, c)) = iter.peek() {
                        if is_ncname_cont(c) {
                            iter.next();
                            name_end = j + c.len_utf8();
                        } else {
                            break;
                        }
                    }
                    let name = &s[name_start..name_end];
                    if let Some(v) = params.get(name) {
                        out.push_str(v);
                    } else {
                        out.push('$');
                        out.push_str(name);
                    }
                    continue;
                }
            }
            out.push('$');
        } else {
            out.push(c);
        }
    }
    out
}

fn is_ncname_start(c: char) -> bool {
    // ASCII-only NCNameStartChar — Schematron `<param>` names in
    // practice are ASCII; full XML 1.0 §4 production is overkill.
    matches!(c, 'a'..='z' | 'A'..='Z' | '_')
}

fn is_ncname_cont(c: char) -> bool {
    is_ncname_start(c) || c.is_ascii_digit() || c == '-' || c == '.'
}

/// Parse `s` as XPath after applying abstract-pattern param
/// substitution (if any).
fn parse_xpath_with_params(s: &str, params: Params) -> Result<Expr> {
    parse_xpath(&substitute_params(s, params)).map_err(XsltError::from)
}

/// Walk a `<schema>` element's children, collecting `<ns>`,
/// `<let>`, `<pattern>` declarations into `out`.  Recurses through
/// any `<include>` elements found at this level.
fn process_schema_children<'a, 'b>(
    parent:    &'b Node<'b>,
    loader:    &dyn crate::loader::Loader,
    base:      Option<&str>,
    abstracts: &Abstracts<'a>,
    out:       &mut Schematron,
) -> Result<()> {
    for child in parent.children() {
        if !is_schematron_element(child) { continue; }
        match child.local_name() {
            "ns" => {
                if let (Some(p), Some(u)) = (
                    attr(child, "prefix"), attr(child, "uri"),
                ) {
                    out.namespaces.insert(p.to_string(), u.to_string());
                }
            }
            "let"     => out.lets.push(compile_let(child, None)?),
            "pattern" => {
                // Skip `<pattern abstract="true">` — already
                // indexed via [`collect_abstract_patterns`]; it's
                // a template, not a runnable pattern.
                if attr(child, "abstract") == Some("true") { continue; }
                if let Some(abs_id) = attr(child, "is-a") {
                    out.patterns.push(instantiate_abstract_pattern(
                        child, abs_id, loader, base, abstracts,
                    )?);
                } else {
                    out.patterns.push(compile_pattern(child, loader, base, abstracts, None)?);
                }
            }
            "phase" => {
                if let Some(id) = attr(child, "id") {
                    let mut active = Vec::new();
                    for c in child.children() {
                        if is_schematron_element(c)
                            && c.local_name() == "active"
                        {
                            if let Some(p) = attr(c, "pattern") {
                                active.push(p.to_string());
                            }
                        }
                    }
                    out.phases.insert(id.to_string(), active);
                }
            }
            "include" => {
                let (doc, target_lookup, sub_base) = load_include(child, loader, base)?;
                let target = locate_include_target(doc.root(), target_lookup.as_deref())?;
                // The included element can be either another `<schema>`
                // (top-level include) or a `<pattern>`/`<let>`/`<ns>`
                // directly (fragment include).  Handle both shapes.
                if target.local_name() == "schema" && is_schematron_element(target) {
                    process_schema_children(target, loader, sub_base.as_deref(), abstracts, out)?;
                } else {
                    process_schema_one(target, loader, sub_base.as_deref(), abstracts, out)?;
                }
            }
            // <title>, <p>, <diagnostics>, <fix> — documentary.
            _ => {}
        }
    }
    Ok(())
}

/// Treat a single included element as a top-level schema child.
/// Mirrors the `match` inside [`process_schema_children`] but for
/// the case where the fragment IS the declaration (not its parent).
fn process_schema_one<'a, 'b>(
    node:      &'b Node<'b>,
    loader:    &dyn crate::loader::Loader,
    base:      Option<&str>,
    abstracts: &Abstracts<'a>,
    out:       &mut Schematron,
) -> Result<()> {
    if !is_schematron_element(node) { return Ok(()); }
    match node.local_name() {
        "ns" => {
            if let (Some(p), Some(u)) = (attr(node, "prefix"), attr(node, "uri")) {
                out.namespaces.insert(p.to_string(), u.to_string());
            }
        }
        "let"     => out.lets.push(compile_let(node, None)?),
        "pattern" => {
            if attr(node, "abstract") == Some("true") { return Ok(()); }
            if let Some(abs_id) = attr(node, "is-a") {
                out.patterns.push(instantiate_abstract_pattern(
                    node, abs_id, loader, base, abstracts,
                )?);
            } else {
                out.patterns.push(compile_pattern(node, loader, base, abstracts, None)?);
            }
        }
        _ => {} // Other fragment kinds aren't valid as schema-level content.
    }
    Ok(())
}

/// Instantiate an abstract pattern via `<pattern is-a="X">`.  Looks
/// up the abstract pattern Node by id, gathers `<param>` children
/// into a substitution map, and compiles each rule with those
/// substitutions applied to its XPath strings.
fn instantiate_abstract_pattern<'a, 'b>(
    instance:  &'b Node<'b>,
    abs_id:    &str,
    loader:    &dyn crate::loader::Loader,
    base:      Option<&str>,
    abstracts: &Abstracts<'a>,
) -> Result<Pattern> {
    let abstract_node = abstracts.patterns.get(abs_id).ok_or_else(|| {
        XsltError::UnresolvedReference(format!(
            "<pattern is-a='{abs_id}'> references an abstract pattern that wasn't declared"
        ))
    })?;
    // Collect <param name=… value=…> children from the instance.
    let mut params: HashMap<String, String> = HashMap::new();
    for c in instance.children() {
        if !is_schematron_element(c) || c.local_name() != "param" { continue; }
        let n = attr(c, "name").ok_or_else(|| XsltError::InvalidStylesheet(
            "sch:param requires a name= attribute".into(),
        ))?;
        let v = attr(c, "value").ok_or_else(|| XsltError::InvalidStylesheet(
            "sch:param requires a value= attribute".into(),
        ))?;
        params.insert(n.to_string(), v.to_string());
    }
    // Compile the abstract pattern's rules with the substitution map.
    let id   = attr(instance, "id").map(|s| s.to_string());
    let name = attr(instance, "name").map(|s| s.to_string());
    let mut rules = Vec::new();
    process_pattern_children(abstract_node, loader, base, abstracts, Some(&params), &mut rules)?;
    Ok(Pattern { id, name, rules })
}

fn compile_pattern<'a, 'b>(
    node:      &'b Node<'b>,
    loader:    &dyn crate::loader::Loader,
    base:      Option<&str>,
    abstracts: &Abstracts<'a>,
    params:    Params,
) -> Result<Pattern> {
    let id   = attr(node, "id").map(|s| s.to_string());
    let name = attr(node, "name").map(|s| s.to_string());
    let mut rules = Vec::new();
    process_pattern_children(node, loader, base, abstracts, params, &mut rules)?;
    Ok(Pattern { id, name, rules })
}

fn process_pattern_children<'a, 'b>(
    parent:    &'b Node<'b>,
    loader:    &dyn crate::loader::Loader,
    base:      Option<&str>,
    abstracts: &Abstracts<'a>,
    params:    Params,
    rules:     &mut Vec<Rule>,
) -> Result<()> {
    for child in parent.children() {
        if !is_schematron_element(child) { continue; }
        match child.local_name() {
            "rule" => {
                // Skip abstract rules — they're templates that
                // contribute via <sch:extends>, not directly.
                if attr(child, "abstract") == Some("true") { continue; }
                rules.push(compile_rule(child, loader, base, abstracts, params)?);
            }
            "include" => {
                let (doc, target_lookup, sub_base) = load_include(child, loader, base)?;
                let target = locate_include_target(doc.root(), target_lookup.as_deref())?;
                // Two shapes: included is a <pattern> (use its rules)
                // or a <rule> directly (single-fragment include).
                match (is_schematron_element(target), target.local_name()) {
                    (true, "pattern") => process_pattern_children(target, loader, sub_base.as_deref(), abstracts, params, rules)?,
                    (true, "rule")    => rules.push(compile_rule(target, loader, sub_base.as_deref(), abstracts, params)?),
                    _ => return Err(XsltError::InvalidStylesheet(format!(
                        "sch:include inside <pattern> must point to a <pattern> or <rule>; got <{}>",
                        target.name(),
                    ))),
                }
            }
            _ => {}
        }
    }
    Ok(())
}

fn compile_rule<'a, 'b>(
    node:      &'b Node<'b>,
    loader:    &dyn crate::loader::Loader,
    base:      Option<&str>,
    abstracts: &Abstracts<'a>,
    params:    Params,
) -> Result<Rule> {
    let ctx = attr(node, "context").ok_or_else(|| XsltError::InvalidStylesheet(
        "sch:rule requires a context= attribute".into(),
    ))?;
    let context = parse_xpath_with_params(ctx, params)?;
    let mut lets    = Vec::new();
    let mut asserts = Vec::new();
    process_rule_children(node, loader, base, abstracts, params, &mut lets, &mut asserts)?;
    Ok(Rule { context, lets, asserts })
}

fn process_rule_children<'a, 'b>(
    parent:    &'b Node<'b>,
    loader:    &dyn crate::loader::Loader,
    base:      Option<&str>,
    abstracts: &Abstracts<'a>,
    params:    Params,
    lets:      &mut Vec<Let>,
    asserts:   &mut Vec<Assertion>,
) -> Result<()> {
    for child in parent.children() {
        if !is_schematron_element(child) { continue; }
        match child.local_name() {
            "let"    => lets.push(compile_let(child, params)?),
            "assert" => asserts.push(compile_assertion(child, AssertKind::Assert, params)?),
            "report" => asserts.push(compile_assertion(child, AssertKind::Report, params)?),
            "extends" => {
                // ISO §6.3: `<sch:extends rule="abstract-rule-id"/>`
                // splices the abstract rule's lets/asserts/reports
                // in here.  The abstract rule was indexed in the
                // pre-pass via [`collect_abstract_rules`].
                let rule_id = attr(child, "rule").ok_or_else(|| XsltError::InvalidStylesheet(
                    "sch:extends requires a rule= attribute".into(),
                ))?;
                let abstract_rule = abstracts.rules.get(rule_id).ok_or_else(||
                    XsltError::UnresolvedReference(format!(
                        "<extends rule='{rule_id}'> references an abstract rule that wasn't declared"
                    ))
                )?;
                process_rule_children(abstract_rule, loader, base, abstracts, params, lets, asserts)?;
            }
            "include" => {
                let (doc, target_lookup, sub_base) = load_include(child, loader, base)?;
                let target = locate_include_target(doc.root(), target_lookup.as_deref())?;
                // Two shapes: included is a <rule> (splice its
                // contents) or a single <assert>/<report>/<let>
                // (treat as the lone declaration at this site).
                match (is_schematron_element(target), target.local_name()) {
                    (true, "rule") => process_rule_children(target, loader, sub_base.as_deref(), abstracts, params, lets, asserts)?,
                    (true, "assert") => asserts.push(compile_assertion(target, AssertKind::Assert, params)?),
                    (true, "report") => asserts.push(compile_assertion(target, AssertKind::Report, params)?),
                    (true, "let")    => lets.push(compile_let(target, params)?),
                    _ => return Err(XsltError::InvalidStylesheet(format!(
                        "sch:include inside <rule> must point to <rule>/<assert>/<report>/<let>; got <{}>",
                        target.name(),
                    ))),
                }
            }
            _ => {}
        }
    }
    Ok(())
}

/// Resolve a `<sch:include href=…>` element: load the referenced
/// document via `loader`, parse it, and return `(doc, fragment_id,
/// resolved_base)`.  Caller uses [`locate_include_target`] to find
/// the matching element inside `doc`.
fn load_include(
    node:   &Node,
    loader: &dyn crate::loader::Loader,
    base:   Option<&str>,
) -> Result<(Document, Option<String>, Option<String>)> {
    let href = attr(node, "href").ok_or_else(|| XsltError::InvalidStylesheet(
        "sch:include requires an href= attribute".into(),
    ))?;
    // Split off `#fragment` if present — Schematron uses the host
    // doc + element id to locate the specific declaration.
    let (path, fragment) = match href.find('#') {
        Some(i) => (&href[..i], Some(href[i+1..].to_string())),
        None    => (href, None),
    };
    let text = loader.load(path, base)?;
    let opts = sup_xml_core::ParseOptions {
        namespace_aware: true, ..Default::default()
    };
    let doc = sup_xml_core::parse_str(&text, &opts).map_err(XsltError::from)?;
    let resolved_base = loader.resolve(path, base).ok();
    Ok((doc, fragment, resolved_base))
}

/// Given the root of an included document and an optional fragment
/// id, find the element to splice in.  Without a fragment, returns
/// the root itself; with a fragment, searches the tree for an
/// element whose `id=` or `xml:id=` matches.
fn locate_include_target<'a>(
    root:     &'a Node<'a>,
    fragment: Option<&str>,
) -> Result<&'a Node<'a>> {
    let Some(id) = fragment else { return Ok(root); };
    find_by_id(root, id).ok_or_else(|| XsltError::InvalidStylesheet(format!(
        "sch:include fragment '#{id}' not found in loaded document"
    )))
}

fn find_by_id<'a>(node: &'a Node<'a>, id: &str) -> Option<&'a Node<'a>> {
    if node.is_element() {
        if attr(node, "id") == Some(id) {
            return Some(node);
        }
        // `xml:id` per https://www.w3.org/TR/xml-id/ — also honoured.
        for a in node.attributes() {
            if a.local_name() == "id"
               && a.namespace.get().and_then(|n| n.prefix()) == Some("xml") && a.value() == id {
                return Some(node);
            }
        }
        for c in node.children() {
            if let Some(hit) = find_by_id(c, id) { return Some(hit); }
        }
    }
    None
}

fn compile_let(node: &Node, params: Params) -> Result<Let> {
    let name  = attr(node, "name").ok_or_else(|| XsltError::InvalidStylesheet(
        "sch:let requires a name= attribute".into(),
    ))?.to_string();
    let value = attr(node, "value").ok_or_else(|| XsltError::InvalidStylesheet(
        "sch:let requires a value= attribute".into(),
    ))?;
    Ok(Let { name, value: parse_xpath_with_params(value, params)? })
}

fn compile_assertion(node: &Node, kind: AssertKind, params: Params) -> Result<Assertion> {
    let test = attr(node, "test").ok_or_else(|| XsltError::InvalidStylesheet(
        format!("sch:{} requires a test= attribute",
            if matches!(kind, AssertKind::Assert) { "assert" } else { "report" }),
    ))?;
    let test = parse_xpath_with_params(test, params)?;
    Ok(Assertion {
        kind,
        test,
        message: compile_message(node, params)?,
        id:   attr(node, "id").map(str::to_string),
        role: attr(node, "role").map(str::to_string),
    })
}

fn compile_message(node: &Node, params: Params) -> Result<Vec<MessagePart>> {
    let mut parts = Vec::new();
    for child in node.children() {
        match child.kind {
            NodeKind::Text | NodeKind::CData => {
                parts.push(MessagePart::Text(child.content().to_string()));
            }
            NodeKind::Element if is_schematron_element(child) => {
                match child.local_name() {
                    "value-of" => {
                        let sel = attr(child, "select").ok_or_else(||
                            XsltError::InvalidStylesheet(
                                "sch:value-of requires select=".into()))?;
                        parts.push(MessagePart::ValueOf(
                            parse_xpath_with_params(sel, params)?));
                    }
                    "name" => {
                        let path = attr(child, "path").unwrap_or(".");
                        parts.push(MessagePart::Name(
                            parse_xpath_with_params(path, params)?));
                    }
                    _ => {
                        // Other inline elements (sch:emph, sch:span):
                        // stringify their text content.
                        parts.push(MessagePart::Text(child_text(child)));
                    }
                }
            }
            _ => {}
        }
    }
    Ok(parts)
}

fn child_text(node: &Node) -> String {
    let mut s = String::new();
    for c in node.children() {
        if matches!(c.kind, NodeKind::Text | NodeKind::CData) {
            s.push_str(c.content());
        }
    }
    s
}

fn attr<'a>(node: &'a Node, name: &str) -> Option<&'a str> {
    for a in node.attributes() {
        if a.name() == name && !a.name().contains(':') {
            return Some(a.value());
        }
    }
    None
}

// ── validation ────────────────────────────────────────────────────

/// One finding from a Schematron validation run.
#[derive(Debug, Clone)]
pub struct Finding {
    pub kind:        FindingKind,
    pub message:     String,
    /// `pattern.id` of the pattern that fired.  `None` for
    /// patterns with no id.
    pub pattern_id:  Option<String>,
    pub assertion_id: Option<String>,
    pub role:        Option<String>,
    /// `generate-id()`-style stable id for the node where the
    /// rule fired.
    pub location_id: String,
    /// Local-name of the offending node — handy for diagnostic
    /// messages.
    pub context_name: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FindingKind {
    /// An `<assert>` test evaluated to false.
    FailedAssert,
    /// A `<report>` test evaluated to true.
    SuccessfulReport,
}

/// Result of a validation run.  `valid()` is the common gate
/// callers reach for; the full `findings` vec carries every
/// failed-assert and successful-report for richer reporting.
#[derive(Debug, Clone, Default)]
pub struct ValidationReport {
    pub findings: Vec<Finding>,
}

impl ValidationReport {
    /// A document is *valid* if no assertions failed.  Successful
    /// reports are diagnostic (they fire when something noteworthy
    /// happened, not when something's wrong), so they don't make
    /// a document invalid.
    pub fn valid(&self) -> bool {
        !self.findings.iter().any(|f| matches!(f.kind, FindingKind::FailedAssert))
    }
}

/// Bridge Schematron's namespace + let bindings into the XPath
/// engine.  Each rule-evaluation builds one of these against the
/// current variable scope.
struct SchBindings<'a> {
    namespaces: &'a HashMap<String, String>,
    vars:       &'a HashMap<String, Value>,
}

/// Static XPath context for a Schematron expression — seeded from the
/// bindings' host config (XPath version + regex dialect) so eval
/// observes the same distinctions Schematron declares.
fn sch_static_ctx(bindings: &SchBindings<'_>) -> StaticContext {
    StaticContext {
        xpath_2_0: bindings.xpath_version_2_or_later(),
        xpath_3_0: false,
        libxml2_compatible: false, current_node: None,
    }
}

impl XPathBindings for SchBindings<'_> {
    fn resolve_prefix(&self, prefix: &str) -> Option<String> {
        self.namespaces.get(prefix).cloned()
    }
    fn variable(&self, name: &str) -> Option<Value> {
        self.vars.get(name).cloned()
    }
}

impl Schematron {
    /// Validate an instance from source text.  Convenience wrapper
    /// that parses with `namespace_aware: true` — required for
    /// Schematron's prefix-based rule contexts to resolve.
    pub fn validate_str(&self, instance_text: &str) -> Result<ValidationReport> {
        self.validate_str_with_phase(instance_text, "#ALL")
    }

    /// Validate an instance from source text, running only the
    /// patterns active under `phase`.  See [`Schematron::validate_with_phase`]
    /// for phase semantics.
    pub fn validate_str_with_phase(
        &self, instance_text: &str, phase: &str,
    ) -> Result<ValidationReport> {
        let opts = sup_xml_core::ParseOptions {
            namespace_aware: true, ..Default::default()
        };
        let doc = sup_xml_core::parse_str(instance_text, &opts).map_err(XsltError::from)?;
        self.validate_with_phase(&doc, phase)
    }

    /// Validate `instance_doc` against this schema.  Returns a
    /// [`ValidationReport`] with every failed assert and
    /// successful report.  The instance document MUST have been
    /// parsed in namespace-aware mode if any rule contexts or
    /// assertion tests use prefixed names; use
    /// [`Schematron::validate_str`] for the common case.
    pub fn validate(&self, instance_doc: &Document) -> Result<ValidationReport> {
        self.validate_with_phase(instance_doc, "#ALL")
    }

    /// Validate `instance_doc`, running only the patterns active
    /// under `phase`.  ISO Schematron §6.5 — phases let a schema
    /// expose multiple validation profiles from one declaration set.
    ///
    /// Special phase names:
    /// * `"#ALL"` — run every pattern (the no-phase default).
    /// * `"#DEFAULT"` — run the phase named by `<schema defaultPhase=…>`;
    ///   if the schema has no `defaultPhase`, falls back to `#ALL`.
    ///
    /// Any other name is looked up in the schema's `<sch:phase>`
    /// declarations.  An unknown name (one that isn't `#ALL`, isn't
    /// `#DEFAULT`, and doesn't match a declared phase id) returns
    /// `Err(InvalidStylesheet)` so callers see the typo instead of
    /// silently validating against zero patterns.
    pub fn validate_with_phase(
        &self, instance_doc: &Document, phase: &str,
    ) -> Result<ValidationReport> {
        let active: Option<&[String]> = match phase {
            "#ALL" => None,
            "#DEFAULT" => match self.default_phase.as_deref() {
                None | Some("#ALL") => None,
                Some(name) => Some(self.phases.get(name)
                    .ok_or_else(|| XsltError::InvalidStylesheet(format!(
                        "schema defaultPhase='{name}' but no <sch:phase id='{name}'> declared"
                    )))?
                    .as_slice()),
            },
            other => Some(self.phases.get(other)
                .ok_or_else(|| XsltError::InvalidStylesheet(format!(
                    "unknown phase '{other}': no matching <sch:phase id='{other}'> in schema"
                )))?
                .as_slice()),
        };
        self.validate_inner(instance_doc, active)
    }

    fn validate_inner(
        &self, instance_doc: &Document, active: Option<&[String]>,
    ) -> Result<ValidationReport> {
        let idx = DocIndex::build(instance_doc);
        let mut report = ValidationReport::default();

        // Pre-evaluate schema-level `<let>` bindings.  XPath
        // context is the document root.  Each let sees the
        // previously-bound lets — we construct fresh bindings per
        // iteration so the borrow on `schema_vars` is released
        // before insert.
        let mut schema_vars: HashMap<String, Value> = HashMap::new();
        for l in &self.lets {
            let v = {
                let bindings = SchBindings {
                    namespaces: &self.namespaces,
                    vars:       &schema_vars,
                };
                let sc = sch_static_ctx(&bindings);
                let ctx = EvalCtx { context_node: 0, pos: 1, size: 1, bindings: &bindings, static_ctx: &sc };
                eval_expr(&l.value, &ctx, &idx).map_err(XsltError::from)?
            };
            schema_vars.insert(l.name.clone(), v);
        }

        // Per pattern → per node → first matching rule fires.
        for pattern in &self.patterns {
            // Phase filter: if `active` names a subset, skip
            // patterns whose id isn't in the list.  Patterns
            // without an id can never be selected by phase.
            if let Some(list) = active {
                let Some(pid) = pattern.id.as_deref() else { continue; };
                if !list.iter().any(|n| n == pid) { continue; }
            }
            for node_id in 0..idx.nodes.len() {
                // Skip the synthetic document and namespace nodes
                // by default — rule contexts almost always target
                // elements / attributes.  (Document-rooted rules
                // are still reachable via context="/".)
                let kind = idx.kind(node_id);
                if !matches!(kind,
                    XPathNodeKind::Element | XPathNodeKind::Document
                        | XPathNodeKind::Attribute)
                {
                    continue;
                }
                for rule in &pattern.rules {
                    if !rule_matches(&rule.context, node_id, &idx,
                        &self.namespaces, &schema_vars)?
                    {
                        continue;
                    }
                    // This rule fires.  Evaluate its lets, then
                    // each assertion.  Lower-cased "first rule
                    // wins" semantics: break after this rule.
                    let mut local_vars = schema_vars.clone();
                    for l in &rule.lets {
                        let v = {
                            let bindings = SchBindings {
                                namespaces: &self.namespaces, vars: &local_vars,
                            };
                            let sc = sch_static_ctx(&bindings);
                            let ctx = EvalCtx { context_node: node_id, pos: 1, size: 1, bindings: &bindings, static_ctx: &sc };
                            eval_expr(&l.value, &ctx, &idx).map_err(XsltError::from)?
                        };
                        local_vars.insert(l.name.clone(), v);
                    }
                    for assertion in &rule.asserts {
                        evaluate_assertion(
                            assertion, node_id, &idx,
                            &self.namespaces, &local_vars,
                            pattern, &mut report,
                        )?;
                    }
                    break;
                }
            }
        }
        Ok(report)
    }
}

fn rule_matches(
    context: &Expr, node: NodeId, idx: &DocIndex<'_>,
    namespaces: &HashMap<String, String>,
    vars:       &HashMap<String, Value>,
) -> Result<bool> {
    let bindings = SchBindings { namespaces, vars };
    let sc = sch_static_ctx(&bindings);
    let mut cur = Some(node);
    while let Some(ctx_node) = cur {
        let ctx = EvalCtx { context_node: ctx_node, pos: 1, size: 1, bindings: &bindings, static_ctx: &sc };
        let v = eval_expr(context, &ctx, idx).map_err(XsltError::from)?;
        if let Value::NodeSet(ns) = v {
            if ns.contains(&node) {
                return Ok(true);
            }
        }
        cur = idx.parent(ctx_node);
    }
    Ok(false)
}

fn evaluate_assertion(
    a:          &Assertion,
    node:       NodeId,
    idx:        &DocIndex<'_>,
    namespaces: &HashMap<String, String>,
    vars:       &HashMap<String, Value>,
    pattern:    &Pattern,
    report:     &mut ValidationReport,
) -> Result<()> {
    let bindings = SchBindings { namespaces, vars };
    let sc = sch_static_ctx(&bindings);
    let ctx = EvalCtx { context_node: node, pos: 1, size: 1, bindings: &bindings, static_ctx: &sc };
    let test_v = eval_expr(&a.test, &ctx, idx).map_err(XsltError::from)?;
    let truth = value_to_bool(&test_v);
    let fired = match a.kind {
        AssertKind::Assert => !truth,  // assert fires when test fails
        AssertKind::Report =>  truth,  // report fires when test succeeds
    };
    if !fired { return Ok(()); }
    let message = render_message(&a.message, node, idx, namespaces, vars)?;
    report.findings.push(Finding {
        kind: match a.kind {
            AssertKind::Assert => FindingKind::FailedAssert,
            AssertKind::Report => FindingKind::SuccessfulReport,
        },
        message,
        pattern_id:   pattern.id.clone(),
        assertion_id: a.id.clone(),
        role:         a.role.clone(),
        location_id:  format!("id{:x}", node),
        context_name: idx.local_name(node).to_string(),
    });
    Ok(())
}

fn render_message(
    parts:      &[MessagePart],
    node:       NodeId,
    idx:        &DocIndex<'_>,
    namespaces: &HashMap<String, String>,
    vars:       &HashMap<String, Value>,
) -> Result<String> {
    let bindings = SchBindings { namespaces, vars };
    let sc = sch_static_ctx(&bindings);
    let ctx = EvalCtx { context_node: node, pos: 1, size: 1, bindings: &bindings, static_ctx: &sc };
    let mut s = String::new();
    for p in parts {
        match p {
            MessagePart::Text(t) => s.push_str(t),
            MessagePart::ValueOf(e) => {
                let v = eval_expr(e, &ctx, idx).map_err(XsltError::from)?;
                s.push_str(&value_to_string(&v, idx));
            }
            MessagePart::Name(e) => {
                let v = eval_expr(e, &ctx, idx).map_err(XsltError::from)?;
                let target_node = match v {
                    Value::NodeSet(ns) if !ns.is_empty() => ns[0],
                    _ => node,
                };
                s.push_str(idx.node_name(target_node));
            }
        }
    }
    // Normalise inner whitespace per common Schematron output
    // conventions — collapse runs of whitespace to a single space
    // and trim leading/trailing whitespace.
    let normalised: Vec<&str> = s.split_whitespace().collect();
    Ok(normalised.join(" "))
}

fn value_to_bool(v: &Value) -> bool {
    match v {
        Value::Boolean(b) => *b,
        Value::Number(n)  => n.as_f64() != 0.0 && !n.as_f64().is_nan(),
        Value::String(s)  => !s.is_empty(),
        Value::NodeSet(n) => !n.is_empty(),
        // ForeignNodeSet only originates from document() in compat-
        // driven XPath; Schematron runs against single-doc XPath.
        Value::ForeignNodeSet(n) => !n.is_empty(),
        Value::Typed(t) => {
            if let Some(b) = t.boolean { return b; }
            if let Some(n) = t.numeric { return n != 0.0 && !n.is_nan(); }
            !t.lexical.is_empty()
        }
        Value::Sequence(items) => match items.first() {
            None    => false,
            Some(v) => value_to_bool(v),
        }
        Value::IntRange { lo, hi } if lo == hi => *lo != 0,
        Value::IntRange { .. } => true,
        Value::Map(_) | Value::Array(_) | Value::Function(_) => true,
    }
}

fn value_to_string<I: DocIndexLike>(v: &Value, idx: &I) -> String {
    use sup_xml_core::xpath::eval::value_to_string;
    value_to_string(v, idx)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn compile(text: &str) -> Schematron {
        Schematron::compile_str(text).expect("schematron compile")
    }

    fn validate(sch: &Schematron, xml: &str) -> ValidationReport {
        sch.validate_str(xml).expect("validate")
    }

    // ── compile ─────────────────────────────────────────────

    #[test]
    fn compile_minimal_schema() {
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="r">
                    <assert test="@x">missing x</assert>
                </rule>
            </pattern>
        </schema>"#);
        assert_eq!(s.patterns.len(), 1);
        assert_eq!(s.patterns[0].rules.len(), 1);
        assert_eq!(s.patterns[0].rules[0].asserts.len(), 1);
    }

    #[test]
    fn compile_rejects_non_schematron_root() {
        let err = Schematron::compile_str("<foo/>").unwrap_err();
        assert!(format!("{err}").contains("sch:schema"), "got: {err}");
    }

    #[test]
    fn accepts_old_namespace() {
        let s = compile(r#"<schema xmlns="http://www.ascc.net/xml/schematron">
            <pattern><rule context="*"><assert test="true()">ok</assert></rule></pattern>
        </schema>"#);
        assert_eq!(s.patterns.len(), 1);
    }

    // ── validate ───────────────────────────────────────────

    #[test]
    fn assert_fails_for_missing_attribute() {
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="r">
                    <assert test="@x">missing x</assert>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, "<r/>");
        assert!(!r.valid());
        assert_eq!(r.findings.len(), 1);
        assert_eq!(r.findings[0].kind, FindingKind::FailedAssert);
        assert_eq!(r.findings[0].message, "missing x");
    }

    #[test]
    fn assert_passes_when_test_true() {
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="r">
                    <assert test="@x">missing x</assert>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, r#"<r x="42"/>"#);
        assert!(r.valid());
        assert!(r.findings.is_empty());
    }

    #[test]
    fn report_fires_when_test_true() {
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="r">
                    <report test="@deprecated">element is deprecated</report>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, r#"<r deprecated="yes"/>"#);
        // Reports don't make the doc invalid — only asserts do.
        assert!(r.valid());
        assert_eq!(r.findings.len(), 1);
        assert_eq!(r.findings[0].kind, FindingKind::SuccessfulReport);
    }

    #[test]
    fn first_matching_rule_wins_per_pattern() {
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="*[@kind='a']">
                    <assert test="false()">first rule</assert>
                </rule>
                <rule context="*">
                    <assert test="false()">second rule</assert>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, r#"<r kind="a"/>"#);
        // First rule matches; second rule should NOT fire.
        let messages: Vec<_> = r.findings.iter().map(|f| f.message.clone()).collect();
        assert!(messages.iter().any(|m| m == "first rule"));
        assert!(!messages.iter().any(|m| m == "second rule"));
    }

    #[test]
    fn namespaces_resolve_via_sch_ns_declaration() {
        let s = compile(r##"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <ns prefix="dc" uri="http://purl.org/dc/elements/1.1/"/>
            <pattern>
                <rule context="dc:title">
                    <assert test="normalize-space(.) != ''">title is empty</assert>
                </rule>
            </pattern>
        </schema>"##);
        let r = validate(&s, r#"<r xmlns:dc="http://purl.org/dc/elements/1.1/">
            <dc:title></dc:title>
        </r>"#);
        assert!(!r.valid(), "empty title should fail assertion");
    }

    #[test]
    fn message_inlines_value_of() {
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="r">
                    <assert test="@x">value: <value-of select="@y"/></assert>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, r#"<r y="hello"/>"#);
        // x is missing → assert fails; message renders value-of @y.
        assert_eq!(r.findings[0].message, "value: hello");
    }

    #[test]
    fn message_inlines_name() {
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="*">
                    <report test="@bad">element <name/> has bad attr</report>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, r#"<foo bad="yes"/>"#);
        assert!(r.findings.iter().any(|f| f.message.contains("foo")));
    }

    #[test]
    fn pattern_id_propagates_to_findings() {
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern id="check-001">
                <rule context="r">
                    <assert test="@x">missing x</assert>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, "<r/>");
        assert_eq!(r.findings[0].pattern_id.as_deref(), Some("check-001"));
    }

    #[test]
    fn rule_let_provides_local_binding() {
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="r">
                    <let name="count" value="count(item)"/>
                    <assert test="$count = 3">expected 3 items</assert>
                </rule>
            </pattern>
        </schema>"#);
        // 2 items → assert fails.
        let r = validate(&s, "<r><item/><item/></r>");
        assert!(!r.valid());
        // 3 items → passes.
        let r = validate(&s, "<r><item/><item/><item/></r>");
        assert!(r.valid());
    }

    // ── error paths ────────────────────────────────────────────

    #[test]
    fn rule_without_context_errors() {
        let r = Schematron::compile_str(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern><rule><assert test="true()">ok</assert></rule></pattern>
        </schema>"#);
        assert!(r.is_err());
        if let Err(XsltError::InvalidStylesheet(msg)) = r {
            assert!(msg.contains("context"), "got {msg}");
        }
    }

    #[test]
    fn let_without_name_errors() {
        let r = Schematron::compile_str(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern><rule context="r">
                <let value="@x"/>
                <assert test="true()">ok</assert>
            </rule></pattern>
        </schema>"#);
        assert!(r.is_err());
        if let Err(XsltError::InvalidStylesheet(msg)) = r {
            assert!(msg.contains("name"), "got {msg}");
        }
    }

    #[test]
    fn let_without_value_errors() {
        let r = Schematron::compile_str(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern><rule context="r">
                <let name="count"/>
                <assert test="true()">ok</assert>
            </rule></pattern>
        </schema>"#);
        assert!(r.is_err());
        if let Err(XsltError::InvalidStylesheet(msg)) = r {
            assert!(msg.contains("value"), "got {msg}");
        }
    }

    #[test]
    fn assert_without_test_errors() {
        let r = Schematron::compile_str(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern><rule context="r"><assert>missing</assert></rule></pattern>
        </schema>"#);
        assert!(r.is_err());
        if let Err(XsltError::InvalidStylesheet(msg)) = r {
            assert!(msg.contains("test"), "got {msg}");
            assert!(msg.contains("assert"), "got {msg}");
        }
    }

    #[test]
    fn report_without_test_errors() {
        let r = Schematron::compile_str(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern><rule context="r"><report>missing</report></rule></pattern>
        </schema>"#);
        assert!(r.is_err());
        if let Err(XsltError::InvalidStylesheet(msg)) = r {
            assert!(msg.contains("report"), "got {msg}");
        }
    }

    #[test]
    fn value_of_without_select_errors() {
        let r = Schematron::compile_str(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern><rule context="r">
                <assert test="true()">val=<value-of/></assert>
            </rule></pattern>
        </schema>"#);
        assert!(r.is_err());
    }

    // ── message inline element handling ────────────────────────

    #[test]
    fn message_inlines_emph_via_text_content() {
        // Non-value-of / non-name child element → fallback path
        // (`_ => {...}` arm in compile_message) that just stringifies
        // the child's text content.
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="r">
                    <assert test="@x">missing <emph>x</emph> attribute</assert>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, "<r/>");
        assert!(r.findings[0].message.contains("missing"));
        assert!(r.findings[0].message.contains("x"));
        assert!(r.findings[0].message.contains("attribute"));
    }

    #[test]
    fn name_with_path_attribute() {
        // <name path="@x"/> — use the optional path attribute (default ".").
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="r">
                    <assert test="false()">node was <name path="."/></assert>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, "<r/>");
        assert!(r.findings[0].message.contains("node was"));
        assert!(r.findings[0].message.contains("r"));
    }

    // ── rule unknown children are silently ignored ─────────────

    #[test]
    fn rule_ignores_unknown_children() {
        // <extension> isn't let/assert/report → silently skipped
        // (`_ => {}` arm in compile_rule).
        let s = compile(r#"<schema xmlns="http://purl.oclc.org/dsdl/schematron">
            <pattern>
                <rule context="r">
                    <extension/>
                    <assert test="@x">missing x</assert>
                </rule>
            </pattern>
        </schema>"#);
        let r = validate(&s, "<r/>");
        assert!(!r.valid());
        assert_eq!(r.findings[0].message, "missing x");
    }
}