xslt-processor 4.0.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/README.md CHANGED
@@ -219,6 +219,54 @@ HTML per se is not strict XML. Because of that, starting on version 2.0.0, this
219
219
  - Tags like `<hr>`, `<link>` and `<meta>` don't need to be closed. The output for these tags doesn't close them (adding a `/` before the tag closes, or a corresponding close tag);
220
220
  - This rule doesn't apply for XHTML, which is strict XML.
221
221
 
222
+ ### Whitespace Handling
223
+
224
+ This library supports `xsl:strip-space` and `xsl:preserve-space` for controlling whitespace in the input document.
225
+
226
+ #### `xsl:strip-space`
227
+
228
+ Use `<xsl:strip-space>` to remove whitespace-only text nodes from specified elements in the input document:
229
+
230
+ ```xml
231
+ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
232
+ <!-- Strip whitespace from all elements -->
233
+ <xsl:strip-space elements="*"/>
234
+
235
+ <!-- Or strip from specific elements -->
236
+ <xsl:strip-space elements="book chapter section"/>
237
+
238
+ <!-- ... templates ... -->
239
+ </xsl:stylesheet>
240
+ ```
241
+
242
+ The `elements` attribute accepts:
243
+ - `*` - matches all elements
244
+ - `name` - matches elements with the specified local name
245
+ - `prefix:*` - matches all elements in a namespace
246
+ - `prefix:name` - matches a specific element in a namespace
247
+ - Multiple patterns separated by whitespace (e.g., `"book chapter section"`)
248
+
249
+ #### `xsl:preserve-space`
250
+
251
+ Use `<xsl:preserve-space>` to preserve whitespace in specific elements, overriding `xsl:strip-space`:
252
+
253
+ ```xml
254
+ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
255
+ <xsl:strip-space elements="*"/>
256
+ <!-- Preserve whitespace in pre and code elements -->
257
+ <xsl:preserve-space elements="pre code"/>
258
+
259
+ <!-- ... templates ... -->
260
+ </xsl:stylesheet>
261
+ ```
262
+
263
+ #### Precedence Rules
264
+
265
+ 1. `xml:space="preserve"` attribute on an element takes highest precedence
266
+ 2. `xsl:preserve-space` overrides `xsl:strip-space` for matching elements
267
+ 3. `xsl:strip-space` applies to remaining matches
268
+ 4. By default (no declarations), whitespace is preserved
269
+
222
270
  ## References
223
271
 
224
272
  - XPath Specification: http://www.w3.org/TR/1999/REC-xpath-19991116
package/index.d.mts CHANGED
@@ -570,6 +570,22 @@ declare class Xslt {
570
570
  outputOmitXmlDeclaration: string;
571
571
  version: string;
572
572
  firstTemplateRan: boolean;
573
+ /**
574
+ * List of element name patterns from xsl:strip-space declarations.
575
+ * Whitespace-only text nodes inside matching elements will be stripped.
576
+ */
577
+ stripSpacePatterns: string[];
578
+ /**
579
+ * List of element name patterns from xsl:preserve-space declarations.
580
+ * Whitespace-only text nodes inside matching elements will be preserved.
581
+ * preserve-space takes precedence over strip-space for conflicting patterns.
582
+ */
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>;
573
589
  constructor(options?: Partial<XsltOptions>);
574
590
  /**
575
591
  * The exported entry point of the XSL-T processor.
@@ -694,6 +710,86 @@ declare class Xslt {
694
710
  * @param template The template.
695
711
  */
696
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;
697
793
  /**
698
794
  * Orders the current node list in the input context according to the
699
795
  * sort order specified by xsl:sort child nodes of the current
@@ -704,6 +800,39 @@ declare class Xslt {
704
800
  * @todo case-order is not implemented.
705
801
  */
706
802
  protected xsltSort(context: ExprContext, template: XNode): void;
803
+ /**
804
+ * Implements `xsl:strip-space`.
805
+ * Collects element name patterns for which whitespace-only text nodes should be stripped.
806
+ * @param template The `<xsl:strip-space>` node.
807
+ */
808
+ protected xsltStripSpace(template: XNode): void;
809
+ /**
810
+ * Implements `xsl:preserve-space`.
811
+ * Collects element name patterns for which whitespace-only text nodes should be preserved.
812
+ * preserve-space takes precedence over strip-space for matching elements.
813
+ * @param template The `<xsl:preserve-space>` node.
814
+ */
815
+ protected xsltPreserveSpace(template: XNode): void;
816
+ /**
817
+ * Determines if a text node from the input document should be stripped.
818
+ * This applies xsl:strip-space and xsl:preserve-space rules to whitespace-only text nodes.
819
+ * @param textNode The text node to check.
820
+ * @returns True if the text node should be stripped (not included in output).
821
+ */
822
+ protected shouldStripWhitespaceNode(textNode: XNode): boolean;
823
+ /**
824
+ * Matches an element name against a strip-space/preserve-space pattern.
825
+ * Supports:
826
+ * - "*" matches any element
827
+ * - "prefix:*" matches any element in a namespace
828
+ * - "name" matches elements with that local name
829
+ * - "prefix:name" matches elements with that QName
830
+ * @param elementName The local name of the element.
831
+ * @param pattern The pattern to match against.
832
+ * @param element The element node (for namespace checking).
833
+ * @returns True if the element matches the pattern.
834
+ */
835
+ protected matchesNamePattern(elementName: string, pattern: string, element: XNode): boolean;
707
836
  /**
708
837
  * Implements `xsl:template`.
709
838
  * @param context The Expression Context.
package/index.d.ts CHANGED
@@ -570,6 +570,22 @@ declare class Xslt {
570
570
  outputOmitXmlDeclaration: string;
571
571
  version: string;
572
572
  firstTemplateRan: boolean;
573
+ /**
574
+ * List of element name patterns from xsl:strip-space declarations.
575
+ * Whitespace-only text nodes inside matching elements will be stripped.
576
+ */
577
+ stripSpacePatterns: string[];
578
+ /**
579
+ * List of element name patterns from xsl:preserve-space declarations.
580
+ * Whitespace-only text nodes inside matching elements will be preserved.
581
+ * preserve-space takes precedence over strip-space for conflicting patterns.
582
+ */
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>;
573
589
  constructor(options?: Partial<XsltOptions>);
574
590
  /**
575
591
  * The exported entry point of the XSL-T processor.
@@ -694,6 +710,86 @@ declare class Xslt {
694
710
  * @param template The template.
695
711
  */
696
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;
697
793
  /**
698
794
  * Orders the current node list in the input context according to the
699
795
  * sort order specified by xsl:sort child nodes of the current
@@ -704,6 +800,39 @@ declare class Xslt {
704
800
  * @todo case-order is not implemented.
705
801
  */
706
802
  protected xsltSort(context: ExprContext, template: XNode): void;
803
+ /**
804
+ * Implements `xsl:strip-space`.
805
+ * Collects element name patterns for which whitespace-only text nodes should be stripped.
806
+ * @param template The `<xsl:strip-space>` node.
807
+ */
808
+ protected xsltStripSpace(template: XNode): void;
809
+ /**
810
+ * Implements `xsl:preserve-space`.
811
+ * Collects element name patterns for which whitespace-only text nodes should be preserved.
812
+ * preserve-space takes precedence over strip-space for matching elements.
813
+ * @param template The `<xsl:preserve-space>` node.
814
+ */
815
+ protected xsltPreserveSpace(template: XNode): void;
816
+ /**
817
+ * Determines if a text node from the input document should be stripped.
818
+ * This applies xsl:strip-space and xsl:preserve-space rules to whitespace-only text nodes.
819
+ * @param textNode The text node to check.
820
+ * @returns True if the text node should be stripped (not included in output).
821
+ */
822
+ protected shouldStripWhitespaceNode(textNode: XNode): boolean;
823
+ /**
824
+ * Matches an element name against a strip-space/preserve-space pattern.
825
+ * Supports:
826
+ * - "*" matches any element
827
+ * - "prefix:*" matches any element in a namespace
828
+ * - "name" matches elements with that local name
829
+ * - "prefix:name" matches elements with that QName
830
+ * @param elementName The local name of the element.
831
+ * @param pattern The pattern to match against.
832
+ * @param element The element node (for namespace checking).
833
+ * @returns True if the element matches the pattern.
834
+ */
835
+ protected matchesNamePattern(elementName: string, pattern: string, element: XNode): boolean;
707
836
  /**
708
837
  * Implements `xsl:template`.
709
838
  * @param context The Expression Context.