xslt-processor 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.mts CHANGED
@@ -581,6 +581,11 @@ declare class Xslt {
581
581
  * preserve-space takes precedence over strip-space for conflicting patterns.
582
582
  */
583
583
  preserveSpacePatterns: string[];
584
+ /**
585
+ * Namespace aliases from xsl:namespace-alias declarations.
586
+ * Maps stylesheet namespace prefixes to result namespace prefixes.
587
+ */
588
+ namespaceAliases: Map<string, string>;
584
589
  constructor(options?: Partial<XsltOptions>);
585
590
  /**
586
591
  * The exported entry point of the XSL-T processor.
@@ -705,6 +710,86 @@ declare class Xslt {
705
710
  * @param template The template.
706
711
  */
707
712
  protected xsltKey(context: ExprContext, template: XNode): void;
713
+ /**
714
+ * Implements `xsl:message`.
715
+ * Outputs a message to the console. If terminate="yes", throws an error to stop processing.
716
+ * @param context The Expression Context.
717
+ * @param template The `<xsl:message>` node.
718
+ */
719
+ protected xsltMessage(context: ExprContext, template: XNode): Promise<void>;
720
+ /**
721
+ * Implements `xsl:namespace-alias`.
722
+ * Declares that a namespace URI in the stylesheet should be replaced by a different
723
+ * namespace URI in the output.
724
+ * @param template The `<xsl:namespace-alias>` node.
725
+ */
726
+ protected xsltNamespaceAlias(template: XNode): void;
727
+ /**
728
+ * Implements `xsl:number`.
729
+ * Inserts a formatted number into the result tree.
730
+ * @param context The Expression Context.
731
+ * @param template The `<xsl:number>` node.
732
+ * @param output The output node.
733
+ */
734
+ protected xsltNumber(context: ExprContext, template: XNode, output?: XNode): void;
735
+ /**
736
+ * Counts nodes for xsl:number based on level, count, and from attributes.
737
+ * @param context The Expression Context.
738
+ * @param level The counting level: 'single', 'multiple', or 'any'.
739
+ * @param count Pattern to match nodes to count.
740
+ * @param from Pattern to start counting from.
741
+ * @returns The count value.
742
+ */
743
+ protected xsltNumberCount(context: ExprContext, level: string, count: string | null, from: string | null): number;
744
+ /**
745
+ * Checks if a node matches a simple name pattern.
746
+ * @param node The node to check.
747
+ * @param pattern The pattern (node name) to match.
748
+ * @returns True if the node matches.
749
+ */
750
+ protected nodeMatchesPattern(node: XNode, pattern: string): boolean;
751
+ /**
752
+ * Gets all nodes preceding the given node in document order.
753
+ * @param node The reference node.
754
+ * @returns Array of preceding nodes.
755
+ */
756
+ protected getAllPrecedingNodes(node: XNode): XNode[];
757
+ /**
758
+ * Collects all descendant nodes of a given node.
759
+ * @param node The parent node.
760
+ * @param result The array to collect into.
761
+ */
762
+ protected collectDescendants(node: XNode, result: XNode[]): void;
763
+ /**
764
+ * Formats a number according to the format string.
765
+ * @param number The number to format.
766
+ * @param format The format string (e.g., "1", "01", "a", "A", "i", "I").
767
+ * @param groupingSeparator Optional grouping separator.
768
+ * @param groupingSize Optional grouping size.
769
+ * @returns The formatted number string.
770
+ */
771
+ protected xsltFormatNumber(number: number, format: string, groupingSeparator: string | null, groupingSize: string | null): string;
772
+ /**
773
+ * Converts a number to alphabetic representation.
774
+ * @param number The number to convert.
775
+ * @param uppercase Whether to use uppercase letters.
776
+ * @returns The alphabetic representation.
777
+ */
778
+ protected numberToAlpha(number: number, uppercase: boolean): string;
779
+ /**
780
+ * Converts a number to Roman numeral representation.
781
+ * @param number The number to convert.
782
+ * @returns The Roman numeral string.
783
+ */
784
+ protected numberToRoman(number: number): string;
785
+ /**
786
+ * Applies grouping separators to a numeric string.
787
+ * @param numStr The numeric string.
788
+ * @param separator The grouping separator.
789
+ * @param size The grouping size.
790
+ * @returns The grouped string.
791
+ */
792
+ protected applyGrouping(numStr: string, separator: string, size: number): string;
708
793
  /**
709
794
  * Orders the current node list in the input context according to the
710
795
  * sort order specified by xsl:sort child nodes of the current
package/index.d.ts CHANGED
@@ -581,6 +581,11 @@ declare class Xslt {
581
581
  * preserve-space takes precedence over strip-space for conflicting patterns.
582
582
  */
583
583
  preserveSpacePatterns: string[];
584
+ /**
585
+ * Namespace aliases from xsl:namespace-alias declarations.
586
+ * Maps stylesheet namespace prefixes to result namespace prefixes.
587
+ */
588
+ namespaceAliases: Map<string, string>;
584
589
  constructor(options?: Partial<XsltOptions>);
585
590
  /**
586
591
  * The exported entry point of the XSL-T processor.
@@ -705,6 +710,86 @@ declare class Xslt {
705
710
  * @param template The template.
706
711
  */
707
712
  protected xsltKey(context: ExprContext, template: XNode): void;
713
+ /**
714
+ * Implements `xsl:message`.
715
+ * Outputs a message to the console. If terminate="yes", throws an error to stop processing.
716
+ * @param context The Expression Context.
717
+ * @param template The `<xsl:message>` node.
718
+ */
719
+ protected xsltMessage(context: ExprContext, template: XNode): Promise<void>;
720
+ /**
721
+ * Implements `xsl:namespace-alias`.
722
+ * Declares that a namespace URI in the stylesheet should be replaced by a different
723
+ * namespace URI in the output.
724
+ * @param template The `<xsl:namespace-alias>` node.
725
+ */
726
+ protected xsltNamespaceAlias(template: XNode): void;
727
+ /**
728
+ * Implements `xsl:number`.
729
+ * Inserts a formatted number into the result tree.
730
+ * @param context The Expression Context.
731
+ * @param template The `<xsl:number>` node.
732
+ * @param output The output node.
733
+ */
734
+ protected xsltNumber(context: ExprContext, template: XNode, output?: XNode): void;
735
+ /**
736
+ * Counts nodes for xsl:number based on level, count, and from attributes.
737
+ * @param context The Expression Context.
738
+ * @param level The counting level: 'single', 'multiple', or 'any'.
739
+ * @param count Pattern to match nodes to count.
740
+ * @param from Pattern to start counting from.
741
+ * @returns The count value.
742
+ */
743
+ protected xsltNumberCount(context: ExprContext, level: string, count: string | null, from: string | null): number;
744
+ /**
745
+ * Checks if a node matches a simple name pattern.
746
+ * @param node The node to check.
747
+ * @param pattern The pattern (node name) to match.
748
+ * @returns True if the node matches.
749
+ */
750
+ protected nodeMatchesPattern(node: XNode, pattern: string): boolean;
751
+ /**
752
+ * Gets all nodes preceding the given node in document order.
753
+ * @param node The reference node.
754
+ * @returns Array of preceding nodes.
755
+ */
756
+ protected getAllPrecedingNodes(node: XNode): XNode[];
757
+ /**
758
+ * Collects all descendant nodes of a given node.
759
+ * @param node The parent node.
760
+ * @param result The array to collect into.
761
+ */
762
+ protected collectDescendants(node: XNode, result: XNode[]): void;
763
+ /**
764
+ * Formats a number according to the format string.
765
+ * @param number The number to format.
766
+ * @param format The format string (e.g., "1", "01", "a", "A", "i", "I").
767
+ * @param groupingSeparator Optional grouping separator.
768
+ * @param groupingSize Optional grouping size.
769
+ * @returns The formatted number string.
770
+ */
771
+ protected xsltFormatNumber(number: number, format: string, groupingSeparator: string | null, groupingSize: string | null): string;
772
+ /**
773
+ * Converts a number to alphabetic representation.
774
+ * @param number The number to convert.
775
+ * @param uppercase Whether to use uppercase letters.
776
+ * @returns The alphabetic representation.
777
+ */
778
+ protected numberToAlpha(number: number, uppercase: boolean): string;
779
+ /**
780
+ * Converts a number to Roman numeral representation.
781
+ * @param number The number to convert.
782
+ * @returns The Roman numeral string.
783
+ */
784
+ protected numberToRoman(number: number): string;
785
+ /**
786
+ * Applies grouping separators to a numeric string.
787
+ * @param numStr The numeric string.
788
+ * @param separator The grouping separator.
789
+ * @param size The grouping size.
790
+ * @returns The grouped string.
791
+ */
792
+ protected applyGrouping(numStr: string, separator: string, size: number): string;
708
793
  /**
709
794
  * Orders the current node list in the input context according to the
710
795
  * sort order specified by xsl:sort child nodes of the current
package/index.js CHANGED
@@ -3550,6 +3550,7 @@ var Xslt = class {
3550
3550
  this.outputOmitXmlDeclaration = "no";
3551
3551
  this.stripSpacePatterns = [];
3552
3552
  this.preserveSpacePatterns = [];
3553
+ this.namespaceAliases = /* @__PURE__ */ new Map();
3553
3554
  this.decimalFormatSettings = {
3554
3555
  decimalSeparator: ".",
3555
3556
  groupingSeparator: ",",
@@ -3667,13 +3668,16 @@ var Xslt = class {
3667
3668
  this.xsltKey(context, template);
3668
3669
  break;
3669
3670
  case "message":
3670
- throw new Error(`not implemented: ${template.localName}`);
3671
+ yield this.xsltMessage(context, template);
3672
+ break;
3671
3673
  case "namespace-alias":
3672
- throw new Error(`not implemented: ${template.localName}`);
3674
+ this.xsltNamespaceAlias(template);
3675
+ break;
3673
3676
  case "number":
3674
- throw new Error(`not implemented: ${template.localName}`);
3677
+ this.xsltNumber(context, template, output);
3678
+ break;
3675
3679
  case "otherwise":
3676
- throw new Error(`xsl:otherwise can't be used outside of xsl:choose.`);
3680
+ throw new Error(`<xsl:otherwise> must be a child of <xsl:choose>.`);
3677
3681
  case "output":
3678
3682
  this.outputMethod = xmlGetAttribute(template, "method");
3679
3683
  this.outputOmitXmlDeclaration = xmlGetAttribute(template, "omit-xml-declaration");
@@ -3709,9 +3713,9 @@ var Xslt = class {
3709
3713
  yield this.xsltVariable(context, template, true);
3710
3714
  break;
3711
3715
  case "when":
3712
- throw new Error(`xsl:when can't be used outside of xsl:choose.`);
3716
+ throw new Error(`<xsl:when> must be a child of <xsl:choose>.`);
3713
3717
  case "with-param":
3714
- throw new Error(`error if here: ${template.localName}`);
3718
+ throw new Error(`<xsl:with-param> must be a child of <xsl:call-template> or <xsl:apply-templates>.`);
3715
3719
  default:
3716
3720
  throw new Error(`error if here: ${template.localName}`);
3717
3721
  }
@@ -4087,6 +4091,281 @@ var Xslt = class {
4087
4091
  context.keys[name][attributeValue] = new NodeSetValue([node]);
4088
4092
  }
4089
4093
  }
4094
+ /**
4095
+ * Implements `xsl:message`.
4096
+ * Outputs a message to the console. If terminate="yes", throws an error to stop processing.
4097
+ * @param context The Expression Context.
4098
+ * @param template The `<xsl:message>` node.
4099
+ */
4100
+ xsltMessage(context, template) {
4101
+ return __async(this, null, function* () {
4102
+ const documentFragment = domCreateDocumentFragment(this.outputDocument);
4103
+ yield this.xsltChildNodes(context, template, documentFragment);
4104
+ const messageText = xmlValue(documentFragment);
4105
+ const terminate = xmlGetAttribute(template, "terminate") || "no";
4106
+ console.log(`[xsl:message] ${messageText}`);
4107
+ if (terminate === "yes") {
4108
+ throw new Error(`xsl:message terminated: ${messageText}`);
4109
+ }
4110
+ });
4111
+ }
4112
+ /**
4113
+ * Implements `xsl:namespace-alias`.
4114
+ * Declares that a namespace URI in the stylesheet should be replaced by a different
4115
+ * namespace URI in the output.
4116
+ * @param template The `<xsl:namespace-alias>` node.
4117
+ */
4118
+ xsltNamespaceAlias(template) {
4119
+ const stylesheetPrefix = xmlGetAttribute(template, "stylesheet-prefix");
4120
+ const resultPrefix = xmlGetAttribute(template, "result-prefix");
4121
+ if (!stylesheetPrefix || !resultPrefix) {
4122
+ throw new Error("<xsl:namespace-alias> requires both stylesheet-prefix and result-prefix attributes.");
4123
+ }
4124
+ this.namespaceAliases.set(stylesheetPrefix, resultPrefix);
4125
+ }
4126
+ /**
4127
+ * Implements `xsl:number`.
4128
+ * Inserts a formatted number into the result tree.
4129
+ * @param context The Expression Context.
4130
+ * @param template The `<xsl:number>` node.
4131
+ * @param output The output node.
4132
+ */
4133
+ xsltNumber(context, template, output) {
4134
+ const value = xmlGetAttribute(template, "value");
4135
+ const level = xmlGetAttribute(template, "level") || "single";
4136
+ const count = xmlGetAttribute(template, "count");
4137
+ const from = xmlGetAttribute(template, "from");
4138
+ const format = xmlGetAttribute(template, "format") || "1";
4139
+ const lang = xmlGetAttribute(template, "lang");
4140
+ const letterValue = xmlGetAttribute(template, "letter-value");
4141
+ const groupingSeparator = xmlGetAttribute(template, "grouping-separator");
4142
+ const groupingSize = xmlGetAttribute(template, "grouping-size");
4143
+ let number;
4144
+ if (value) {
4145
+ const result = this.xPath.xPathEval(value, context);
4146
+ number = Math.round(result.numberValue());
4147
+ } else {
4148
+ number = this.xsltNumberCount(context, level, count, from);
4149
+ }
4150
+ const formattedNumber = this.xsltFormatNumber(number, format, groupingSeparator, groupingSize);
4151
+ const textNode = domCreateTextNode(this.outputDocument, formattedNumber);
4152
+ const targetOutput = output || this.outputDocument;
4153
+ textNode.siblingPosition = targetOutput.childNodes.length;
4154
+ domAppendChild(targetOutput, textNode);
4155
+ }
4156
+ /**
4157
+ * Counts nodes for xsl:number based on level, count, and from attributes.
4158
+ * @param context The Expression Context.
4159
+ * @param level The counting level: 'single', 'multiple', or 'any'.
4160
+ * @param count Pattern to match nodes to count.
4161
+ * @param from Pattern to start counting from.
4162
+ * @returns The count value.
4163
+ */
4164
+ xsltNumberCount(context, level, count, from) {
4165
+ const currentNode = context.nodeList[context.position];
4166
+ const countPattern = count || currentNode.nodeName;
4167
+ switch (level) {
4168
+ case "single": {
4169
+ let num = 1;
4170
+ let sibling = currentNode.previousSibling;
4171
+ while (sibling) {
4172
+ if (sibling.nodeType === currentNode.nodeType) {
4173
+ if (this.nodeMatchesPattern(sibling, countPattern)) {
4174
+ num++;
4175
+ }
4176
+ }
4177
+ sibling = sibling.previousSibling;
4178
+ }
4179
+ return num;
4180
+ }
4181
+ case "multiple": {
4182
+ let num = 1;
4183
+ let sibling = currentNode.previousSibling;
4184
+ while (sibling) {
4185
+ if (sibling.nodeType === currentNode.nodeType) {
4186
+ if (this.nodeMatchesPattern(sibling, countPattern)) {
4187
+ num++;
4188
+ }
4189
+ }
4190
+ sibling = sibling.previousSibling;
4191
+ }
4192
+ return num;
4193
+ }
4194
+ case "any": {
4195
+ let num = 1;
4196
+ const allNodes = this.getAllPrecedingNodes(currentNode);
4197
+ for (const node of allNodes) {
4198
+ if (this.nodeMatchesPattern(node, countPattern)) {
4199
+ num++;
4200
+ }
4201
+ }
4202
+ return num;
4203
+ }
4204
+ default:
4205
+ return 1;
4206
+ }
4207
+ }
4208
+ /**
4209
+ * Checks if a node matches a simple name pattern.
4210
+ * @param node The node to check.
4211
+ * @param pattern The pattern (node name) to match.
4212
+ * @returns True if the node matches.
4213
+ */
4214
+ nodeMatchesPattern(node, pattern) {
4215
+ if (pattern === "*") {
4216
+ return node.nodeType === DOM_ELEMENT_NODE;
4217
+ }
4218
+ return node.nodeName === pattern || node.localName === pattern;
4219
+ }
4220
+ /**
4221
+ * Gets all nodes preceding the given node in document order.
4222
+ * @param node The reference node.
4223
+ * @returns Array of preceding nodes.
4224
+ */
4225
+ getAllPrecedingNodes(node) {
4226
+ const result = [];
4227
+ let sibling = node.previousSibling;
4228
+ while (sibling) {
4229
+ result.push(sibling);
4230
+ this.collectDescendants(sibling, result);
4231
+ sibling = sibling.previousSibling;
4232
+ }
4233
+ let parent = node.parentNode;
4234
+ while (parent) {
4235
+ let parentSibling = parent.previousSibling;
4236
+ while (parentSibling) {
4237
+ result.push(parentSibling);
4238
+ this.collectDescendants(parentSibling, result);
4239
+ parentSibling = parentSibling.previousSibling;
4240
+ }
4241
+ parent = parent.parentNode;
4242
+ }
4243
+ return result;
4244
+ }
4245
+ /**
4246
+ * Collects all descendant nodes of a given node.
4247
+ * @param node The parent node.
4248
+ * @param result The array to collect into.
4249
+ */
4250
+ collectDescendants(node, result) {
4251
+ for (const child of node.childNodes) {
4252
+ if (child.nodeType === DOM_ELEMENT_NODE) {
4253
+ result.push(child);
4254
+ this.collectDescendants(child, result);
4255
+ }
4256
+ }
4257
+ }
4258
+ /**
4259
+ * Formats a number according to the format string.
4260
+ * @param number The number to format.
4261
+ * @param format The format string (e.g., "1", "01", "a", "A", "i", "I").
4262
+ * @param groupingSeparator Optional grouping separator.
4263
+ * @param groupingSize Optional grouping size.
4264
+ * @returns The formatted number string.
4265
+ */
4266
+ xsltFormatNumber(number, format, groupingSeparator, groupingSize) {
4267
+ const formatChar = format.charAt(0);
4268
+ let result;
4269
+ switch (formatChar) {
4270
+ case "1":
4271
+ result = number.toString();
4272
+ if (format.length > 1 && format.match(/^0+1$/)) {
4273
+ const width = format.length;
4274
+ result = number.toString().padStart(width, "0");
4275
+ }
4276
+ break;
4277
+ case "a":
4278
+ result = this.numberToAlpha(number, false);
4279
+ break;
4280
+ case "A":
4281
+ result = this.numberToAlpha(number, true);
4282
+ break;
4283
+ case "i":
4284
+ result = this.numberToRoman(number).toLowerCase();
4285
+ break;
4286
+ case "I":
4287
+ result = this.numberToRoman(number);
4288
+ break;
4289
+ default:
4290
+ result = number.toString();
4291
+ }
4292
+ if (groupingSeparator && groupingSize) {
4293
+ const size = parseInt(groupingSize, 10);
4294
+ if (size > 0 && !isNaN(size)) {
4295
+ result = this.applyGrouping(result, groupingSeparator, size);
4296
+ }
4297
+ }
4298
+ return result;
4299
+ }
4300
+ /**
4301
+ * Converts a number to alphabetic representation.
4302
+ * @param number The number to convert.
4303
+ * @param uppercase Whether to use uppercase letters.
4304
+ * @returns The alphabetic representation.
4305
+ */
4306
+ numberToAlpha(number, uppercase) {
4307
+ if (number <= 0) return "";
4308
+ let result = "";
4309
+ while (number > 0) {
4310
+ number--;
4311
+ result = String.fromCharCode(number % 26 + (uppercase ? 65 : 97)) + result;
4312
+ number = Math.floor(number / 26);
4313
+ }
4314
+ return result;
4315
+ }
4316
+ /**
4317
+ * Converts a number to Roman numeral representation.
4318
+ * @param number The number to convert.
4319
+ * @returns The Roman numeral string.
4320
+ */
4321
+ numberToRoman(number) {
4322
+ if (number <= 0 || number > 3999) return number.toString();
4323
+ const romanNumerals = [
4324
+ [1e3, "M"],
4325
+ [900, "CM"],
4326
+ [500, "D"],
4327
+ [400, "CD"],
4328
+ [100, "C"],
4329
+ [90, "XC"],
4330
+ [50, "L"],
4331
+ [40, "XL"],
4332
+ [10, "X"],
4333
+ [9, "IX"],
4334
+ [5, "V"],
4335
+ [4, "IV"],
4336
+ [1, "I"]
4337
+ ];
4338
+ let result = "";
4339
+ for (const [value, numeral] of romanNumerals) {
4340
+ while (number >= value) {
4341
+ result += numeral;
4342
+ number -= value;
4343
+ }
4344
+ }
4345
+ return result;
4346
+ }
4347
+ /**
4348
+ * Applies grouping separators to a numeric string.
4349
+ * @param numStr The numeric string.
4350
+ * @param separator The grouping separator.
4351
+ * @param size The grouping size.
4352
+ * @returns The grouped string.
4353
+ */
4354
+ applyGrouping(numStr, separator, size) {
4355
+ const parts = numStr.split(".");
4356
+ let intPart = parts[0];
4357
+ const decPart = parts[1];
4358
+ let result = "";
4359
+ let count = 0;
4360
+ for (let i = intPart.length - 1; i >= 0; i--) {
4361
+ if (count > 0 && count % size === 0) {
4362
+ result = separator + result;
4363
+ }
4364
+ result = intPart[i] + result;
4365
+ count++;
4366
+ }
4367
+ return decPart ? result + "." + decPart : result;
4368
+ }
4090
4369
  /**
4091
4370
  * Orders the current node list in the input context according to the
4092
4371
  * sort order specified by xsl:sort child nodes of the current