libxml-rs 0.1.0-alpha.43

Native-Rust forensic reimplementation of libxml2+libxslt with C ABI drop-in replacement. Cross-version oracle matrix (libxml2 2.7.8-2.15.3, libxslt 1.1.26-1.1.45) with semantic epochs; full xmllint/xmlcatalog/xsltproc CLIs; differential-court-verified C API closure; three-DSO ELF packaging (libxml2.so.16 core + libxslt.so.1/libexslt.so.0 facades, upstream NEEDED chain); fail-closed oracle-isolated ABI-FUNCTION-SIGNATURE plane (SOURCE_PROTOTYPE + MACHINE_ABI fingerprints, zero silent omissions); Phase-12 real downstream substitution (binary/static/docker substitution, export-surface disposition, ELF version graphs); Phase-13 hostile audit courts (ABI/ownership/allocator/callbacks/failure/threads/oracle-contamination) byte-identical vs the system oracle incl. the upstream thread-local globals model; Phase-14 downstream custodian validation courts (lxml/Nokogiri/PHP/Debian). Test counts live in atlas/TEST_COUNTS.json, residuals in atlas/RESIDUAL_LEDGER.json (generated evidence).
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
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
//! XSLT stylesheet compilation (§32, §85 Phase 8).
//!
//! # Upstream contract
//!
//! Parity target: upstream libxslt `xslt.c` + `preproc.c` (1.1.45;
//! `SRC-LIBXSLT-1.1.42-XSLT-C` and `SRC-LIBXSLT-1.1.42-PREPROC-C` under
//! oracle/historical/src/libxslt-1.1.42/). The subsystem census maps this
//! module to the xslt-lifecycle, xslt-compilation, xslt-includes,
//! xslt-priorities, xslt-sorting, xslt-whitespace, xslt-transform-ctxt,
//! xslt-global-state and xslt-exports subsystems.
//!
//! # Conceptual behavior
//!
//! `xsltParseStylesheetProcess`-style pipeline: preprocess the tree
//! (merge adjacent text runs, strip whitespace-only runs, drop comments
//! and PIs — `xsltParsePreprocessStylesheetTree`, xslt.c 1.1.45), detect
//! normal vs simplified stylesheets, compile top-level elements in import
//! order, and finish with the output definition, decimal formats, and
//! strip/preserve rules. Compilation errors are reported through
//! `xsltTransformError` with a compilation-error context line.
//!
//! # Ownership & safety invariants
//!
//! `compile` takes ownership of the stylesheet document: it is stored in
//! `style->doc` and freed by `xsltFreeStylesheet` (R-000103: instruction
//! content nodes belong to this document and must never be freed by the
//! template/key/attribute-set free paths). Compiled definitions hold
//! borrowed pointers into the stylesheet document (`inst`) plus
//! heap-duplicated strings. On failure the stylesheet is freed; `style`
//! must be a zero-initialized `_xsltStylesheet` (per-ABI layout,
//! R-000140).
//!
//! # Historical quirks & epochs
//!
//! E-008 (atlas/SEMANTIC_EPOCHS.md): xsltproc compilation+transform output
//! is byte-identical from libxslt 1.1.26 (2009) through 1.1.45, so the
//! compile-time decisions (import precedence, template priority, whitespace
//! stripping) are frozen behavior since 2009. R-000109 fixed the RTF
//! variable double-free whose root cause was a compile-time choice
//! (`var->tree = inst->children`); R-000140 fixed the eight Rust
//! `_xslt*` ABI mirrors this module writes through.
//!
//! # Deliberate oddities
//!
//! - Simplified stylesheet support: a non-XSLT root element is wrapped in
//!   an implicit `xsl:template match="/"` (upstream `literal_result`
//!   flag), an intentional compatibility feature.
//! - The preprocess pass is performed even for non-stylesheet roots
//!   (guarded by `is_stylesheet`) to match upstream tree shaping.
//!
//! # Proving courts
//!
//! XSLT, CLI-XSLTPROC (compilation corpus), EXSLT, ORACLE-IDENTITY,
//! PREPROCESSOR-SURFACE, BUILD-CONFIG-SCRIPT, plus the in-crate `cargo
//! test` suites.
//!
//! # Tempting simplifications that would break parity
//!
//! - Skipping the tree preprocess (whitespace merging) breaks literal
//!   text output of stylesheets (upstream xslt.c does it unconditionally).
//! - Sorting templates with a plain sort instead of the insertion-by-
//!   priority-with-import-depth-tiebreak breaks XSLT 1.0 conflict
//!   resolution (see templates module).
//! - Freeing `inst`-derived content at compile time reintroduces the
//!   R-000109/R-000103 double-free; stylesheet-document ownership must be
//!   honored.
//! - Reusing upstream precedence constants without the candidate layout
//!   (`position` carrying import depth) breaks apply-imports (R-000158
//!   lesson on deferred state).
//!
//! Walks the parsed stylesheet document and produces the compiled
//! representation: templates, keys, variables, parameters, attribute
//! sets, namespace aliases, decimal formats, strip/preserve-space rules,
//! and output settings.
//!
//! # UPSTREAM-PARITY
//!
//! Upstream libxslt (xslt.c) compilation phases:
//!
//! 1. `xsltParseStylesheetDoc` → checks the root element:
//!    - `xsl:stylesheet` / `xsl:transform` — normal stylesheet
//!    - otherwise — simplified stylesheet (literal result element with
//!      an implicit `<xsl:template match="/">`)
//! 2. Process `xsl:import` elements first (they must precede all other
//!    top-level elements).
//! 3. Process remaining top-level elements in document order.
//! 4. Compile templates with their match patterns and priorities.
//! 5. Templates are inserted into the stylesheet's ordered list,
//!    sorted by priority (highest first).

use crate::abi::structs::*;
use crate::abi::types::xmlElementType::*;
use crate::abi::types::*;
use crate::xml::tree::{doc_get_root_element, get_prop, search_ns};
use std::ffi::c_void;
use std::os::raw::{c_char, c_int};
use std::ptr;

/// The XSLT namespace URI.
pub const XSLT_NAMESPACE: &str = "http://www.w3.org/1999/XSL/Transform";

/// Compile a stylesheet from a document.
///
/// Takes ownership of the stylesheet lifecycle: the document is stored in
/// `style->doc`; on failure the stylesheet is freed.
///
/// # SAFETY
///
/// - `style` must be a valid zero-initialized `_xsltStylesheet`.
/// - `doc` must be a valid parsed document.
pub unsafe fn compile(style: *mut _xsltStylesheet, doc: *mut _xmlDoc) -> c_int {
    if style.is_null() || doc.is_null() {
        return -1;
    }
    (*style).doc = doc;
    let root = doc_get_root_element(doc);
    if root.is_null() {
        // No root element: not a stylesheet.
        return -1;
    }

    // Determine the namespace of the root element.
    let root_ns = get_element_ns(root);
    let root_name = get_element_name(root);

    // UPSTREAM-PARITY: preprocess the stylesheet tree before compiling
    // (xsltParsePreprocessStylesheetTree, xslt.c 1.1.45): merge adjacent
    // text runs, strip whitespace-only runs, remove comments and PIs.
    let is_stylesheet = matches!(
        (root_ns.as_deref(), root_name.as_deref()),
        (Some(ns), Some(name))
            if ns == XSLT_NAMESPACE && (name == "stylesheet" || name == "transform")
    );
    preprocess_stylesheet_tree(root, is_stylesheet);

    match (root_ns.as_deref(), root_name.as_deref()) {
        (Some(ns), Some(name))
            if ns == XSLT_NAMESPACE && (name == "stylesheet" || name == "transform") =>
        {
            // Normal stylesheet (upstream xsltParseStylesheetProcess
            // clears literal_result).
            (*style).literal_result = 0;
            // Normal stylesheet.
            compile_top_level(style, root, 0);
        }
        _ => {
            // Simplified stylesheet: a literal result element with an
            // implicit template matching "/" (upstream sets
            // literal_result = 1, which disables the preproc parent-context
            // checks).
            (*style).literal_result = 1;
            // UPSTREAM-PARITY (xslt.c xsltParseStylesheetProcess 1.1.42): a
            // simplified stylesheet root must carry an `xsl:version`
            // attribute; without it the document is not a stylesheet.
            let xslt_ns = b"http://www.w3.org/1999/XSL/Transform\0";
            let version_attr = crate::xml::tree::has_ns_prop(
                root,
                c"version".as_ptr() as *const xmlChar,
                xslt_ns.as_ptr() as *const xmlChar,
            );
            if version_attr.is_null() {
                crate::xslt::errors::xsltTransformError(
                    ptr::null_mut(),
                    style,
                    root,
                    c"xsltParseStylesheetProcess : document is not a stylesheet\n".as_ptr()
                        as *const c_char,
                );
                return -1;
            }
            compile_simplified(style, root);
        }
    }

    // UPSTREAM-PARITY (preproc.c xsltPreCompute): select/test expressions of
    // the select-bearing instructions are compiled at stylesheet-parse time;
    // a failed compile emits "XPath error : Invalid expression", the
    // compilation-error context line and the instruction-specific message,
    // then increments style->errors (xsltproc exits 5). The candidate
    // evaluates XPath at transform time, so this document-order walk
    // validates the same expressions at compile time to reproduce the
    // oracle's diagnostics (R-000166 / malformed-XSLT item).
    validate_content_selects(style, root);
    0
}

/// Validate the select/test expressions of every select-bearing XSLT
/// instruction in document order (upstream preproc.c xsltPreCompute).
///
/// # SAFETY
///
/// - `style` and `node` must be valid pointers.
unsafe fn validate_content_selects(style: *mut _xsltStylesheet, node: *mut _xmlNode) {
    let mut cur = node;
    while !cur.is_null() {
        unsafe {
            validate_content_selects(style, (*cur).children);
            validate_one_select(style, cur);
        }
        cur = unsafe { (*cur).next };
    }
}

/// Validate one instruction's select/test expression (upstream preproc.c
/// message catalog).
///
/// # SAFETY
///
/// - `style` / `inst` must be valid pointers.
unsafe fn validate_one_select(style: *mut _xsltStylesheet, inst: *mut _xmlNode) {
    let (
        is_value_of,
        is_copy_of,
        is_for_each,
        is_apply,
        is_sort,
        is_if,
        is_when,
        is_variable,
        is_param,
    ) = (
        is_xslt_element(inst, "value-of"),
        is_xslt_element(inst, "copy-of"),
        is_xslt_element(inst, "for-each"),
        is_xslt_element(inst, "apply-templates"),
        is_xslt_element(inst, "sort"),
        is_xslt_element(inst, "if"),
        is_xslt_element(inst, "when"),
        is_xslt_element(inst, "variable"),
        is_xslt_element(inst, "param"),
    );
    if !(is_value_of
        || is_copy_of
        || is_for_each
        || is_apply
        || is_sort
        || is_if
        || is_when
        || is_variable
        || is_param)
    {
        return;
    }
    // UPSTREAM-PARITY (preproc.c xsltCheckParentElement, called from
    // xsltSortComp): xsl:sort is only allowed within xsl:apply-templates or
    // xsl:for-each; otherwise "element sort is not allowed within that
    // context" is reported first (before the select compile). Simplified
    // stylesheets (literal_result) skip the parent checks.
    if is_sort && unsafe { (*style).literal_result } == 0 {
        let parent = unsafe { (*inst).parent };
        let parent_ok = !parent.is_null()
            && (is_xslt_element(parent, "apply-templates") || is_xslt_element(parent, "for-each"));
        if !parent_ok {
            let mut cmsg = b"element sort is not allowed within that context\n".to_vec();
            let msg_len = cmsg.len();
            cmsg.push(0);
            crate::xslt::errors::xsltTransformError(
                ptr::null_mut(),
                style,
                inst,
                cmsg.as_ptr() as *const c_char,
            );
            let _ = msg_len;
            unsafe { (*style).errors += 1 };
        }
    }
    let attr = if is_if || is_when {
        c"test".as_ptr() as *const xmlChar
    } else {
        c"select".as_ptr() as *const xmlChar
    };
    let prop = get_prop(inst, attr);
    if prop.is_null() {
        return;
    }
    let s = String::from_utf8_lossy(unsafe {
        core::slice::from_raw_parts(prop as *const u8, libc::strlen(prop as *const libc::c_char))
    })
    .into_owned();
    if crate::xml::xpath::compile(&s).is_none() {
        let msg = if is_value_of {
            format!(
                "xsl:value-of : could not compile select expression '{}'\n",
                s
            )
        } else if is_copy_of {
            format!(
                "xsl:copy-of : could not compile select expression '{}'\n",
                s
            )
        } else if is_for_each {
            format!(
                "xsl:for-each : could not compile select expression '{}'\n",
                s
            )
        } else if is_apply {
            format!(
                "XSLT-apply-templates: could not compile select expression '{}'\n",
                s
            )
        } else if is_sort {
            format!(
                "xsltSortComp: could not compile select expression '{}'\n",
                s
            )
        } else if is_if {
            format!("xsl:if : could not compile test expression '{}'\n", s)
        } else if is_when {
            format!("xsl:when : could not compile test expression '{}'\n", s)
        } else if is_variable {
            format!(
                "XSLT-variable: Failed to compile the XPath expression '{}'.\n",
                s
            )
        } else {
            format!("XSLT-param: could not compile select expression '{}'.\n", s)
        };
        // "XPath error : Invalid expression" (xpath.c xmlXPathErr).
        let xerr = "XPath error : Invalid expression\n";
        libc::write(2, xerr.as_ptr() as *const libc::c_void, xerr.len());
        let mut cmsg = msg.into_bytes();
        let msg_len = cmsg.len();
        cmsg.push(0);
        crate::xslt::errors::xsltTransformError(
            ptr::null_mut(),
            style,
            inst,
            cmsg.as_ptr() as *const c_char,
        );
        let _ = msg_len;
        (*style).errors += 1;
    }
    libc::free(prop as *mut libc::c_void);
}

/// Get the document URL (or NULL).
#[allow(dead_code)]
unsafe fn doc_URL(doc: *mut _xmlDoc) -> *const xmlChar {
    if doc.is_null() || (*doc).URL.is_null() {
        return ptr::null();
    }
    (*doc).URL
}

/// Resolve an `xsl:include`/`xsl:import` href against the stylesheet
/// document's base URI (upstream imports.c xsltParseStylesheetImport /
/// xsltParseStylesheetInclude: `xmlNodeGetBase(style->doc, cur)` then
/// `xmlBuildURI(uriRef, base)`). Returns owned bytes of the href when no
/// base resolves it.
///
/// # SAFETY
///
/// - `style` and `href` must be valid pointers.
unsafe fn resolve_style_href(style: *mut _xsltStylesheet, href: *const xmlChar) -> Vec<u8> {
    let href_bytes =
        core::slice::from_raw_parts(href, libc::strlen(href as *const libc::c_char) as usize)
            .to_vec();
    let base = if !(*style).doc.is_null() && !(*(*style).doc).URL.is_null() {
        let len = libc::strlen((*(*style).doc).URL as *const libc::c_char) as usize;
        Some(core::slice::from_raw_parts((*(*style).doc).URL, len).to_vec())
    } else {
        None
    };
    match base {
        Some(b) => crate::xml::uri::resolve_uri(&b, &href_bytes).unwrap_or(href_bytes),
        None => href_bytes,
    }
}

/// Compile the top-level elements of a stylesheet.
///
/// # SAFETY
///
/// - All pointers must be valid.
pub(crate) unsafe fn compile_top_level(
    style: *mut _xsltStylesheet,
    root: *mut _xmlNode,
    depth: c_int,
) {
    // Process imports first (they must precede other elements).
    let mut child = (*root).children;
    while !child.is_null() {
        let next = (*child).next;
        if is_xslt_element(child, "import") {
            let href = get_prop(child, c"href".as_ptr() as *const xmlChar);
            if !href.is_null() {
                // UPSTREAM-PARITY (imports.c): resolve against the
                // stylesheet base URI, not the process CWD.
                let resolved = resolve_style_href(style, href);
                let mut c = resolved;
                c.push(0);
                let imported =
                    crate::xslt::stylesheet::xsltParseStylesheetFile(c.as_ptr() as *const xmlChar);
                if !imported.is_null() {
                    (*imported).parent = style;
                    (*imported).next = (*style).imports;
                    (*style).imports = imported;
                    // Compile the imported stylesheet's top-level into its
                    // own templates (already done during its parse).
                }
                libc::free(href as *mut libc::c_void);
            }
        }
        child = next;
    }

    // Process remaining top-level elements.
    let mut child = (*root).children;
    while !child.is_null() {
        let next = (*child).next;
        if (*child).type_ != XML_ELEMENT_NODE as c_int {
            child = next;
            continue;
        }
        if !is_xslt_namespace(child) {
            child = next;
            continue;
        }
        let name = get_element_name(child);
        match name.as_deref() {
            Some("import") => {} // already handled
            Some("include") => {
                let href = get_prop(child, c"href".as_ptr() as *const xmlChar);
                if !href.is_null() {
                    // UPSTREAM-PARITY (imports.c): resolve against the
                    // stylesheet base URI, not the process CWD.
                    let resolved = resolve_style_href(style, href);
                    let mut c = resolved;
                    c.push(0);
                    let included = crate::xslt::stylesheet::xsltParseStylesheetFile(
                        c.as_ptr() as *const xmlChar
                    );
                    if !included.is_null() {
                        // Compile the included stylesheet's top-level
                        // elements at the same depth.
                        let inc_root = doc_get_root_element((*included).doc);
                        if !inc_root.is_null() {
                            compile_top_level(style, inc_root, depth);
                        }
                        // The included doc's templates were added to the
                        // included style; move them to this style.
                        move_templates(included, style);
                        // Merge strip/preserve spaces.
                        merge_strip_spaces(included, style);
                        // Free the included stylesheet shell (not the doc).
                        (*included).doc = ptr::null_mut();
                        crate::xslt::stylesheet::xsltFreeStylesheet(included);
                    }
                    libc::free(href as *mut libc::c_void);
                }
            }
            Some("template") => {
                compile_template(style, child, depth);
            }
            Some("variable") | Some("param") => {
                let is_param = if name.as_deref() == Some("param") {
                    1
                } else {
                    0
                };
                compile_variable(style, child, depth, is_param);
            }
            Some("key") => {
                compile_key(style, child, depth);
            }
            Some("decimal-format") => {
                compile_decimal_format(style, child);
            }
            Some("namespace-alias") => {
                compile_namespace_alias(style, child);
            }
            Some("attribute-set") => {
                compile_attribute_set(style, child, depth);
            }
            Some("strip-space") => {
                compile_space_rules(style, child, 1, depth);
            }
            Some("preserve-space") => {
                compile_space_rules(style, child, 0, depth);
            }
            Some("output") => {
                compile_output(style, child);
            }
            Some("stylesheet") | Some("transform") => {
                // Nested stylesheet elements are an error; ignore.
            }
            _ => {}
        }
        child = next;
    }
}

/// Check whether a node is an XSLT element with the given local name.
///
/// # SAFETY
///
/// - `node` must be a valid element node.
pub unsafe fn is_xslt_element(node: *mut _xmlNode, name: &str) -> bool {
    if node.is_null() || (*node).type_ != XML_ELEMENT_NODE as c_int {
        return false;
    }
    let ns = get_element_ns(node);
    if ns.as_deref() != Some(XSLT_NAMESPACE) {
        return false;
    }
    get_element_name(node).as_deref() == Some(name)
}

/// Check whether a node is in the XSLT namespace.
///
/// # SAFETY
///
/// - `node` must be a valid element node.
pub unsafe fn is_xslt_namespace(node: *mut _xmlNode) -> bool {
    if node.is_null() || (*node).type_ != XML_ELEMENT_NODE as c_int {
        return false;
    }
    get_element_ns(node).as_deref() == Some(XSLT_NAMESPACE)
}

/// Get the namespace URI of an element as a String.
///
/// # SAFETY
///
/// - `node` must be a valid element node.
pub unsafe fn get_element_ns(node: *mut _xmlNode) -> Option<String> {
    if node.is_null() || (*node).ns.is_null() || (*(*node).ns).href.is_null() {
        return None;
    }
    let bytes = core::slice::from_raw_parts(
        (*(*node).ns).href,
        libc::strlen((*(*node).ns).href as *const libc::c_char) as usize,
    );
    String::from_utf8_lossy(bytes).into_owned().into()
}

/// Get the local name of an element as a String.
///
/// # SAFETY
///
/// - `node` must be a valid element node.
pub unsafe fn get_element_name(node: *mut _xmlNode) -> Option<String> {
    if node.is_null() || (*node).name.is_null() {
        return None;
    }
    let bytes = core::slice::from_raw_parts(
        (*node).name,
        libc::strlen((*node).name as *const libc::c_char) as usize,
    );
    String::from_utf8_lossy(bytes).into_owned().into()
}

/// Preprocess the stylesheet's node tree.
///
/// # UPSTREAM-PARITY
///
/// Faithful port of the strict-mode behavior of
/// `xsltParsePreprocessStylesheetTree` (xslt.c, libxslt 1.1.45):
///
/// - adjacent text/CDATA-section nodes are merged into one text node;
/// - a merged run that is whitespace-only is removed, unless the parent
///   element has `xml:space="preserve"` or is `xsl:text`;
/// - comments and processing instructions are removed from the tree;
/// - CDATA-section nodes are converted to text nodes;
/// - whitespace-only text nodes directly preceding `xsl:param`/`xsl:sort`
///   are removed;
/// - elements in the XSLT "strip" set (`xsl:choose`, `xsl:call-template`,
///   `xsl:apply-templates`, `xsl:apply-imports`, `xsl:attribute-set`, and
///   the `xsl:stylesheet`/`xsl:transform` element itself) always strip
///   whitespace-only runs.
///
/// This explains the observable behavior verified against the oracle:
/// `<xsl:template><a>\n  X\n</a></xsl:template>` keeps the whitespace
/// around `X` (the merged run is not whitespace-only) while
/// `<xsl:template><a>\n  <b/></a></xsl:template>` strips it.
///
/// # SAFETY
///
/// - `root` must be a valid element node of the stylesheet document.
unsafe fn preprocess_stylesheet_tree(root: *mut _xmlNode, is_stylesheet: bool) {
    /// Per-element whitespace-handling state (upstream compiler-node-info).
    struct St {
        strip_whitespace: bool,
        preserve_whitespace: bool,
    }

    /// True when every byte is a whitespace character (upstream xsltIsBlank).
    const unsafe fn is_blank(content: *const xmlChar) -> bool {
        if content.is_null() {
            return true;
        }
        let mut i = 0usize;
        while unsafe { *content.add(i) != 0 } {
            match unsafe { *content.add(i) } {
                b' ' | b'\t' | b'\n' | b'\r' => {}
                _ => return false,
            }
            i += 1;
        }
        true
    }

    /// Append `src` bytes to `dst`'s content (upstream xmlNodeAddContent).
    unsafe fn merge_text(dst: *mut _xmlNode, src: *const xmlChar) {
        if src.is_null() {
            return;
        }
        let src_len = crate::xml::tree::xml_strlen(src) as usize;
        if src_len == 0 {
            return;
        }
        let cur = unsafe { (*dst).content };
        let cur_len = if cur.is_null() {
            0
        } else {
            crate::xml::tree::xml_strlen(cur) as usize
        };
        let new_len = cur_len + src_len;
        let buf = crate::abi::allocator::xmlMallocImpl(new_len + 1) as *mut xmlChar;
        if buf.is_null() {
            return;
        }
        if cur_len > 0 && !cur.is_null() {
            core::ptr::copy_nonoverlapping(cur, buf, cur_len);
        }
        core::ptr::copy_nonoverlapping(src, buf.add(cur_len), src_len);
        *buf.add(new_len) = 0;
        if !cur.is_null() {
            crate::abi::allocator::xmlFreeImpl(cur as *mut core::ffi::c_void);
        }
        unsafe { (*dst).content = buf };
    }

    /// Convert a CDATA-section node to a text node.
    unsafe fn to_text(node: *mut _xmlNode) {
        if unsafe { (*node).type_ } != XML_CDATA_SECTION_NODE as c_int {
            return;
        }
        unsafe {
            (*node).type_ = XML_TEXT_NODE as c_int;
            let name = crate::abi::allocator::xmlMallocImpl(5) as *mut xmlChar;
            if !name.is_null() {
                core::ptr::copy_nonoverlapping(c"text".as_ptr() as *const xmlChar, name, 5);
                (*node).name = name;
            }
        }
    }

    /// The xml:space attribute value of an element (1 = preserve,
    /// 0 = default, -1 = unset).
    unsafe fn xml_space(node: *mut _xmlNode) -> c_int {
        let mut attr = unsafe { (*node).properties };
        while !attr.is_null() {
            let a = unsafe { &*attr };
            let ns_is_xml = if a.ns.is_null() {
                false
            } else {
                let prefix = unsafe { (*a.ns).prefix };
                !prefix.is_null()
                    && unsafe { *prefix == b'x' }
                    && unsafe { *prefix.add(1) == b'm' }
                    && unsafe { *prefix.add(2) == b'l' }
                    && unsafe { *prefix.add(3) == 0 }
            };
            if ns_is_xml && !a.name.is_null() && unsafe { *a.name == b's' } {
                let name =
                    crate::abi::versioning::c_str_to_bytes(a.name as *const std::os::raw::c_char)
                        .unwrap_or(b"");
                if name == b"space" && !a.children.is_null() {
                    let val =
                        crate::abi::versioning::c_str_to_bytes(
                            unsafe { (*a.children).content } as *const std::os::raw::c_char
                        )
                        .unwrap_or(b"");
                    if val == b"preserve" {
                        return 1;
                    }
                    if val == b"default" {
                        return 0;
                    }
                }
            }
            attr = a.next;
        }
        -1
    }

    /// Apply the end-of-text-run strip check (upstream `end_of_text`).
    /// Returns true when the node was scheduled for deletion.
    unsafe fn check_strip(node: *mut _xmlNode, st: &St, delete: &mut Vec<*mut _xmlNode>) {
        if node.is_null() {
            return;
        }
        let content = unsafe { (*node).content };
        let blank = content.is_null() || unsafe { *content == 0 } || is_blank(content);
        if blank && (st.strip_whitespace || !st.preserve_whitespace) {
            delete.push(node);
        } else {
            to_text(node);
        }
    }

    unsafe fn walk(children: *mut _xmlNode, st: &St, stylesheet_depth: bool, _top_level: bool) {
        let mut cur = children;
        let mut text_node: *mut _xmlNode = ptr::null_mut();
        let mut delete: Vec<*mut _xmlNode> = Vec::new();

        while !cur.is_null() {
            let next = unsafe { (*cur).next };
            for d in delete.drain(..) {
                crate::xml::tree::unlink_node(d);
                crate::xml::tree::free_node(d);
            }

            match unsafe { (*cur).type_ } {
                t if t == XML_ELEMENT_NODE as c_int => {
                    // Compute the state for this element's content. Upstream
                    // resets stripWhitespace to 0 for every element (only the
                    // listed XSLT instructions set it to 1);
                    // preserveWhitespace is inherited and can be changed by
                    // xml:space or xsl:text.
                    let mut nst = St {
                        strip_whitespace: false,
                        preserve_whitespace: st.preserve_whitespace,
                    };
                    if !unsafe { (*cur).children }.is_null() {
                        match xml_space(cur) {
                            1 => nst.preserve_whitespace = true,
                            0 => nst.preserve_whitespace = false,
                            _ => {}
                        }
                    }
                    if is_xslt_element(cur, "text") {
                        nst.preserve_whitespace = true;
                    } else if is_xslt_element(cur, "choose")
                        || is_xslt_element(cur, "call-template")
                        || is_xslt_element(cur, "apply-templates")
                        || is_xslt_element(cur, "apply-imports")
                        || is_xslt_element(cur, "attribute-set")
                    {
                        nst.strip_whitespace = true;
                    } else if stylesheet_depth {
                        // The xsl:stylesheet/xsl:transform element itself.
                        nst.strip_whitespace = true;
                    } else if is_xslt_element(cur, "param") || is_xslt_element(cur, "sort") {
                        // Remove whitespace-only text nodes directly before
                        // xsl:param / xsl:sort (upstream default case).
                        let mut prev = unsafe { (*cur).prev };
                        while !prev.is_null()
                            && unsafe { (*prev).type_ } == XML_TEXT_NODE as c_int
                            && is_blank(unsafe { (*prev).content })
                        {
                            let p = prev;
                            prev = unsafe { (*prev).prev };
                            crate::xml::tree::unlink_node(p);
                            crate::xml::tree::free_node(p);
                        }
                    }
                    if !unsafe { (*cur).children }.is_null() {
                        walk(unsafe { (*cur).children }, &nst, false, false);
                    }
                }
                t if t == XML_TEXT_NODE as c_int || t == XML_CDATA_SECTION_NODE as c_int => {
                    // Strict mode: merge adjacent text nodes.
                    if text_node.is_null() {
                        text_node = cur;
                    } else {
                        merge_text(text_node, unsafe { (*cur).content });
                        delete.push(cur);
                    }
                    let end_of_run = unsafe { (*cur).next }.is_null()
                        || unsafe { (*(*cur).next).type_ } == XML_ELEMENT_NODE as c_int;
                    if end_of_run {
                        check_strip(text_node, st, &mut delete);
                        text_node = ptr::null_mut();
                    }
                }
                t if t == XML_COMMENT_NODE as c_int || t == XML_PI_NODE as c_int => {
                    // A comment/PI terminates the pending text run: the
                    // whitespace-only run BEFORE it must be stripped on its
                    // own (upstream xslt.c preprocessing treats the deleted
                    // node as a run boundary), otherwise it merges with the
                    // text after the comment and leaks blank lines into the
                    // template output (xslt010_gt10129 blank-row diff).
                    if !text_node.is_null() {
                        check_strip(text_node, st, &mut delete);
                        text_node = ptr::null_mut();
                    }
                    delete.push(cur);
                }
                _ => {}
            }
            cur = next;
        }
        for d in delete {
            crate::xml::tree::unlink_node(d);
            crate::xml::tree::free_node(d);
        }
    }

    let st = St {
        strip_whitespace: false,
        preserve_whitespace: false,
    };
    walk(unsafe { (*root).children }, &st, is_stylesheet, true);
}

/// Compile a simplified stylesheet: a literal result element with an
/// implicit `<xsl:template match="/">`.
///
/// # SAFETY
///
/// - All pointers must be valid.
unsafe fn compile_simplified(style: *mut _xsltStylesheet, root: *mut _xmlNode) {
    // Create an implicit template matching "/" (the document node).
    let templ = libc::calloc(1, core::mem::size_of::<_xsltTemplate>()) as *mut _xsltTemplate;
    if templ.is_null() {
        return;
    }
    (*templ).style = style;
    (*templ).priority = 0.5; // match="/" has implicit priority 0.5 (f32)
    (*templ).content = root;
    // Compile the "/" pattern so the template matches the document node.
    // UPSTREAM-PARITY: templ->match holds the match STRING; the compiled
    // pattern is carried in templ->params (void*, unused upstream for the
    // candidate's internal pattern pointer — documented safe divergence).
    // The match string is heap-copied: xsltFreeTemplate owns r#match.
    (*templ).r#match = libc::malloc(2) as *mut xmlChar;
    if (*templ).r#match.is_null() {
        libc::free(templ as *mut libc::c_void);
        return;
    }
    *(*templ).r#match = b'/';
    *(*templ).r#match.add(1) = 0;
    let compiled =
        crate::xslt::patterns::xsltCompilePattern(c"/".as_ptr() as *const xmlChar, (*style).doc);
    (*templ).params = compiled as *mut c_void;
    // The root element becomes the template content.
    add_template_to_style(style, templ);
}

/// Add a compiled template to the stylesheet's ordered list.
///
/// The list is maintained in descending priority order.
///
/// # SAFETY
///
/// - All pointers must be valid.
pub(crate) unsafe fn add_template_to_style(style: *mut _xsltStylesheet, templ: *mut _xsltTemplate) {
    if style.is_null() || templ.is_null() {
        return;
    }
    (*templ).next = (*style).templates;
    (*style).templates = templ;
}

/// Compile an `xsl:template` element.
///
/// # SAFETY
///
/// - All pointers must be valid.
pub(crate) unsafe fn compile_template(
    style: *mut _xsltStylesheet,
    inst: *mut _xmlNode,
    _depth: c_int,
) {
    let templ = libc::calloc(1, core::mem::size_of::<_xsltTemplate>()) as *mut _xsltTemplate;
    if templ.is_null() {
        return;
    }
    (*templ).style = style;
    (*templ).content = (*inst).children;

    // match attribute.
    let match_str = get_prop(inst, c"match".as_ptr() as *const xmlChar);
    if !match_str.is_null() {
        // UPSTREAM-PARITY: templ->match holds the match STRING; the compiled
        // pattern is carried in templ->params (candidate-internal pointer).
        let compiled = crate::xslt::patterns::xsltCompilePattern(match_str, (*style).doc);
        (*templ).r#match = match_str; // ownership of the string transfers
        (*templ).params = compiled as *mut c_void;
        // Compute the default priority from the pattern string.
        (*templ).priority = crate::xslt::patterns::xsltDefaultPriority(match_str) as f32;
    } else {
        (*templ).priority = 0.0;
    }

    // name attribute.
    let name_str = get_prop(inst, c"name".as_ptr() as *const xmlChar);
    if !name_str.is_null() {
        (*templ).name = name_str;
    }

    // mode attribute.
    let mode_str = get_prop(inst, c"mode".as_ptr() as *const xmlChar);
    if !mode_str.is_null() {
        (*templ).mode = mode_str;
    }

    // priority attribute.
    let priority_str = get_prop(inst, c"priority".as_ptr() as *const xmlChar);
    if !priority_str.is_null() {
        let p = crate::abi::exports_xml2::xmlXPathCastStringToNumber(priority_str);
        if !p.is_nan() {
            (*templ).priority = p as f32;
        }
        libc::free(priority_str as *mut libc::c_void);
    }

    add_template_to_style(style, templ);
}

/// Compile an `xsl:variable` or `xsl:param` element.
///
/// # SAFETY
///
/// - All pointers must be valid.
pub(crate) unsafe fn compile_variable(
    style: *mut _xsltStylesheet,
    inst: *mut _xmlNode,
    depth: c_int,
    is_param: c_int,
) {
    let var = libc::calloc(1, core::mem::size_of::<_xsltStackElem>()) as *mut _xsltStackElem;
    if var.is_null() {
        return;
    }
    // UPSTREAM-PARITY: xsltStackElem has no style/inst/depth fields; the
    // scope level lives in `level`, the PARAM marker in `flags`.
    (*var).level = depth;
    (*var).flags = if is_param != 0 { 2 } else { 0 }; // PARAM flag

    let name = get_prop(inst, c"name".as_ptr() as *const xmlChar);
    if !name.is_null() {
        (*var).name = name;
    }
    let select = get_prop(inst, c"select".as_ptr() as *const xmlChar);
    if !select.is_null() {
        (*var).select = select;
    }
    // Content (inline value template).
    (*var).tree = (*inst).children;

    // Add to the stylesheet's variable list (upstream: `variables` is the
    // xsltStackElem list head).
    (*var).next = (*style).variables;
    (*style).variables = var;
}

/// Compile an `xsl:key` element.
///
/// # SAFETY
///
/// - All pointers must be valid.
pub(crate) unsafe fn compile_key(style: *mut _xsltStylesheet, inst: *mut _xmlNode, _depth: c_int) {
    let name = get_prop(inst, c"name".as_ptr() as *const xmlChar);
    let match_str = get_prop(inst, c"match".as_ptr() as *const xmlChar);
    let use_str = get_prop(inst, c"use".as_ptr() as *const xmlChar);
    if name.is_null() || match_str.is_null() || use_str.is_null() {
        if !name.is_null() {
            libc::free(name as *mut libc::c_void);
        }
        if !match_str.is_null() {
            libc::free(match_str as *mut libc::c_void);
        }
        if !use_str.is_null() {
            libc::free(use_str as *mut libc::c_void);
        }
        return;
    }
    let def = libc::calloc(1, core::mem::size_of::<_xsltKeyDef>()) as *mut _xsltKeyDef;
    if def.is_null() {
        libc::free(name as *mut libc::c_void);
        libc::free(match_str as *mut libc::c_void);
        libc::free(use_str as *mut libc::c_void);
        return;
    }
    (*def).inst = inst;
    (*def).name = name;
    (*def).r#match = match_str;
    (*def).r#use = use_str;
    (*def).next = (*style).keys as *mut _xsltKeyDef;
    (*style).keys = def as *mut c_void;
}

/// Compile an `xsl:decimal-format` element.
///
/// UPSTREAM-PARITY (xslt.c `xsltParseStylesheetDecimalFormat`):
///
/// - With no `name` attribute the **default** decimal format (the chain
///   head, created by `xsltNewStylesheetInternal`) is customized in place.
/// - With a `name` the QName is validated, its prefix resolved against the
///   element's in-scope namespaces, an existing format with the same QName
///   is rejected ("already exists" warning), and the new format is
///   **appended** after the default.
///
/// # SAFETY
///
/// - All pointers must be valid.
pub(crate) unsafe fn compile_decimal_format(style: *mut _xsltStylesheet, inst: *mut _xmlNode) {
    if style.is_null() || inst.is_null() {
        return;
    }
    let name = get_prop(inst, c"name".as_ptr() as *const xmlChar);
    let fmt: *mut _xsltDecimalFormat;
    let mut ns_uri: *const xmlChar = ptr::null();

    if name.is_null() {
        // No name: customize the default format in place.
        fmt = (*style).decimalFormat;
        if fmt.is_null() {
            return;
        }
    } else {
        // Validate the QName (upstream xmlValidateQName).
        let bytes = core::slice::from_raw_parts(
            name as *const u8,
            libc::strlen(name as *const libc::c_char),
        );
        let mut valid = !bytes.is_empty() && !bytes.iter().any(|b| b.is_ascii_whitespace());
        let colon = bytes.iter().position(|&b| b == b':');
        if let Some(idx) = colon {
            if idx == 0 || idx + 1 >= bytes.len() || bytes[idx + 1..].contains(&b':') {
                valid = false;
            }
        }
        if !valid {
            let msg = format!(
                "xsl:decimal-format: Invalid QName '{}'\n",
                String::from_utf8_lossy(bytes)
            );
            emit_compile_error(style, inst, msg.as_bytes());
            (*style).warnings += 1;
            libc::free(name as *mut libc::c_void);
            return;
        }
        // Resolve the namespace URI for a prefixed QName.
        let local_name: *mut xmlChar;
        if let Some(idx) = colon {
            let mut prefix_buf = bytes[..idx].to_vec();
            prefix_buf.push(0);
            let ns = search_ns((*inst).doc, inst, prefix_buf.as_ptr() as *const xmlChar);
            if ns.is_null() {
                // Upstream xsltGetQNameURI: unresolvable prefix -> warning.
                let msg = format!(
                    "xsl:decimal-format: Invalid QName '{}'\n",
                    String::from_utf8_lossy(bytes)
                );
                emit_compile_error(style, inst, msg.as_bytes());
                (*style).warnings += 1;
                libc::free(name as *mut libc::c_void);
                return;
            }
            ns_uri = (*ns).href;
            let mut owned = bytes[idx + 1..].to_vec();
            owned.push(0);
            local_name = libc::malloc(owned.len()) as *mut xmlChar;
            if local_name.is_null() {
                libc::free(name as *mut libc::c_void);
                return;
            }
            libc::memcpy(
                local_name as *mut libc::c_void,
                owned.as_ptr() as *const libc::c_void,
                owned.len(),
            );
            libc::free(name as *mut libc::c_void);
        } else {
            local_name = name;
        }
        // Reject duplicate QNames (upstream "already exists" warning).
        let existing =
            crate::abi::exports_xslt_exec::xsltDecimalFormatGetByQName(style, ns_uri, local_name);
        if !existing.is_null() {
            let msg = format!(
                "xsltParseStylestyleDecimalFormat: {} already exists\n",
                String::from_utf8_lossy(unsafe {
                    core::slice::from_raw_parts(
                        local_name as *const u8,
                        libc::strlen(local_name as *const libc::c_char),
                    )
                })
            );
            emit_compile_error(style, inst, msg.as_bytes());
            (*style).warnings += 1;
            libc::free(local_name as *mut libc::c_void);
            return;
        }
        // Create the new format and append it after the default (upstream
        // walks to the tail).
        fmt =
            libc::calloc(1, core::mem::size_of::<_xsltDecimalFormat>()) as *mut _xsltDecimalFormat;
        if fmt.is_null() {
            libc::free(local_name as *mut libc::c_void);
            return;
        }
        (*fmt).name = local_name;
        // Borrowed ns href (the stylesheet document owns it and outlives
        // the format list; see xsltFreeStylesheet ordering).
        (*fmt).nsUri = ns_uri;
        let mut tail = (*style).decimalFormat;
        while !tail.is_null() && !(*tail).next.is_null() {
            tail = (*tail).next;
        }
        if !tail.is_null() {
            (*tail).next = fmt;
        } else {
            (*style).decimalFormat = fmt;
        }
    }
    // Attribute characters (applied to the default in place, or to the
    // freshly created named format).
    set_fmt_char(fmt, inst, b"decimal-separator\0", b".\0", 0);
    set_fmt_char(fmt, inst, b"grouping-separator\0", b",\0", 1);
    set_fmt_char(fmt, inst, b"infinity\0", b"Infinity\0", 2);
    set_fmt_char(fmt, inst, b"minus-sign\0", b"-\0", 3);
    set_fmt_char(fmt, inst, b"NaN\0", b"NaN\0", 4);
    set_fmt_char(fmt, inst, b"percent\0", b"%\0", 5);
    set_fmt_char(fmt, inst, b"per-mille\0", "‰".as_bytes(), 6);
    set_fmt_char(fmt, inst, b"zero-digit\0", b"0\0", 7);
    set_fmt_char(fmt, inst, b"digit\0", b"#\0", 8);
    set_fmt_char(fmt, inst, b"pattern-separator\0", b";\0", 9);
}

/// Emit a stylesheet-compile error through the XSLT error machinery with
/// upstream-identical observable output (message without a trailing
/// newline; the error channel appends one).
///
/// # SAFETY
///
/// - `style`/`inst` may be NULL.
unsafe fn emit_compile_error(style: *mut _xsltStylesheet, inst: *mut _xmlNode, msg: &[u8]) {
    {
        let mut buf = msg.to_vec();
        buf.push(0);
        crate::xslt::errors::xsltTransformError(
            ptr::null_mut(),
            style,
            inst,
            buf.as_ptr() as *const c_char,
        );
    }
}

/// Set a decimal format character from the instruction attributes or default.
///
/// # SAFETY
///
/// - All pointers must be valid.
unsafe fn set_fmt_char(
    fmt: *mut _xsltDecimalFormat,
    inst: *mut _xmlNode,
    attr_name: &[u8],
    default: &[u8],
    field: c_int,
) {
    let mut full = attr_name.to_vec();
    full.push(0);
    // UPSTREAM-PARITY: the character comes from the `xsl:decimal-format`
    // instruction's attribute (xslt.c xsltParseStylesheetDecimalFormat), or
    // the standard default when the attribute is absent.
    let val = get_prop(inst, full.as_ptr() as *const xmlChar);
    let chosen = if !val.is_null() {
        val
    } else {
        alloc_str(default)
    };
    // Free any previous value (the default format may be customized in
    // place by a name-less xsl:decimal-format, exactly as upstream frees
    // the old field before assigning).
    match field {
        0 => {
            if !(*fmt).decimalPoint.is_null() {
                libc::free((*fmt).decimalPoint as *mut libc::c_void);
            }
            (*fmt).decimalPoint = chosen;
        }
        1 => {
            if !(*fmt).grouping.is_null() {
                libc::free((*fmt).grouping as *mut libc::c_void);
            }
            (*fmt).grouping = chosen;
        }
        2 => {
            if !(*fmt).infinity.is_null() {
                libc::free((*fmt).infinity as *mut libc::c_void);
            }
            (*fmt).infinity = chosen;
        }
        3 => {
            if !(*fmt).minusSign.is_null() {
                libc::free((*fmt).minusSign as *mut libc::c_void);
            }
            (*fmt).minusSign = chosen;
        }
        4 => {
            if !(*fmt).noNumber.is_null() {
                libc::free((*fmt).noNumber as *mut libc::c_void);
            }
            (*fmt).noNumber = chosen;
        }
        5 => {
            if !(*fmt).percent.is_null() {
                libc::free((*fmt).percent as *mut libc::c_void);
            }
            (*fmt).percent = chosen;
        }
        6 => {
            if !(*fmt).permille.is_null() {
                libc::free((*fmt).permille as *mut libc::c_void);
            }
            (*fmt).permille = chosen;
        }
        7 => {
            if !(*fmt).zeroDigit.is_null() {
                libc::free((*fmt).zeroDigit as *mut libc::c_void);
            }
            (*fmt).zeroDigit = chosen;
        }
        8 => {
            if !(*fmt).digit.is_null() {
                libc::free((*fmt).digit as *mut libc::c_void);
            }
            (*fmt).digit = chosen;
        }
        _ => {
            if !(*fmt).patternSeparator.is_null() {
                libc::free((*fmt).patternSeparator as *mut libc::c_void);
            }
            (*fmt).patternSeparator = chosen;
        }
    }
}

/// Allocate a NUL-terminated string.
unsafe fn alloc_str(bytes: &[u8]) -> *mut xmlChar {
    let p = libc::malloc(bytes.len() + 1) as *mut xmlChar;
    if p.is_null() {
        return ptr::null_mut();
    }
    libc::memcpy(
        p as *mut libc::c_void,
        bytes.as_ptr() as *const libc::c_void,
        bytes.len(),
    );
    *p.add(bytes.len()) = 0;
    p
}

/// Compile an `xsl:namespace-alias` element.
///
/// # SAFETY
///
/// - All pointers must be valid.
unsafe fn compile_namespace_alias(style: *mut _xsltStylesheet, inst: *mut _xmlNode) {
    let stylesheet_prefix = get_prop(inst, c"stylesheet-prefix".as_ptr() as *const xmlChar);
    let result_prefix = get_prop(inst, c"result-prefix".as_ptr() as *const xmlChar);
    if stylesheet_prefix.is_null() || result_prefix.is_null() {
        if !stylesheet_prefix.is_null() {
            libc::free(stylesheet_prefix as *mut libc::c_void);
        }
        if !result_prefix.is_null() {
            libc::free(result_prefix as *mut libc::c_void);
        }
        return;
    }
    // Resolve prefixes to namespace URIs via the instruction's nsDef.
    let style_ns = prefix_to_uri(inst, stylesheet_prefix);
    let result_ns = prefix_to_uri(inst, result_prefix);
    libc::free(stylesheet_prefix as *mut libc::c_void);
    libc::free(result_prefix as *mut libc::c_void);
    if style_ns.is_null() || result_ns.is_null() {
        if !style_ns.is_null() {
            libc::free(style_ns as *mut libc::c_void);
        }
        if !result_ns.is_null() {
            libc::free(result_ns as *mut libc::c_void);
        }
        return;
    }
    let alias = libc::calloc(1, core::mem::size_of::<_xsltNsAlias>()) as *mut _xsltNsAlias;
    if alias.is_null() {
        libc::free(style_ns as *mut libc::c_void);
        libc::free(result_ns as *mut libc::c_void);
        return;
    }
    (*alias).styleNs = style_ns;
    (*alias).resultNs = result_ns;
    (*alias).next = (*style).nsAliases as *mut _xsltNsAlias;
    (*style).nsAliases = alias as *mut c_void;
}

/// Resolve a namespace prefix to a URI using the node's namespace
/// declarations. Returns a heap-allocated string or NULL.
///
/// # SAFETY
///
/// - All pointers must be valid.
unsafe fn prefix_to_uri(node: *mut _xmlNode, prefix: *const xmlChar) -> *mut xmlChar {
    if node.is_null() || prefix.is_null() {
        return ptr::null_mut();
    }
    let mut ns = (*node).nsDef;
    while !ns.is_null() {
        let ns_prefix = (*ns).prefix;
        let prefix_matches = if ns_prefix.is_null() {
            *prefix == 0
        } else {
            libc::strcmp(
                ns_prefix as *const libc::c_char,
                prefix as *const libc::c_char,
            ) == 0
        };
        if prefix_matches && !(*ns).href.is_null() {
            return alloc_str(core::slice::from_raw_parts(
                (*ns).href,
                libc::strlen((*ns).href as *const libc::c_char) as usize,
            ));
        }
        ns = (*ns).next;
    }
    ptr::null_mut()
}

/// Compile an `xsl:attribute-set` element.
///
/// # SAFETY
///
/// - All pointers must be valid.
unsafe fn compile_attribute_set(style: *mut _xsltStylesheet, inst: *mut _xmlNode, depth: c_int) {
    let name = get_prop(inst, c"name".as_ptr() as *const xmlChar);
    if name.is_null() {
        return;
    }
    let set = libc::calloc(1, core::mem::size_of::<_xsltAttrSet>()) as *mut _xsltAttrSet;
    if set.is_null() {
        libc::free(name as *mut libc::c_void);
        return;
    }
    (*set).name = name;
    (*set).inst = inst;
    (*set).style = style;
    (*set).depth = depth;
    (*set).next = (*style).attributeSets as *mut _xsltAttrSet;
    (*style).attributeSets = set as *mut c_void;
}

/// Compile `xsl:strip-space` or `xsl:preserve-space` elements.
///
/// # SAFETY
///
/// - All pointers must be valid.
unsafe fn compile_space_rules(
    style: *mut _xsltStylesheet,
    inst: *mut _xmlNode,
    strip: c_int,
    depth: c_int,
) {
    let elements = get_prop(inst, c"elements".as_ptr() as *const xmlChar);
    if elements.is_null() {
        return;
    }
    // Split on whitespace.
    let bytes = core::slice::from_raw_parts(
        elements,
        libc::strlen(elements as *const libc::c_char) as usize,
    );
    for name in bytes
        .split(|b| *b == b' ' || *b == b'\t' || *b == b'\n' || *b == b'\r')
        .filter(|s| !s.is_empty())
    {
        let mut cname = name.to_vec();
        cname.push(0);
        let entry = libc::calloc(
            1,
            core::mem::size_of::<crate::xslt::whitespace::_xsltStripSpace>(),
        ) as *mut crate::xslt::whitespace::_xsltStripSpace;
        if entry.is_null() {
            continue;
        }
        (*entry).name = alloc_str(&cname);
        (*entry).depth = depth;
        if strip != 0 {
            (*entry).next = (*style).stripSpaces as *mut crate::xslt::whitespace::_xsltStripSpace;
            (*style).stripSpaces = entry as *mut c_void;
        } else {
            // UPSTREAM-PARITY: upstream keeps only a stripSpaces hash + a
            // stripAll flag; the candidate's preserve-list head is carried in
            // the unused nsDefs void* slot (documented divergence).
            (*entry).next = (*style).nsDefs as *mut crate::xslt::whitespace::_xsltStripSpace;
            (*style).nsDefs = entry as *mut c_void;
        }
    }
    libc::free(elements as *mut libc::c_void);
}

/// Compile the `xsl:output` element into the stylesheet's output settings.
///
/// # SAFETY
///
/// - All pointers must be valid.
unsafe fn compile_output(style: *mut _xsltStylesheet, inst: *mut _xmlNode) {
    let method = get_prop(inst, c"method".as_ptr() as *const xmlChar);
    if !method.is_null() {
        (*style).method = method;
    }
    let version = get_prop(inst, c"version".as_ptr() as *const xmlChar);
    if !version.is_null() {
        (*style).version = version;
    }
    let encoding = get_prop(inst, c"encoding".as_ptr() as *const xmlChar);
    if !encoding.is_null() {
        (*style).encoding = encoding;
    }
    let omit = get_prop(inst, c"omit-xml-declaration".as_ptr() as *const xmlChar);
    if !omit.is_null() {
        (*style).omitXmlDeclaration = if cstr_eq(omit, b"yes") { 1 } else { 0 };
        libc::free(omit as *mut libc::c_void);
    }
    let standalone = get_prop(inst, c"standalone".as_ptr() as *const xmlChar);
    if !standalone.is_null() {
        (*style).standalone = if cstr_eq(standalone, b"yes") {
            1
        } else if cstr_eq(standalone, b"no") {
            0
        } else {
            -1
        };
        libc::free(standalone as *mut libc::c_void);
    }
    let indent = get_prop(inst, c"indent".as_ptr() as *const xmlChar);
    if !indent.is_null() {
        (*style).indent = if cstr_eq(indent, b"yes") { 1 } else { 0 };
        libc::free(indent as *mut libc::c_void);
    }
    let doctype_public = get_prop(inst, c"doctype-public".as_ptr() as *const xmlChar);
    if !doctype_public.is_null() {
        (*style).doctypePublic = doctype_public;
    }
    let doctype_system = get_prop(inst, c"doctype-system".as_ptr() as *const xmlChar);
    if !doctype_system.is_null() {
        (*style).doctypeSystem = doctype_system;
    }
    let media_type = get_prop(inst, c"media-type".as_ptr() as *const xmlChar);
    if !media_type.is_null() {
        (*style).mediaType = media_type;
    }
}

/// Compare a C string with a byte literal.
unsafe fn cstr_eq(s: *const xmlChar, expected: &[u8]) -> bool {
    if s.is_null() {
        return false;
    }
    let len = libc::strlen(s as *const libc::c_char) as usize;
    if len != expected.len() {
        return false;
    }
    let bytes = core::slice::from_raw_parts(s, len);
    bytes == expected
}

/// Move templates from an included stylesheet to the including one.
///
/// # SAFETY
///
/// - All pointers must be valid.
unsafe fn move_templates(src: *mut _xsltStylesheet, dst: *mut _xsltStylesheet) {
    let mut cur = (*src).templates;
    while !cur.is_null() {
        let next = (*cur).next;
        (*cur).style = dst;
        add_template_to_style(dst, cur);
        cur = next;
    }
    (*src).templates = ptr::null_mut();
}

/// Merge strip/preserve-space rules from an included stylesheet.
///
/// # SAFETY
///
/// - All pointers must be valid.
unsafe fn merge_strip_spaces(src: *mut _xsltStylesheet, dst: *mut _xsltStylesheet) {
    let mut cur = (*src).stripSpaces as *mut crate::xslt::whitespace::_xsltStripSpace;
    while !cur.is_null() {
        let next = (*cur).next;
        (*cur).next = (*dst).stripSpaces as *mut crate::xslt::whitespace::_xsltStripSpace;
        (*dst).stripSpaces = cur as *mut c_void;
        cur = next;
    }
    (*src).stripSpaces = ptr::null_mut();
    let mut cur = (*src).nsDefs as *mut crate::xslt::whitespace::_xsltStripSpace;
    while !cur.is_null() {
        let next = (*cur).next;
        (*cur).next = (*dst).nsDefs as *mut crate::xslt::whitespace::_xsltStripSpace;
        (*dst).nsDefs = cur as *mut c_void;
        cur = next;
    }
    (*src).nsDefs = ptr::null_mut();
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::xml::tree::*;
    use core::ptr;

    /// Verify that `compile` with NULL arguments returns `-1`.
    ///
    /// # Safety
    ///
    /// - `compile` rejects NULL `style`/`doc` before any dereference, so
    ///   passing `ptr::null_mut()` for both is safe and no raw pointer is
    ///   read inside the unsafe block.
    #[test]
    fn test_compile_null() {
        unsafe {
            assert_eq!(compile(ptr::null_mut(), ptr::null_mut()), -1);
        }
    }

    /// Verify the element/namespace helpers tolerate NULL nodes.
    ///
    /// # Safety
    ///
    /// - `is_xslt_element`, `is_xslt_namespace`, `get_element_ns`, and
    ///   `get_element_name` all return early on a NULL `node` (or NULL
    ///   `ns`/`name` fields) before dereferencing, so passing
    ///   `ptr::null_mut()` is safe and reads no memory.
    #[test]
    fn test_is_xslt_element_null() {
        unsafe {
            assert!(!is_xslt_element(ptr::null_mut(), "template"));
            assert!(!is_xslt_namespace(ptr::null_mut()));
            assert!(get_element_ns(ptr::null_mut()).is_none());
            assert!(get_element_name(ptr::null_mut()).is_none());
        }
    }

    /// Compile a simplified stylesheet (literal result element) and check
    /// the implicit template wrapping the root element.
    ///
    /// # Safety
    ///
    /// - `doc` and `root` are live nodes created by `new_doc`/`new_node`
    ///   and linked with `doc_set_root_element`; `style` is a zeroed
    ///   `_xsltStylesheet` from `libc::calloc`, both required by `compile`.
    /// - `compile` stores `doc` into `style->doc`, so `xsltFreeStylesheet`
    ///   (which owns the stylesheet document) is the single release path;
    ///   the templates list it walks is valid after compilation.
    #[test]
    fn test_compile_simplified_stylesheet() {
        unsafe {
            // A simplified stylesheet: literal result element <html> with the
            // required xsl:version attribute (upstream xslt.c
            // xsltParseStylesheetProcess rejects simplified roots without it).
            let xsl = b"<?xml version=\"1.0\"?><html xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xsl:version=\"1.0\"><body></body></html>\0";
            let doc = crate::abi::exports_xml2::xmlReadMemory(
                xsl.as_ptr() as *const c_char,
                (xsl.len() - 1) as c_int,
                ptr::null(),
                ptr::null(),
                0,
            );
            assert!(!doc.is_null());
            let root = doc_get_root_element(doc);
            assert!(!root.is_null());
            let style =
                libc::calloc(1, core::mem::size_of::<_xsltStylesheet>()) as *mut _xsltStylesheet;
            let ret = compile(style, doc);
            assert_eq!(ret, 0);
            assert_eq!((*style).doc, doc);
            assert!(!(*style).templates.is_null());
            // The implicit template's content is the root element.
            assert_eq!((*(*style).templates).content, root);
            crate::xslt::stylesheet::xsltFreeStylesheet(style);
        }
    }

    /// A malformed `select` on `xsl:value-of` is reported at compile time
    /// and increments `style->errors`.
    ///
    /// # Safety
    ///
    /// - The `xsl` byte string is a valid NUL-terminated buffer passed to
    ///   `xmlReadMemory`, which returns a valid document (asserted
    ///   non-NULL) or NULL that `compile` rejects.
    /// - `style` is a zeroed `_xsltStylesheet` from `libc::calloc` as
    ///   required by `compile`; `xsltFreeStylesheet` releases `style` and
    ///   the stylesheet document it owns.
    #[test]
    fn test_compile_bad_select_reports_error() {
        // UPSTREAM-PARITY (preproc.c xsltPreCompute): a malformed select on
        // xsl:value-of is reported at stylesheet-compile time and increments
        // style->errors (xsltproc exits 5).
        unsafe {
            let xsl = b"<?xml version=\"1.0\"?>\n\
            <xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n\
              <xsl:template match=\"/\">\n\
                <xsl:value-of select=\"number(&apos;x&apos;\"/>\n\
              </xsl:template>\n\
            </xsl:stylesheet>\0";
            let doc = crate::abi::exports_xml2::xmlReadMemory(
                xsl.as_ptr() as *const c_char,
                (xsl.len() - 1) as c_int,
                c"bad.xsl".as_ptr() as *const c_char,
                ptr::null(),
                0,
            );
            assert!(!doc.is_null());
            let style =
                libc::calloc(1, core::mem::size_of::<_xsltStylesheet>()) as *mut _xsltStylesheet;
            let ret = compile(style, doc);
            assert_eq!(ret, 0);
            assert!(
                (*style).errors > 0,
                "malformed select must increment style->errors (got {})",
                (*style).errors
            );
            // xsltFreeStylesheet owns the stylesheet document.
            crate::xslt::stylesheet::xsltFreeStylesheet(style);
        }
    }

    /// An `xsl:sort` outside `apply-templates`/`for-each` is reported at
    /// compile time and increments `style->errors`.
    ///
    /// # Safety
    ///
    /// - The `xsl` byte string is a valid NUL-terminated buffer passed to
    ///   `xmlReadMemory`, which returns a valid document (asserted
    ///   non-NULL) or NULL that `compile` rejects.
    /// - `style` is a zeroed `_xsltStylesheet` from `libc::calloc` as
    ///   required by `compile`; `xsltFreeStylesheet` releases `style` and
    ///   the stylesheet document it owns.
    #[test]
    fn test_compile_sort_outside_context_reports_error() {
        // UPSTREAM-PARITY (preproc.c xsltCheckParentElement): xsl:sort
        // outside xsl:apply-templates/xsl:for-each reports "element sort is
        // not allowed within that context" and increments style->errors.
        unsafe {
            let xsl = b"<?xml version=\"1.0\"?>\n\
            <xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n\
              <xsl:template match=\"/\">\n\
                <xsl:sort select=\"title\"/>\n\
              </xsl:template>\n\
            </xsl:stylesheet>\0";
            let doc = crate::abi::exports_xml2::xmlReadMemory(
                xsl.as_ptr() as *const c_char,
                (xsl.len() - 1) as c_int,
                c"bad.xsl".as_ptr() as *const c_char,
                ptr::null(),
                0,
            );
            assert!(!doc.is_null());
            let style =
                libc::calloc(1, core::mem::size_of::<_xsltStylesheet>()) as *mut _xsltStylesheet;
            let ret = compile(style, doc);
            assert_eq!(ret, 0);
            assert!(
                (*style).errors > 0,
                "sort outside apply-templates/for-each must be an error"
            );
            // xsltFreeStylesheet owns the stylesheet document.
            crate::xslt::stylesheet::xsltFreeStylesheet(style);
        }
    }
}