xslt-processor 5.0.0 → 5.0.1
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 +41 -1
- package/index.js +30 -21
- package/index.js.map +1 -1
- package/index.mjs +30 -21
- package/index.mjs.map +1 -1
- package/package.json +1 -1
- package/umd/xslt-processor.global.js +1 -1
- package/umd/xslt-processor.global.js.map +1 -1
package/README.md
CHANGED
|
@@ -119,7 +119,47 @@ const xslt = new Xslt(options);
|
|
|
119
119
|
- `parameters` (`array`, default `[]`): external parameters that you want to use.
|
|
120
120
|
- `name`: the parameter name;
|
|
121
121
|
- `namespaceUri` (optional): the namespace;
|
|
122
|
-
- `value`: the value.
|
|
122
|
+
- `value`: the value. The type is preserved automatically:
|
|
123
|
+
- **string** values become `StringValue`;
|
|
124
|
+
- **number** values become `NumberValue` (usable in XPath arithmetic);
|
|
125
|
+
- **boolean** values become `BooleanValue` (usable in `xsl:if`/`xsl:when` tests);
|
|
126
|
+
- Note: in XPath/XSLT, any non-empty string is truthy, so the string "false" still behaves as true in tests;
|
|
127
|
+
- **`NodeSetValue`** instances are kept as-is (useful for passing additional documents);
|
|
128
|
+
- DOM nodes (objects with `nodeType`) are wrapped in a `NodeSetValue`;
|
|
129
|
+
- arrays of nodes are wrapped in a `NodeSetValue`.
|
|
130
|
+
|
|
131
|
+
**`parameters` examples:**
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
import { Xslt, XmlParser } from 'xslt-processor'
|
|
135
|
+
|
|
136
|
+
// String parameter (default behavior)
|
|
137
|
+
const xslt = new Xslt({ parameters: [
|
|
138
|
+
{ name: 'title', value: 'Hello' }
|
|
139
|
+
] });
|
|
140
|
+
|
|
141
|
+
// Number parameter — works in XPath arithmetic ($count + 1 = 43)
|
|
142
|
+
const xslt = new Xslt({ parameters: [
|
|
143
|
+
{ name: 'count', value: 42 }
|
|
144
|
+
] });
|
|
145
|
+
|
|
146
|
+
// Boolean parameter — works in xsl:if / xsl:when tests
|
|
147
|
+
const xslt = new Xslt({ parameters: [
|
|
148
|
+
{ name: 'debug', value: true }
|
|
149
|
+
] });
|
|
150
|
+
|
|
151
|
+
// Node-set parameter — pass an additional document for cross-document lookups
|
|
152
|
+
import { NodeSetValue } from 'xslt-processor/xpath/values'
|
|
153
|
+
|
|
154
|
+
const xmlParser = new XmlParser();
|
|
155
|
+
const lookupDoc = xmlParser.xmlParse('<lookup><entry key="a">Alpha</entry></lookup>');
|
|
156
|
+
|
|
157
|
+
const xslt = new Xslt({ parameters: [
|
|
158
|
+
{ name: 'lookup', value: new NodeSetValue([lookupDoc]) }
|
|
159
|
+
] });
|
|
160
|
+
// In XSLT: <xsl:value-of select="$lookup/lookup/entry[@key='a']"/>
|
|
161
|
+
```
|
|
162
|
+
|
|
123
163
|
- `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:
|
|
124
164
|
- Denying external loading entirely;
|
|
125
165
|
- Loading from the local filesystem or other non-HTTP sources;
|
package/index.js
CHANGED
|
@@ -11867,28 +11867,37 @@ var NodeConverter = class {
|
|
|
11867
11867
|
*/
|
|
11868
11868
|
convertVariables(exprContext) {
|
|
11869
11869
|
const variables = {};
|
|
11870
|
-
|
|
11871
|
-
|
|
11872
|
-
|
|
11873
|
-
|
|
11874
|
-
|
|
11875
|
-
|
|
11876
|
-
|
|
11877
|
-
|
|
11878
|
-
|
|
11879
|
-
|
|
11880
|
-
|
|
11881
|
-
|
|
11882
|
-
|
|
11883
|
-
|
|
11884
|
-
|
|
11885
|
-
|
|
11886
|
-
|
|
11870
|
+
const contexts = [];
|
|
11871
|
+
let ctx = exprContext;
|
|
11872
|
+
while (ctx) {
|
|
11873
|
+
contexts.push(ctx);
|
|
11874
|
+
ctx = ctx.parent;
|
|
11875
|
+
}
|
|
11876
|
+
for (let i = contexts.length - 1; i >= 0; i--) {
|
|
11877
|
+
const current = contexts[i];
|
|
11878
|
+
for (const [name, value] of Object.entries(current.variables || {})) {
|
|
11879
|
+
if (value && typeof value === "object" && "stringValue" in value) {
|
|
11880
|
+
const nodeValue = value;
|
|
11881
|
+
if (nodeValue.type === "node-set") {
|
|
11882
|
+
variables[name] = value.nodeSetValue().map((n) => this.adaptXNode(n));
|
|
11883
|
+
} else if (nodeValue.type === "string") {
|
|
11884
|
+
variables[name] = value.stringValue();
|
|
11885
|
+
} else if (nodeValue.type === "number") {
|
|
11886
|
+
variables[name] = value.numberValue();
|
|
11887
|
+
} else if (nodeValue.type === "boolean") {
|
|
11888
|
+
variables[name] = value.booleanValue();
|
|
11889
|
+
} else if (nodeValue.type === "map") {
|
|
11890
|
+
variables[name] = nodeValue.value;
|
|
11891
|
+
} else if (nodeValue.type === "array") {
|
|
11892
|
+
variables[name] = nodeValue.value;
|
|
11893
|
+
} else if (nodeValue.type === "function") {
|
|
11894
|
+
variables[name] = nodeValue.value;
|
|
11895
|
+
} else {
|
|
11896
|
+
variables[name] = value.stringValue();
|
|
11897
|
+
}
|
|
11887
11898
|
} else {
|
|
11888
|
-
variables[name] = value
|
|
11899
|
+
variables[name] = value;
|
|
11889
11900
|
}
|
|
11890
|
-
} else {
|
|
11891
|
-
variables[name] = value;
|
|
11892
11901
|
}
|
|
11893
11902
|
}
|
|
11894
11903
|
return variables;
|
|
@@ -12481,7 +12490,7 @@ var ExprContext = class _ExprContext {
|
|
|
12481
12490
|
this.xsltVersion,
|
|
12482
12491
|
typeof opt_position !== "undefined" ? opt_position : this.position,
|
|
12483
12492
|
this.decimalFormatSettings,
|
|
12484
|
-
this.variables,
|
|
12493
|
+
Object.create(this.variables || {}),
|
|
12485
12494
|
this.knownNamespaces,
|
|
12486
12495
|
this,
|
|
12487
12496
|
this.caseInsensitive,
|