xslt-processor 4.2.1 → 4.3.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.mjs CHANGED
@@ -2177,6 +2177,122 @@ function xmlGetAttribute(node, name) {
2177
2177
  }
2178
2178
  return value;
2179
2179
  }
2180
+ function nodeToJsonObject(node) {
2181
+ if (!node) {
2182
+ return null;
2183
+ }
2184
+ const nodeType = node.nodeType;
2185
+ if (nodeType === DOM_TEXT_NODE || nodeType === DOM_CDATA_SECTION_NODE) {
2186
+ const text = node.nodeValue ? node.nodeValue.trim() : "";
2187
+ return text.length > 0 ? text : null;
2188
+ }
2189
+ if (nodeType === DOM_COMMENT_NODE) {
2190
+ return null;
2191
+ }
2192
+ if (nodeType === DOM_DOCUMENT_NODE || nodeType === DOM_DOCUMENT_FRAGMENT_NODE) {
2193
+ const children = node.childNodes || [];
2194
+ const childObjects = [];
2195
+ for (let i = 0; i < children.length; i++) {
2196
+ const child = children[i];
2197
+ const childObj = nodeToJsonObject(child);
2198
+ if (childObj !== null) {
2199
+ childObjects.push(childObj);
2200
+ }
2201
+ }
2202
+ if (childObjects.length === 0) {
2203
+ return null;
2204
+ } else if (childObjects.length === 1) {
2205
+ return childObjects[0];
2206
+ } else {
2207
+ return childObjects;
2208
+ }
2209
+ }
2210
+ if (nodeType === DOM_ELEMENT_NODE) {
2211
+ const obj = {};
2212
+ const element = node;
2213
+ const hasAttributes = element.attributes && element.attributes.length > 0;
2214
+ if (hasAttributes) {
2215
+ for (let i = 0; i < element.attributes.length; i++) {
2216
+ const attr = element.attributes[i];
2217
+ obj["@" + attr.nodeName] = attr.nodeValue;
2218
+ }
2219
+ }
2220
+ const children = element.childNodes || [];
2221
+ let textContent = "";
2222
+ let hasElementChildren = false;
2223
+ const childElements = {};
2224
+ for (let i = 0; i < children.length; i++) {
2225
+ const child = children[i];
2226
+ const childType = child.nodeType;
2227
+ if (childType === DOM_TEXT_NODE || childType === DOM_CDATA_SECTION_NODE) {
2228
+ const text = child.nodeValue ? child.nodeValue.trim() : "";
2229
+ if (text.length > 0) {
2230
+ textContent += text;
2231
+ }
2232
+ } else if (childType === DOM_ELEMENT_NODE) {
2233
+ hasElementChildren = true;
2234
+ const childElement = child;
2235
+ const childName = childElement.localName || childElement.nodeName;
2236
+ const childObj = nodeToJsonObject(child);
2237
+ if (childObj !== null) {
2238
+ if (childElements[childName]) {
2239
+ if (!Array.isArray(childElements[childName])) {
2240
+ childElements[childName] = [childElements[childName]];
2241
+ }
2242
+ childElements[childName].push(childObj);
2243
+ } else {
2244
+ childElements[childName] = childObj;
2245
+ }
2246
+ }
2247
+ }
2248
+ }
2249
+ Object.assign(obj, childElements);
2250
+ if (!hasElementChildren && textContent.length > 0) {
2251
+ if (!hasAttributes && Object.keys(childElements).length === 0) {
2252
+ return textContent;
2253
+ } else {
2254
+ obj["#text"] = textContent;
2255
+ }
2256
+ }
2257
+ if (Object.keys(obj).length === 0) {
2258
+ return null;
2259
+ }
2260
+ return obj;
2261
+ }
2262
+ return null;
2263
+ }
2264
+ function xmlToJson(node) {
2265
+ if (!node) {
2266
+ return "{}";
2267
+ }
2268
+ let rootElement = node;
2269
+ if (node.nodeType === DOM_DOCUMENT_NODE || node.nodeType === DOM_DOCUMENT_FRAGMENT_NODE) {
2270
+ const children = node.childNodes || [];
2271
+ for (let i = 0; i < children.length; i++) {
2272
+ if (children[i].nodeType === DOM_ELEMENT_NODE) {
2273
+ rootElement = children[i];
2274
+ break;
2275
+ }
2276
+ }
2277
+ }
2278
+ const element = rootElement;
2279
+ const rootName = element.localName || element.nodeName;
2280
+ const jsonObj = {};
2281
+ const elementContent = nodeToJsonObject(rootElement);
2282
+ if (elementContent === null) {
2283
+ jsonObj[rootName] = {};
2284
+ } else if (typeof elementContent === "object" && !Array.isArray(elementContent)) {
2285
+ jsonObj[rootName] = elementContent;
2286
+ } else {
2287
+ jsonObj[rootName] = elementContent;
2288
+ }
2289
+ try {
2290
+ const cleaned = JSON.parse(JSON.stringify(jsonObj));
2291
+ return JSON.stringify(cleaned);
2292
+ } catch (error) {
2293
+ return JSON.stringify(jsonObj);
2294
+ }
2295
+ }
2180
2296
 
2181
2297
  // src/dom/xml-parser.ts
2182
2298
  import he2 from "he";
@@ -3515,9 +3631,10 @@ var Xslt = class {
3515
3631
  cData: options.cData === true,
3516
3632
  escape: options.escape === true,
3517
3633
  selfClosingTags: options.selfClosingTags === true,
3634
+ outputMethod: options.outputMethod,
3518
3635
  parameters: options.parameters || []
3519
3636
  };
3520
- this.outputMethod = "xml";
3637
+ this.outputMethod = options.outputMethod || "xml";
3521
3638
  this.outputOmitXmlDeclaration = "no";
3522
3639
  this.stripSpacePatterns = [];
3523
3640
  this.preserveSpacePatterns = [];
@@ -3540,7 +3657,7 @@ var Xslt = class {
3540
3657
  * The exported entry point of the XSL-T processor.
3541
3658
  * @param xmlDoc The input document root, as DOM node.
3542
3659
  * @param stylesheet The stylesheet document root, as DOM node.
3543
- * @returns the processed document, as XML text in a string.
3660
+ * @returns the processed document, as XML text in a string, or JSON string if outputMethod is 'json'.
3544
3661
  */
3545
3662
  xsltProcess(xmlDoc, stylesheet) {
3546
3663
  return __async(this, null, function* () {
@@ -3553,6 +3670,9 @@ var Xslt = class {
3553
3670
  }
3554
3671
  }
3555
3672
  yield this.xsltProcessContext(expressionContext, stylesheet, this.outputDocument);
3673
+ if (this.outputMethod === "json") {
3674
+ return xmlToJson(outputDocument);
3675
+ }
3556
3676
  const transformedOutputXml = xmlTransformedText(outputDocument, {
3557
3677
  cData: this.options.cData,
3558
3678
  escape: this.options.escape,