xmljslib 1.0.2 → 1.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/dist/cli.js +354 -21
- package/dist/index.cjs +444 -44
- package/dist/index.d.cts +133 -2
- package/dist/index.d.ts +133 -2
- package/dist/index.js +442 -43
- package/package.json +63 -62
- package/readme.md +573 -466
- package/readme.ru.md +110 -3
package/readme.md
CHANGED
|
@@ -1,466 +1,573 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
# xmljslib
|
|
4
|
-
|
|
5
|
-
[English](readme.md) | [Русский](readme.ru.md)
|
|
6
|
-
|
|
7
|
-
**xmljslib** is a TypeScript-first library for parsing, building, and working with XML data in Node.js.
|
|
8
|
-
|
|
9
|
-
It provides a small API for converting XML into JavaScript objects, serializing objects back into XML, and generating TypeScript definitions from WSDL/XSD schemas.
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- **XMLParser** — parse XML strings into JSON-like objects.
|
|
14
|
-
- **XMLBuilder** — serialize JavaScript objects into XML.
|
|
15
|
-
- **WSDLTypeGenerator** — generate TypeScript definitions from WSDL and XSD schemas.
|
|
16
|
-
- **TypeScript support** — generic types for parsed XML and generated WSDL definitions.
|
|
17
|
-
- **Namespace handling** — optionally remove namespace prefixes.
|
|
18
|
-
- **
|
|
19
|
-
- **
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
<
|
|
99
|
-
</
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
console.log(person
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
console.log(value
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
console.log(XMLParser.isXMLTag('person'));
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
$
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
$
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
{ name: '
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
###
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
```typescript
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
```
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
# xmljslib
|
|
4
|
+
|
|
5
|
+
[English](readme.md) | [Русский](readme.ru.md)
|
|
6
|
+
|
|
7
|
+
**xmljslib** is a TypeScript-first library for parsing, building, and working with XML data in Node.js.
|
|
8
|
+
|
|
9
|
+
It provides a small API for converting XML into JavaScript objects, serializing objects back into XML, and generating TypeScript definitions from WSDL/XSD schemas.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **XMLParser** — parse XML strings into JSON-like objects.
|
|
14
|
+
- **XMLBuilder** — serialize JavaScript objects into XML.
|
|
15
|
+
- **WSDLTypeGenerator** — generate TypeScript definitions from WSDL and XSD schemas.
|
|
16
|
+
- **TypeScript support** — generic types for parsed XML and generated WSDL definitions.
|
|
17
|
+
- **Namespace handling** — optionally remove namespace prefixes.
|
|
18
|
+
- **Automatic namespaces** — namespace prefixes extracted from WSDL/XSD are applied by `XMLBuilder`.
|
|
19
|
+
- **External schemas** — support for XSD imports.
|
|
20
|
+
- **Zero runtime dependencies** — built with native Node.js APIs.
|
|
21
|
+
|
|
22
|
+
## Requirements
|
|
23
|
+
|
|
24
|
+
- Node.js 18+
|
|
25
|
+
- ESM-compatible environment
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install xmljslib
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick start
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { XMLParser, XMLBuilder } from 'xmljslib';
|
|
37
|
+
|
|
38
|
+
// Parse XML
|
|
39
|
+
const xml = '<root><name>Test</name></root>';
|
|
40
|
+
|
|
41
|
+
const parser = new XMLParser(xml);
|
|
42
|
+
console.log(parser.toJSON());
|
|
43
|
+
|
|
44
|
+
// {
|
|
45
|
+
// root: {
|
|
46
|
+
// $value: null,
|
|
47
|
+
// name: {
|
|
48
|
+
// $value: 'Test'
|
|
49
|
+
// }
|
|
50
|
+
// }
|
|
51
|
+
// }
|
|
52
|
+
|
|
53
|
+
// Build XML
|
|
54
|
+
const builder = new XMLBuilder({
|
|
55
|
+
root: {
|
|
56
|
+
Name: 'Test',
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
console.log(builder.toXML());
|
|
61
|
+
// <root><Name>Test</Name></root>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## XMLParser
|
|
65
|
+
|
|
66
|
+
`XMLParser` converts an XML string into a JavaScript object.
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
new XMLParser<T extends object = Record<string, any>>(
|
|
70
|
+
xml: string,
|
|
71
|
+
config?: XMLParserConfig,
|
|
72
|
+
);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Configuration
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
interface XMLParserConfig {
|
|
79
|
+
trimPrefixes?: boolean;
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
| Option | Type | Default | Description |
|
|
84
|
+
|---|---|---|---|
|
|
85
|
+
| `trimPrefixes` | `boolean` | `false` | Remove namespace prefixes from element names. |
|
|
86
|
+
|
|
87
|
+
For example, with `trimPrefixes: true`, an element named `ns:Person` is represented as `Person`.
|
|
88
|
+
|
|
89
|
+
### toJSON()
|
|
90
|
+
|
|
91
|
+
Returns a JSON-like representation of the XML document.
|
|
92
|
+
|
|
93
|
+
Element text is stored in the `$value` property. Elements containing child elements are represented as nested objects. Repeated elements are represented as arrays.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
const xml = `
|
|
97
|
+
<person id="42">
|
|
98
|
+
<name>Alex</name>
|
|
99
|
+
<age>30</age>
|
|
100
|
+
</person>`;
|
|
101
|
+
|
|
102
|
+
const parser = new XMLParser(xml);
|
|
103
|
+
|
|
104
|
+
console.log(parser.toJSON());
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The result uses an explicit `$value` wrapper for element content:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
{
|
|
111
|
+
person: {
|
|
112
|
+
$value: null,
|
|
113
|
+
name: {
|
|
114
|
+
$value: 'Alex'
|
|
115
|
+
},
|
|
116
|
+
age: {
|
|
117
|
+
$value: '30'
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Use a generic type when you want TypeScript to describe the expected parsed structure:
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
interface PersonResponse {
|
|
127
|
+
person: {
|
|
128
|
+
$value: null;
|
|
129
|
+
name: {
|
|
130
|
+
$value: string;
|
|
131
|
+
};
|
|
132
|
+
age: {
|
|
133
|
+
$value: string;
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const parser = new XMLParser<PersonResponse>(xml);
|
|
139
|
+
const result = parser.toJSON();
|
|
140
|
+
|
|
141
|
+
console.log(result?.person.name.$value);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### XML elements with attributes
|
|
145
|
+
|
|
146
|
+
XML elements can contain both text and attributes. The parser represents such values using XML node wrappers.
|
|
147
|
+
|
|
148
|
+
Use `valueOf()` when you need the primitive value of a node.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
const xml = '<person id="42">Alex</person>';
|
|
152
|
+
|
|
153
|
+
const parser = new XMLParser(xml);
|
|
154
|
+
const result = parser.toJSON();
|
|
155
|
+
|
|
156
|
+
const person = result.person;
|
|
157
|
+
|
|
158
|
+
console.log(person.valueOf()); // 'Alex'
|
|
159
|
+
console.log(person.$id); // '42'
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
**Important: use `valueOf()` for strict comparisons.**
|
|
163
|
+
|
|
164
|
+
An XML node with attributes is an object wrapper, not a primitive string. JavaScript does not perform type coercion for strict equality (`===`).
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
const value = person;
|
|
168
|
+
|
|
169
|
+
console.log(value == 'Alex'); // true
|
|
170
|
+
console.log(value === 'Alex'); // false
|
|
171
|
+
|
|
172
|
+
console.log(value.valueOf() === 'Alex'); // true
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
When comparing XML node content, prefer `valueOf() === expectedValue`.
|
|
176
|
+
|
|
177
|
+
### Checking XML tags
|
|
178
|
+
|
|
179
|
+
`XMLParser.isXMLTag()` checks whether a string represents a valid XML tag.
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
console.log(XMLParser.isXMLTag('<person>')); // true
|
|
183
|
+
console.log(XMLParser.isXMLTag('person')); // false
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## XMLBuilder
|
|
187
|
+
|
|
188
|
+
`XMLBuilder` serializes a JavaScript object into an XML string.
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
new XMLBuilder(obj).toXML(config?);
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Basic usage
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { XMLBuilder } from 'xmljslib';
|
|
198
|
+
|
|
199
|
+
const builder = new XMLBuilder({
|
|
200
|
+
person: {
|
|
201
|
+
name: 'Alex',
|
|
202
|
+
age: 30,
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
console.log(builder.toXML());
|
|
207
|
+
// <person><name>Alex</name><age>30</age></person>
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Attributes
|
|
211
|
+
|
|
212
|
+
Attributes are represented by properties prefixed with `$`.
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
const builder = new XMLBuilder({
|
|
216
|
+
person: {
|
|
217
|
+
$id: '42',
|
|
218
|
+
$active: true,
|
|
219
|
+
name: 'Alex',
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
console.log(builder.toXML());
|
|
224
|
+
// <person id="42" active="true"><name>Alex</name></person>
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Element text
|
|
228
|
+
|
|
229
|
+
Use `$value` to specify an element's text content.
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
const builder = new XMLBuilder({
|
|
233
|
+
person: {
|
|
234
|
+
$id: '42',
|
|
235
|
+
$value: 'Alex',
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
console.log(builder.toXML());
|
|
240
|
+
// <person id="42">Alex</person>
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Repeated elements
|
|
244
|
+
|
|
245
|
+
Use arrays to create repeated XML elements.
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
const builder = new XMLBuilder({
|
|
249
|
+
people: {
|
|
250
|
+
person: [
|
|
251
|
+
{ name: 'Alex' },
|
|
252
|
+
{ name: 'Sam' },
|
|
253
|
+
],
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
console.log(builder.toXML());
|
|
258
|
+
// <people><person><name>Alex</name></person><person><name>Sam</name></person></people>
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### XML declaration
|
|
262
|
+
|
|
263
|
+
Pass `{ declaration: true }` to prepend an XML declaration.
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
const builder = new XMLBuilder({
|
|
267
|
+
root: {
|
|
268
|
+
name: 'Example',
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
console.log(builder.toXML({ declaration: true }));
|
|
273
|
+
// <?xml version="1.0" encoding="UTF-8"?><root><name>Example</name></root>
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Namespaces
|
|
277
|
+
|
|
278
|
+
Use `$namespace` to set the namespace prefix of an element, and `$xmlns:*` to declare a namespace.
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
const builder = new XMLBuilder({
|
|
282
|
+
'soap:Envelope': {
|
|
283
|
+
'$xmlns:soap': 'http://schemas.xmlsoap.org/soap/envelope/',
|
|
284
|
+
'soap:Body': {
|
|
285
|
+
Ping: {
|
|
286
|
+
Status: 'SUCCESS',
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
console.log(builder.toXML());
|
|
293
|
+
// <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><Ping><Status>SUCCESS</Status></Ping></soap:Body></soap:Envelope>
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Automatic prefixes from WSDL
|
|
297
|
+
|
|
298
|
+
`WSDLTypeGenerator` extracts the namespaces declared in the source document and exports them as a registry. Pass that registry to `XMLBuilder` and prefixes are resolved by element name, while the `xmlns:*` declarations are added to the root element automatically.
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import { XMLBuilder } from 'xmljslib';
|
|
302
|
+
import { XMLNamespaceRegistry } from './types/service';
|
|
303
|
+
|
|
304
|
+
const request = new XMLBuilder<Request>(
|
|
305
|
+
{
|
|
306
|
+
Ping: {
|
|
307
|
+
Status: 'SUCCESS',
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
{ namespaces: XMLNamespaceRegistry },
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
console.log(request.toXML());
|
|
314
|
+
// <tns:Ping xmlns:tns="http://tempuri.org/"><tns:Status>SUCCESS</tns:Status></tns:Ping>
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
The registry can also be passed to `toXML()`:
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
builder.toXML({ namespaces: XMLNamespaceRegistry });
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
The registry itself is a plain object:
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
interface XMLNamespaceRegistry {
|
|
327
|
+
prefixes?: Record<string, string>; // prefix -> namespace URI
|
|
328
|
+
elements?: Record<string, string>; // element name -> prefix
|
|
329
|
+
children?: Record<string, Record<string, string>>; // parent -> { child -> prefix }
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Resolution rules:
|
|
334
|
+
|
|
335
|
+
- An explicit `$namespace` on a node always wins over the registry. An empty string (`$namespace: ''`) leaves that element without a prefix.
|
|
336
|
+
- Element names are matched without a prefix; tags that already contain `:` are left untouched.
|
|
337
|
+
- Only the namespaces that are actually used are declared, and the declarations are placed on the root element.
|
|
338
|
+
- Global XSD elements always belong to the target namespace of their schema. Local elements receive a prefix only when the schema is qualified (`elementFormDefault="qualified"`), exactly as the schema describes them.
|
|
339
|
+
- When the same element name is declared in several namespaces, the prefix depends on the parent. The `children` map carries that context; without it the general `elements` map is used.
|
|
340
|
+
|
|
341
|
+
## WSDLTypeGenerator
|
|
342
|
+
|
|
343
|
+
`WSDLTypeGenerator` generates TypeScript definitions from WSDL and XSD schemas.
|
|
344
|
+
|
|
345
|
+
This is useful when working with SOAP APIs: instead of manually describing request and response structures, you can generate types from the service definition and use them with `XMLBuilder` and `XMLParser`.
|
|
346
|
+
|
|
347
|
+
### Generate TypeScript definitions
|
|
348
|
+
|
|
349
|
+
```typescript
|
|
350
|
+
import { WSDLTypeGenerator } from 'xmljslib';
|
|
351
|
+
|
|
352
|
+
const generator = new WSDLTypeGenerator();
|
|
353
|
+
|
|
354
|
+
await generator.generate(
|
|
355
|
+
'https://example.com/service?wsdl',
|
|
356
|
+
'./src/types/service.ts',
|
|
357
|
+
);
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
The generator retrieves the WSDL, processes its schemas, and writes the resulting TypeScript definitions to the specified file.
|
|
361
|
+
|
|
362
|
+
### Compile definitions without writing a file
|
|
363
|
+
|
|
364
|
+
Use `compileToTypeScript()` to retrieve the generated TypeScript source as a string.
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
const generator = new WSDLTypeGenerator();
|
|
368
|
+
|
|
369
|
+
const source = generator.compileToTypeScript();
|
|
370
|
+
|
|
371
|
+
console.log(source);
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### Generated type helpers
|
|
375
|
+
|
|
376
|
+
The generator provides reusable helper types:
|
|
377
|
+
|
|
378
|
+
| Type | Description |
|
|
379
|
+
|---|---|
|
|
380
|
+
| `XMLLeaf<T, Attrs>` | XML leaf element with a value and optional attributes. |
|
|
381
|
+
| `XMLBranch<Fields, Attrs>` | XML element containing nested fields and optional attributes. |
|
|
382
|
+
| `canBeArray<T>` | A value that can be either a single item or an array. |
|
|
383
|
+
|
|
384
|
+
The generator also exposes counters:
|
|
385
|
+
|
|
386
|
+
```typescript
|
|
387
|
+
generator.complexTypeCount;
|
|
388
|
+
generator.simpleTypeCount;
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
These report the number of generated complex and simple types.
|
|
392
|
+
|
|
393
|
+
### Namespace registry
|
|
394
|
+
|
|
395
|
+
The generator extracts the namespaces declared in the WSDL/XSD and exports them next to the generated types:
|
|
396
|
+
|
|
397
|
+
```typescript
|
|
398
|
+
export const XMLNamespacePrefixes = {
|
|
399
|
+
'tns': 'http://tempuri.org/',
|
|
400
|
+
} as const;
|
|
401
|
+
|
|
402
|
+
export const XMLNamespaceElements = {
|
|
403
|
+
'Ping': 'tns',
|
|
404
|
+
'Status': 'tns',
|
|
405
|
+
} as const;
|
|
406
|
+
|
|
407
|
+
export const XMLNamespaceRegistry = {
|
|
408
|
+
prefixes: XMLNamespacePrefixes,
|
|
409
|
+
elements: XMLNamespaceElements,
|
|
410
|
+
} as const;
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
| Export | Description |
|
|
414
|
+
|---|---|
|
|
415
|
+
| `XMLNamespacePrefixes` | Namespace prefix to namespace URI mapping. |
|
|
416
|
+
| `XMLNamespaceElements` | Element name to namespace prefix mapping. |
|
|
417
|
+
| `XMLNamespaceChildren` | Namespace prefixes of child elements in the context of their parent — for elements declared in several namespaces at once. |
|
|
418
|
+
| `XMLNamespaceRegistry` | All mappings in one object, ready to be passed to `XMLBuilder`. |
|
|
419
|
+
|
|
420
|
+
Every generated type also carries the namespace of the schema it was declared in:
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
export type PingRequest = XMLBranch<{
|
|
424
|
+
$namespace?: 'tns';
|
|
425
|
+
Status?: ResultStatusFlag;
|
|
426
|
+
}>;
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
### Using generated types
|
|
430
|
+
|
|
431
|
+
Generated definitions can be used directly with the parser and builder.
|
|
432
|
+
|
|
433
|
+
```typescript
|
|
434
|
+
import { XMLParser, XMLBuilder } from 'xmljslib';
|
|
435
|
+
import type { WSDLPortTypes } from './types/service';
|
|
436
|
+
import { XMLNamespaceRegistry } from './types/service';
|
|
437
|
+
|
|
438
|
+
type Operation = WSDLPortTypes['ServicePort']['SomeOperation'];
|
|
439
|
+
|
|
440
|
+
type Request = Operation['input'];
|
|
441
|
+
type Response = Operation['output'];
|
|
442
|
+
|
|
443
|
+
const request = new XMLBuilder<Request>(
|
|
444
|
+
{
|
|
445
|
+
// Request fields generated from the WSDL schema
|
|
446
|
+
},
|
|
447
|
+
{ namespaces: XMLNamespaceRegistry },
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
const requestXML = request.toXML();
|
|
451
|
+
|
|
452
|
+
const response = new XMLParser<Response>(
|
|
453
|
+
'<response>...</response>',
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
const responseData = response.toJSON();
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
Replace `ServicePort` and `SomeOperation` with the names generated from your WSDL.
|
|
460
|
+
|
|
461
|
+
### Schema support
|
|
462
|
+
|
|
463
|
+
The generator supports:
|
|
464
|
+
|
|
465
|
+
- WSDL messages and port types.
|
|
466
|
+
- Operations and their input/output message references.
|
|
467
|
+
- Named XSD complex types.
|
|
468
|
+
- Named XSD simple types and enumerations.
|
|
469
|
+
- Nested and inline complex types.
|
|
470
|
+
- XSD attributes.
|
|
471
|
+
- External XSD imports.
|
|
472
|
+
|
|
473
|
+
The exact TypeScript output depends on the structures defined in the source WSDL/XSD documents.
|
|
474
|
+
|
|
475
|
+
## CLI
|
|
476
|
+
|
|
477
|
+
`xmljslib` ships with a CLI that generates TypeScript types — together with the documentation of the original schema — from a WSDL/XSD document.
|
|
478
|
+
|
|
479
|
+
### Installation
|
|
480
|
+
|
|
481
|
+
```bash
|
|
482
|
+
npm install -g xmljslib # global installation
|
|
483
|
+
npx xmljslib --help # run without installing
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
### Usage
|
|
487
|
+
|
|
488
|
+
```bash
|
|
489
|
+
xmljslib --import <wsdl-url> [output-path]
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
| Option | Description |
|
|
493
|
+
|---|---|
|
|
494
|
+
| `--import <url>` | URL of the WSDL/XSD document to convert. Required. |
|
|
495
|
+
| `[output-path]` | Path of the generated `.ts` file. Default: `./src/types`. |
|
|
496
|
+
| `--help`, `-h` | Print the usage information. |
|
|
497
|
+
|
|
498
|
+
### Examples
|
|
499
|
+
|
|
500
|
+
```bash
|
|
501
|
+
# Generate types into ./src/types
|
|
502
|
+
xmljslib --import http://www.dneonline.com/calculator.asmx?WSDL
|
|
503
|
+
|
|
504
|
+
# Generate types into a custom file
|
|
505
|
+
xmljslib --import https://example.com/service?WSDL ./src/types/service.types.ts
|
|
506
|
+
|
|
507
|
+
# Run through the npm script of the project
|
|
508
|
+
npm run import -- https://example.com/service?WSDL ./src/types/service.types.ts
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
The command exits with code `1` when no types could be generated, so it can be used in CI.
|
|
512
|
+
|
|
513
|
+
### What is generated
|
|
514
|
+
|
|
515
|
+
- TypeScript interfaces for complex types and unions for simple types.
|
|
516
|
+
- `WSDLPortTypes` — an interface with the operations of every port type, including their input and output types.
|
|
517
|
+
- JSDoc comments taken from `wsdl:documentation` and `xs:documentation`.
|
|
518
|
+
|
|
519
|
+
Runnable examples:
|
|
520
|
+
|
|
521
|
+
- [Generating types with the CLI](src/examples/cli/generateTypes.ts)
|
|
522
|
+
- [Generating documented types without the CLI](src/examples/cli/generateDocumentation.ts)
|
|
523
|
+
|
|
524
|
+
## Examples
|
|
525
|
+
|
|
526
|
+
The repository contains standalone examples demonstrating the library's API.
|
|
527
|
+
|
|
528
|
+
- [Examples directory](src/examples/)
|
|
529
|
+
- [Parsing simple elements](src/examples/xmlParser/simpleElements.ts)
|
|
530
|
+
- [Building elements with attributes](src/examples/xmlBuilder/attributes.ts)
|
|
531
|
+
- [Building elements with namespaces](src/examples/xmlBuilder/namespaces.ts)
|
|
532
|
+
- [Generating documented types with the CLI](src/examples/cli/generateTypes.ts)
|
|
533
|
+
|
|
534
|
+
Each example can be used as a starting point for experimenting with the parser, the builder or the WSDL generator.
|
|
535
|
+
|
|
536
|
+
## Limitations
|
|
537
|
+
|
|
538
|
+
`xmljslib` focuses on converting XML to JavaScript objects and back. It is not a full XML DOM implementation and does not provide SOAP transport functionality.
|
|
539
|
+
|
|
540
|
+
In particular:
|
|
541
|
+
|
|
542
|
+
- It does not send HTTP requests.
|
|
543
|
+
- It does not manage authentication or SOAP headers at the transport level.
|
|
544
|
+
- It does not replace an HTTP client such as `fetch` or `axios`.
|
|
545
|
+
- It does not expose the internal XML parsing tree as a public API.
|
|
546
|
+
|
|
547
|
+
You can use any HTTP client to send the XML produced by `XMLBuilder` and pass the received response to `XMLParser`.
|
|
548
|
+
|
|
549
|
+
## Development
|
|
550
|
+
|
|
551
|
+
```bash
|
|
552
|
+
npm install
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
Available scripts:
|
|
556
|
+
|
|
557
|
+
| Command | Description |
|
|
558
|
+
|---|---|
|
|
559
|
+
| `npm test` | Run tests silently. |
|
|
560
|
+
| `npm run test:verbose` | Run tests with output. |
|
|
561
|
+
| `npm run test:coverage` | Run tests with coverage. |
|
|
562
|
+
| `npm run lint` | Check code with Biome. |
|
|
563
|
+
| `npm run lint:fix` | Apply Biome fixes. |
|
|
564
|
+
| `npm run typecheck` | Run TypeScript type checking. |
|
|
565
|
+
| `npm run build` | Build ESM and CJS bundles. |
|
|
566
|
+
| `npm run dev` | Start development mode with tsx. |
|
|
567
|
+
| `npm run import -- <url> [output]` | Generate types from a WSDL URL with the CLI. |
|
|
568
|
+
| `npm run example:cli` | Run the CLI example (generates documented types). |
|
|
569
|
+
| `npm run example:types` | Run the library API example (generates documented types). |
|
|
570
|
+
|
|
571
|
+
## License
|
|
572
|
+
|
|
573
|
+
See [LICENSE](LICENSE).
|