xslt-processor 5.0.2 → 5.0.4
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 +6 -0
- package/dist/LICENSE +165 -0
- package/dist/README.md +513 -0
- package/{index.d.mts → dist/index.d.mts} +6 -0
- package/{index.d.ts → dist/index.d.ts} +6 -0
- package/{index.js → dist/index.js} +25 -12
- package/{index.js.map → dist/index.js.map} +1 -1
- package/{index.mjs → dist/index.mjs} +25 -12
- package/{index.mjs.map → dist/index.mjs.map} +1 -1
- package/{umd → dist/umd}/xslt-processor.global.js +5 -5
- package/{umd → dist/umd}/xslt-processor.global.js.map +1 -1
- package/package.json +12 -9
package/README.md
CHANGED
|
@@ -13,6 +13,12 @@ _A JavaScript XSLT processor without native library dependencies._
|
|
|
13
13
|
</a>
|
|
14
14
|
<img src="https://img.shields.io/npm/dw/xslt-processor" />
|
|
15
15
|
<img src="https://img.shields.io/github/license/DesignLiquido/xslt-processor" />
|
|
16
|
+
<a href="https://github.com/sponsors/leonelsanchesdasilva" target="_blank">
|
|
17
|
+
<img src="https://img.shields.io/github/sponsors/leonelsanchesdasilva" />
|
|
18
|
+
</a>
|
|
19
|
+
<a href="https://buymeacoffee.com/leonelsanches" target="_blank">
|
|
20
|
+
<img src="https://img.shields.io/badge/Buy%20Me%20a%20Coffee-orange?logo=buy-me-a-coffee" />
|
|
21
|
+
</a>
|
|
16
22
|
</p>
|
|
17
23
|
|
|
18
24
|
## Interactive Demos
|
package/dist/LICENSE
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/dist/README.md
ADDED
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
# XSLT-processor
|
|
2
|
+
|
|
3
|
+
_A JavaScript XSLT processor without native library dependencies._
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://github.com/DesignLiquido/xslt-processor/issues" target="_blank">
|
|
7
|
+
<img src="https://img.shields.io/github/issues/DesignLiquido/xslt-processor" />
|
|
8
|
+
</a>
|
|
9
|
+
<img src="https://img.shields.io/github/stars/Designliquido/xslt-processor" />
|
|
10
|
+
<img src="https://img.shields.io/github/forks/Designliquido/xslt-processor" />
|
|
11
|
+
<a href="https://www.npmjs.com/package/xslt-processor" target="_blank">
|
|
12
|
+
<img src="https://img.shields.io/npm/v/xslt-processor" />
|
|
13
|
+
</a>
|
|
14
|
+
<img src="https://img.shields.io/npm/dw/xslt-processor" />
|
|
15
|
+
<img src="https://img.shields.io/github/license/DesignLiquido/xslt-processor" />
|
|
16
|
+
<a href="https://github.com/sponsors/leonelsanchesdasilva" target="_blank">
|
|
17
|
+
<img src="https://img.shields.io/github/sponsors/leonelsanchesdasilva" />
|
|
18
|
+
</a>
|
|
19
|
+
<a href="https://buymeacoffee.com/leonelsanches" target="_blank">
|
|
20
|
+
<img src="https://img.shields.io/badge/Buy%20Me%20a%20Coffee-orange?logo=buy-me-a-coffee" />
|
|
21
|
+
</a>
|
|
22
|
+
</p>
|
|
23
|
+
|
|
24
|
+
## Interactive Demos
|
|
25
|
+
|
|
26
|
+
Try the XSLT and XPath processors directly in your browser:
|
|
27
|
+
|
|
28
|
+
**[🚀 Visit Interactive Demos](https://designliquido.github.io/xslt-processor/)**
|
|
29
|
+
|
|
30
|
+
## How to
|
|
31
|
+
|
|
32
|
+
Install xslt-processor using [npm](https://docs.npmjs.com/about-npm), [ohpm](https://ohpm.openharmony.cn/#/en/home) or [yarn](https://yarnpkg.com):
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
npm install xslt-processor
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
ohpm install xslt-processor
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
yarn add xslt-processor
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Within your ES2015+ code, import the `Xslt` class, the `XmlParser` class and use it this way:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
import { Xslt, XmlParser } from 'xslt-processor'
|
|
50
|
+
|
|
51
|
+
// xmlString: string of xml file contents
|
|
52
|
+
// xsltString: string of xslt file contents
|
|
53
|
+
// outXmlString: output xml string.
|
|
54
|
+
const xslt = new Xslt();
|
|
55
|
+
const xmlParser = new XmlParser();
|
|
56
|
+
// Either
|
|
57
|
+
const outXmlString = await xslt.xsltProcess(
|
|
58
|
+
xmlParser.xmlParse(xmlString),
|
|
59
|
+
xmlParser.xmlParse(xsltString)
|
|
60
|
+
);
|
|
61
|
+
// Or
|
|
62
|
+
xslt.xsltProcess(
|
|
63
|
+
xmlParser.xmlParse(xmlString),
|
|
64
|
+
xmlParser.xmlParse(xsltString)
|
|
65
|
+
).then(output => {
|
|
66
|
+
// `output` is equivalent to `outXmlString` (a string with XML).
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
If you write pre-2015 JS code, make adjustments as needed.
|
|
71
|
+
|
|
72
|
+
### Working with browser DOM and XDocument output
|
|
73
|
+
|
|
74
|
+
Feature available in v5 (next major) of this library.
|
|
75
|
+
|
|
76
|
+
If you already have a browser DOM `Document` or `Node`, convert it to an `XDocument` without re-parsing XML strings:
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
import { domDocumentToXDocument } from 'xslt-processor'
|
|
80
|
+
|
|
81
|
+
const parser = new DOMParser();
|
|
82
|
+
const nativeDoc = parser.parseFromString('<root>hello</root>', 'text/xml');
|
|
83
|
+
const xDoc = domDocumentToXDocument(nativeDoc);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
You can also run XSLT and get the output as an `XDocument` tree instead of a serialized string:
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
import { Xslt, XmlParser } from 'xslt-processor'
|
|
90
|
+
|
|
91
|
+
const xmlParser = new XmlParser();
|
|
92
|
+
const xslt = new Xslt();
|
|
93
|
+
|
|
94
|
+
const xmlDoc = xmlParser.xmlParse('<root><item>hello</item></root>');
|
|
95
|
+
const styleDoc = xmlParser.xmlParse(
|
|
96
|
+
'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' +
|
|
97
|
+
' <xsl:template match="/">' +
|
|
98
|
+
' <output><xsl:value-of select="/root/item"/></output>' +
|
|
99
|
+
' </xsl:template>' +
|
|
100
|
+
'</xsl:stylesheet>'
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const outDoc = await xslt.xsltProcessToDocument(xmlDoc, styleDoc);
|
|
104
|
+
// outDoc is an XDocument you can traverse or serialize with xmlTransformedText.
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### `Xslt` class options
|
|
108
|
+
|
|
109
|
+
You can pass an `options` object to `Xslt` class:
|
|
110
|
+
|
|
111
|
+
```js
|
|
112
|
+
const options = {
|
|
113
|
+
escape: false,
|
|
114
|
+
selfClosingTags: true,
|
|
115
|
+
parameters: [{ name: 'myparam', value: '123' }],
|
|
116
|
+
outputMethod: 'xml'
|
|
117
|
+
};
|
|
118
|
+
const xslt = new Xslt(options);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
- `cData` (`boolean`, default `true`): resolves CDATA elements in the output. Content under CDATA is resolved as text. This overrides `escape` for CDATA content.
|
|
122
|
+
- `escape` (`boolean`, default `true`): replaces symbols like `<`, `>`, `&` and `"` by the corresponding [HTML/XML entities](https://www.tutorialspoint.com/xml/xml_character_entities.htm). Can be overridden by `disable-output-escaping`, that also does the opposite, unescaping `>` and `<` by `<` and `>`, respectively.
|
|
123
|
+
- `selfClosingTags` (`boolean`, default `true`): Self-closes tags that don't have inner elements, if `true`. For instance, `<test></test>` becomes `<test />`.
|
|
124
|
+
- `outputMethod` (`string`, default `xml`): Specifies the default output method. If `<xsl:output>` is declared in your XSLT file, this will be overridden. Valid values: `xml`, `html`, `text`, `xhtml`, `json`, `adaptive`.
|
|
125
|
+
- `parameters` (`array`, default `[]`): external parameters that you want to use.
|
|
126
|
+
- `name`: the parameter name;
|
|
127
|
+
- `namespaceUri` (optional): the namespace;
|
|
128
|
+
- `value`: the value. The type is preserved automatically:
|
|
129
|
+
- **string** values become `StringValue`;
|
|
130
|
+
- **number** values become `NumberValue` (usable in XPath arithmetic);
|
|
131
|
+
- **boolean** values become `BooleanValue` (usable in `xsl:if`/`xsl:when` tests);
|
|
132
|
+
- Note: in XPath/XSLT, any non-empty string is truthy, so the string "false" still behaves as true in tests;
|
|
133
|
+
- **`NodeSetValue`** instances are kept as-is (useful for passing additional documents);
|
|
134
|
+
- DOM nodes (objects with `nodeType`) are wrapped in a `NodeSetValue`;
|
|
135
|
+
- arrays of nodes are wrapped in a `NodeSetValue`.
|
|
136
|
+
|
|
137
|
+
**`parameters` examples:**
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
import { Xslt, XmlParser } from 'xslt-processor'
|
|
141
|
+
|
|
142
|
+
// String parameter (default behavior)
|
|
143
|
+
const xslt = new Xslt({ parameters: [
|
|
144
|
+
{ name: 'title', value: 'Hello' }
|
|
145
|
+
] });
|
|
146
|
+
|
|
147
|
+
// Number parameter — works in XPath arithmetic ($count + 1 = 43)
|
|
148
|
+
const xslt = new Xslt({ parameters: [
|
|
149
|
+
{ name: 'count', value: 42 }
|
|
150
|
+
] });
|
|
151
|
+
|
|
152
|
+
// Boolean parameter — works in xsl:if / xsl:when tests
|
|
153
|
+
const xslt = new Xslt({ parameters: [
|
|
154
|
+
{ name: 'debug', value: true }
|
|
155
|
+
] });
|
|
156
|
+
|
|
157
|
+
// Node-set parameter — pass an additional document for cross-document lookups
|
|
158
|
+
import { NodeSetValue } from 'xslt-processor/xpath/values'
|
|
159
|
+
|
|
160
|
+
const xmlParser = new XmlParser();
|
|
161
|
+
const lookupDoc = xmlParser.xmlParse('<lookup><entry key="a">Alpha</entry></lookup>');
|
|
162
|
+
|
|
163
|
+
const xslt = new Xslt({ parameters: [
|
|
164
|
+
{ name: 'lookup', value: new NodeSetValue([lookupDoc]) }
|
|
165
|
+
] });
|
|
166
|
+
// In XSLT: <xsl:value-of select="$lookup/lookup/entry[@key='a']"/>
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
- `fetchFunction` (`(uri: string) => Promise<string>`, optional): a custom function for loading external resources referenced by `<xsl:import>` and `<xsl:include>`. Receives the URI and must return the fetched content as a string. Defaults to the global `fetch` API. This is useful for:
|
|
170
|
+
- Denying external loading entirely;
|
|
171
|
+
- Loading from the local filesystem or other non-HTTP sources;
|
|
172
|
+
- Transforming or remapping URIs before fetching.
|
|
173
|
+
|
|
174
|
+
**`fetchFunction` examples:**
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
import { readFileSync } from 'fs';
|
|
178
|
+
|
|
179
|
+
// Deny all external loading
|
|
180
|
+
const xslt = new Xslt({
|
|
181
|
+
fetchFunction: async (uri) => {
|
|
182
|
+
throw new Error(`External loading is not allowed: ${uri}`);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// Load from local filesystem
|
|
187
|
+
const xslt = new Xslt({
|
|
188
|
+
fetchFunction: async (uri) => {
|
|
189
|
+
return readFileSync(uri, 'utf-8');
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// Remap URIs before fetching
|
|
194
|
+
const xslt = new Xslt({
|
|
195
|
+
fetchFunction: async (uri) => {
|
|
196
|
+
const remapped = uri.replace('https://example.com/', '/local/stylesheets/');
|
|
197
|
+
const response = await fetch(remapped);
|
|
198
|
+
return response.text();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### JSON Output Format
|
|
204
|
+
|
|
205
|
+
When using `outputMethod: 'json'`, the XSLT processor will convert the resulting XML document to JSON format. This is useful for APIs and modern JavaScript applications.
|
|
206
|
+
|
|
207
|
+
**Example:**
|
|
208
|
+
|
|
209
|
+
```js
|
|
210
|
+
const xslt = new Xslt({ outputMethod: 'json' });
|
|
211
|
+
const xmlParser = new XmlParser();
|
|
212
|
+
|
|
213
|
+
const xmlString = `<root>
|
|
214
|
+
<users>
|
|
215
|
+
<user>Alice</user>
|
|
216
|
+
<user>Bob</user>
|
|
217
|
+
</users>
|
|
218
|
+
</root>`;
|
|
219
|
+
|
|
220
|
+
const xsltString = `<?xml version="1.0"?>
|
|
221
|
+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
|
222
|
+
<xsl:template match="/">
|
|
223
|
+
<xsl:copy-of select="root"/>
|
|
224
|
+
</xsl:template>
|
|
225
|
+
</xsl:stylesheet>`;
|
|
226
|
+
|
|
227
|
+
const result = await xslt.xsltProcess(
|
|
228
|
+
xmlParser.xmlParse(xmlString),
|
|
229
|
+
xmlParser.xmlParse(xsltString)
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
// result will be a JSON string:
|
|
233
|
+
// {"root":{"users":{"user":["Alice","Bob"]}}}
|
|
234
|
+
|
|
235
|
+
const parsed = JSON.parse(result);
|
|
236
|
+
console.log(parsed.root.users.user); // ["Alice", "Bob"]
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
**JSON Structure Rules:**
|
|
240
|
+
|
|
241
|
+
- Each element becomes a property in a JSON object
|
|
242
|
+
- Text-only elements become string values
|
|
243
|
+
- Elements with multiple children of the same name become arrays
|
|
244
|
+
- Empty elements are omitted from the output
|
|
245
|
+
- Attributes are prefixed with `@` (when present in the output)
|
|
246
|
+
- Mixed text and element content uses the `#text` property for text nodes
|
|
247
|
+
|
|
248
|
+
#### Adaptive Output Format
|
|
249
|
+
|
|
250
|
+
When using `outputMethod: 'adaptive'`, the XSLT processor automatically detects the most appropriate output format based on the transformation result. This implements XSLT 3.1 adaptive output behavior.
|
|
251
|
+
|
|
252
|
+
**Detection Rules:**
|
|
253
|
+
|
|
254
|
+
- If the output contains only text nodes (no elements), it returns as plain text
|
|
255
|
+
- If the output contains one or more elements, it returns as XML
|
|
256
|
+
|
|
257
|
+
**Example:**
|
|
258
|
+
|
|
259
|
+
```js
|
|
260
|
+
const xslt = new Xslt({ outputMethod: 'adaptive' });
|
|
261
|
+
const xmlParser = new XmlParser();
|
|
262
|
+
|
|
263
|
+
// Example 1: Pure text output
|
|
264
|
+
const xmlString1 = `<root><value>Hello World</value></root>`;
|
|
265
|
+
const xsltString1 = `<?xml version="1.0"?>
|
|
266
|
+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
|
267
|
+
<xsl:template match="/">
|
|
268
|
+
<xsl:value-of select="root/value"/>
|
|
269
|
+
</xsl:template>
|
|
270
|
+
</xsl:stylesheet>`;
|
|
271
|
+
|
|
272
|
+
const result1 = await xslt.xsltProcess(
|
|
273
|
+
xmlParser.xmlParse(xmlString1),
|
|
274
|
+
xmlParser.xmlParse(xsltString1)
|
|
275
|
+
);
|
|
276
|
+
console.log(result1); // "Hello World" (plain text)
|
|
277
|
+
|
|
278
|
+
// Example 2: XML output
|
|
279
|
+
const xmlString2 = `<root><user>John</user></root>`;
|
|
280
|
+
const xsltString2 = `<?xml version="1.0"?>
|
|
281
|
+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
|
282
|
+
<xsl:template match="/">
|
|
283
|
+
<users><xsl:copy-of select="root/user"/></users>
|
|
284
|
+
</xsl:template>
|
|
285
|
+
</xsl:stylesheet>`;
|
|
286
|
+
|
|
287
|
+
const result2 = await xslt.xsltProcess(
|
|
288
|
+
xmlParser.xmlParse(xmlString2),
|
|
289
|
+
xmlParser.xmlParse(xsltString2)
|
|
290
|
+
);
|
|
291
|
+
console.log(result2); // "<users><user>John</user></users>" (XML)
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Direct use in browsers
|
|
295
|
+
|
|
296
|
+
You can simply add a tag like this:
|
|
297
|
+
|
|
298
|
+
```html
|
|
299
|
+
<script type="application/javascript" src="https://unpkg.com/xslt-processor@latest/umd/xslt-processor.global.js"></script>
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
All the exports will live under `globalThis.XsltProcessor` and `window.XsltProcessor`. [See a usage example here](https://github.com/DesignLiquido/xslt-processor/blob/main/interactive-tests/xslt.html).
|
|
303
|
+
|
|
304
|
+
## XPath Parser
|
|
305
|
+
|
|
306
|
+
To access the XPath parser, you can use the instance present at `Xslt` class:
|
|
307
|
+
|
|
308
|
+
```js
|
|
309
|
+
const xslt = new Xslt();
|
|
310
|
+
const xPath = xslt.xPath;
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Or you can import it like this:
|
|
314
|
+
|
|
315
|
+
```js
|
|
316
|
+
import { XPath } from 'xslt-processor'
|
|
317
|
+
|
|
318
|
+
const xPath = new XPath();
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
`XPath` class is an external dependency, [living in its own repository](https://github.com/DesignLiquido/xpath).
|
|
322
|
+
|
|
323
|
+
## Introduction
|
|
324
|
+
|
|
325
|
+
XSLT-processor contains an implementation of XSLT in JavaScript. Because XSLT uses XPath, it also contains an implementation of XPath that can be used independently of XSLT. This implementation has the advantage that it makes XSLT uniformly available whenever the browser's native `XSLTProcessor()`
|
|
326
|
+
is not available such as in Node.js or in web workers.
|
|
327
|
+
|
|
328
|
+
XSLT-processor builds on Google's [AJAXSLT](https://github.com/4031651/ajaxslt) which was written before `XSLTProcessor()` became available in browsers, but the code base has been updated to comply with ES2015+ and to make it work outside of browsers.
|
|
329
|
+
|
|
330
|
+
This implementation of XSLT operates at the DOM level on its input documents. It internally uses a DOM implementation to create the output document, but usually returns the output document as text stream. The DOM to construct the output document can be supplied by the application, or else an internal minimal DOM implementation is used. This DOM comes with a minimal XML parser that can be used to generate a suitable DOM representation of the input documents if they are present as text.
|
|
331
|
+
|
|
332
|
+
## Building from source
|
|
333
|
+
|
|
334
|
+
The XPath engine lives in a Git submodule at `src/xpath/lib`. A regular `git clone` does **not** fetch it automatically, so the build will fail unless the submodule is initialised.
|
|
335
|
+
|
|
336
|
+
### Fresh clone
|
|
337
|
+
|
|
338
|
+
```sh
|
|
339
|
+
git clone --recurse-submodules https://github.com/DesignLiquido/xslt-processor.git
|
|
340
|
+
cd xslt-processor
|
|
341
|
+
yarn install
|
|
342
|
+
yarn build
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### Already cloned without submodules
|
|
346
|
+
|
|
347
|
+
```sh
|
|
348
|
+
git submodule update --init --recursive
|
|
349
|
+
yarn install
|
|
350
|
+
yarn build
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Updating the submodule to the latest commit
|
|
354
|
+
|
|
355
|
+
```sh
|
|
356
|
+
git submodule update --remote src/xpath/lib
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
## Tests and usage examples
|
|
360
|
+
|
|
361
|
+
New tests are written in Jest and can be run by calling: `yarn test`.
|
|
362
|
+
|
|
363
|
+
The files `xslt.html` and `xpath.html` in the directory `interactive-tests` are interactive tests. They can be run directly from the file system; no HTTP server is needed.
|
|
364
|
+
|
|
365
|
+
Both interactive tests and automatic tests demonstrate the use of the library functions.
|
|
366
|
+
|
|
367
|
+
## Conformance
|
|
368
|
+
|
|
369
|
+
A few features that are required by the XSLT and XPath standards were left out (but patches to add them are welcome).
|
|
370
|
+
|
|
371
|
+
See our [TODO](TODO.md) for a list of missing features that we are aware of (please add more items by means of PRs).
|
|
372
|
+
|
|
373
|
+
So far, we have implemented XQuery functions for versions 1.0 and 2.0, but this is not complete yet.
|
|
374
|
+
|
|
375
|
+
The DOM implementation is minimal so as to support the XSLT processing, and not intended to be complete.
|
|
376
|
+
|
|
377
|
+
The implementation is all agnostic about namespaces. It just expects XSLT elements to have tags that carry the `xsl:` prefix, but we disregard all namespace declarations for them.
|
|
378
|
+
|
|
379
|
+
[There are a few nonstandard XPath functions](https://github.com/search?q=repo%3ADesignLiquido%2Fxslt-processor%20ext-&type=code).
|
|
380
|
+
|
|
381
|
+
### HTML Conformance
|
|
382
|
+
|
|
383
|
+
HTML per se is not strict XML. Because of that, starting on version 2.0.0, this library handles HTML differently than XML:
|
|
384
|
+
|
|
385
|
+
- For a document to be treated as HTML, it needs to have a `<!DOCTYPE>` tag defined with one of the following valid formats:
|
|
386
|
+
- `<!DOCTYPE html>` (for HTML5);
|
|
387
|
+
- `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">` (for HTML4);
|
|
388
|
+
- `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">` (for XHTML 1.1).
|
|
389
|
+
- 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);
|
|
390
|
+
- This rule doesn't apply for XHTML, which is strict XML.
|
|
391
|
+
|
|
392
|
+
### Whitespace Handling
|
|
393
|
+
|
|
394
|
+
This library supports `xsl:strip-space` and `xsl:preserve-space` for controlling whitespace in the input document.
|
|
395
|
+
|
|
396
|
+
#### `xsl:strip-space`
|
|
397
|
+
|
|
398
|
+
Use `<xsl:strip-space>` to remove whitespace-only text nodes from specified elements in the input document:
|
|
399
|
+
|
|
400
|
+
```xml
|
|
401
|
+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
|
402
|
+
<!-- Strip whitespace from all elements -->
|
|
403
|
+
<xsl:strip-space elements="*"/>
|
|
404
|
+
|
|
405
|
+
<!-- Or strip from specific elements -->
|
|
406
|
+
<xsl:strip-space elements="book chapter section"/>
|
|
407
|
+
|
|
408
|
+
<!-- ... templates ... -->
|
|
409
|
+
</xsl:stylesheet>
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
The `elements` attribute accepts:
|
|
413
|
+
- `*` - matches all elements
|
|
414
|
+
- `name` - matches elements with the specified local name
|
|
415
|
+
- `prefix:*` - matches all elements in a namespace
|
|
416
|
+
- `prefix:name` - matches a specific element in a namespace
|
|
417
|
+
- Multiple patterns separated by whitespace (e.g., `"book chapter section"`)
|
|
418
|
+
|
|
419
|
+
#### `xsl:preserve-space`
|
|
420
|
+
|
|
421
|
+
Use `<xsl:preserve-space>` to preserve whitespace in specific elements, overriding `xsl:strip-space`:
|
|
422
|
+
|
|
423
|
+
```xml
|
|
424
|
+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
|
425
|
+
<xsl:strip-space elements="*"/>
|
|
426
|
+
<!-- Preserve whitespace in pre and code elements -->
|
|
427
|
+
<xsl:preserve-space elements="pre code"/>
|
|
428
|
+
|
|
429
|
+
<!-- ... templates ... -->
|
|
430
|
+
</xsl:stylesheet>
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
#### Precedence Rules
|
|
434
|
+
|
|
435
|
+
1. `xml:space="preserve"` attribute on an element takes highest precedence
|
|
436
|
+
2. `xsl:preserve-space` overrides `xsl:strip-space` for matching elements
|
|
437
|
+
3. `xsl:strip-space` applies to remaining matches
|
|
438
|
+
4. By default (no declarations), whitespace is preserved
|
|
439
|
+
|
|
440
|
+
### Breaking Changes
|
|
441
|
+
|
|
442
|
+
Breaking changes are usually documented at every major release, but we also keep a short version of them below.
|
|
443
|
+
|
|
444
|
+
#### Version 2
|
|
445
|
+
|
|
446
|
+
Until version 2.3.1, use like the example below:
|
|
447
|
+
|
|
448
|
+
```js
|
|
449
|
+
import { Xslt, XmlParser } from 'xslt-processor'
|
|
450
|
+
|
|
451
|
+
// xmlString: string of xml file contents
|
|
452
|
+
// xsltString: string of xslt file contents
|
|
453
|
+
// outXmlString: output xml string.
|
|
454
|
+
const xslt = new Xslt();
|
|
455
|
+
const xmlParser = new XmlParser();
|
|
456
|
+
const outXmlString = xslt.xsltProcess( // Not async.
|
|
457
|
+
xmlParser.xmlParse(xmlString),
|
|
458
|
+
xmlParser.xmlParse(xsltString)
|
|
459
|
+
);
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
Version 3 received `<xsl:include>` which relies on Fetch API, which is asynchronous. Version 2 doesn't support `<xsl:include>`.
|
|
463
|
+
|
|
464
|
+
If using Node.js older than version v17.5.0, please use version 3.2.3, that uses `node-fetch` package. Versions 3.3.0 onward require at least Node.js version v17.5.0, since they use native `fetch()` function.
|
|
465
|
+
|
|
466
|
+
#### Version 1
|
|
467
|
+
|
|
468
|
+
Until version 1.2.8, use like the example below:
|
|
469
|
+
|
|
470
|
+
```js
|
|
471
|
+
import { Xslt, xmlParse } from 'xslt-processor'
|
|
472
|
+
|
|
473
|
+
// xmlString: string of xml file contents
|
|
474
|
+
// xsltString: string of xslt file contents
|
|
475
|
+
// outXmlString: output xml string.
|
|
476
|
+
const xslt = new Xslt();
|
|
477
|
+
const outXmlString = xslt.xsltProcess(
|
|
478
|
+
xmlParse(xmlString),
|
|
479
|
+
xmlParse(xsltString)
|
|
480
|
+
);
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
#### Version 0
|
|
484
|
+
|
|
485
|
+
Until version 0.11.7, use like the example below:
|
|
486
|
+
|
|
487
|
+
```js
|
|
488
|
+
import { xsltProcess, xmlParse } from 'xslt-processor'
|
|
489
|
+
|
|
490
|
+
// xmlString: string of xml file contents
|
|
491
|
+
// xsltString: string of xslt file contents
|
|
492
|
+
// outXmlString: output xml string.
|
|
493
|
+
const outXmlString = xsltProcess(
|
|
494
|
+
xmlParse(xmlString),
|
|
495
|
+
xmlParse(xsltString)
|
|
496
|
+
);
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
and to access the XPath parser:
|
|
500
|
+
|
|
501
|
+
```js
|
|
502
|
+
import { xpathParse } from 'xslt-processor'
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
These functions are part of `Xslt` and `XPath` classes, respectively, at version 1.x onward.
|
|
506
|
+
|
|
507
|
+
## References
|
|
508
|
+
|
|
509
|
+
- XPath Specification: http://www.w3.org/TR/1999/REC-xpath-19991116
|
|
510
|
+
- XSLT Specification: http://www.w3.org/TR/1999/REC-xslt-19991116
|
|
511
|
+
- W3C DOM Level 3 Core Specification: http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/
|
|
512
|
+
- ECMAScript Language Specification: http://www.ecma-international.org/publications/standards/Ecma-262.htm
|
|
513
|
+
- Arkts Language Specification: https://gitee.com/openharmony/arkcompiler_runtime_core/releases/download/ArkTS-spec-1.2.0-alpha-20250307/arktsspecification.pdf
|