xmljslib 1.0.3 → 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/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
- - **External schemas** — support for XSD imports.
19
- - **Zero runtime dependencies** — built with native Node.js APIs.
20
-
21
- ## Requirements
22
-
23
- - Node.js 18+
24
- - ESM-compatible environment
25
-
26
- ## Installation
27
-
28
- ```bash
29
- npm install xmljslib
30
- ```
31
-
32
- ## Quick start
33
-
34
- ```typescript
35
- import { XMLParser, XMLBuilder } from 'xmljslib';
36
-
37
- // Parse XML
38
- const xml = '<root><name>Test</name></root>';
39
-
40
- const parser = new XMLParser(xml);
41
- console.log(parser.toJSON());
42
-
43
- // {
44
- // root: {
45
- // $value: null,
46
- // name: {
47
- // $value: 'Test'
48
- // }
49
- // }
50
- // }
51
-
52
- // Build XML
53
- const builder = new XMLBuilder({
54
- root: {
55
- Name: 'Test',
56
- },
57
- });
58
-
59
- console.log(builder.toXML());
60
- // <root><Name>Test</Name></root>
61
- ```
62
-
63
- ## XMLParser
64
-
65
- `XMLParser` converts an XML string into a JavaScript object.
66
-
67
- ```typescript
68
- new XMLParser<T extends object = Record<string, any>>(
69
- xml: string,
70
- config?: XMLParserConfig,
71
- );
72
- ```
73
-
74
- ### Configuration
75
-
76
- ```typescript
77
- interface XMLParserConfig {
78
- trimPrefixes?: boolean;
79
- }
80
- ```
81
-
82
- | Option | Type | Default | Description |
83
- |---|---|---|---|
84
- | `trimPrefixes` | `boolean` | `false` | Remove namespace prefixes from element names. |
85
-
86
- For example, with `trimPrefixes: true`, an element named `ns:Person` is represented as `Person`.
87
-
88
- ### toJSON()
89
-
90
- Returns a JSON-like representation of the XML document.
91
-
92
- Element text is stored in the `$value` property. Elements containing child elements are represented as nested objects. Repeated elements are represented as arrays.
93
-
94
- ```typescript
95
- const xml = `
96
- <person id="42">
97
- <name>Alex</name>
98
- <age>30</age>
99
- </person>`;
100
-
101
- const parser = new XMLParser(xml);
102
-
103
- console.log(parser.toJSON());
104
- ```
105
-
106
- The result uses an explicit `$value` wrapper for element content:
107
-
108
- ```typescript
109
- {
110
- person: {
111
- $value: null,
112
- name: {
113
- $value: 'Alex'
114
- },
115
- age: {
116
- $value: '30'
117
- }
118
- }
119
- }
120
- ```
121
-
122
- Use a generic type when you want TypeScript to describe the expected parsed structure:
123
-
124
- ```typescript
125
- interface PersonResponse {
126
- person: {
127
- $value: null;
128
- name: {
129
- $value: string;
130
- };
131
- age: {
132
- $value: string;
133
- };
134
- };
135
- }
136
-
137
- const parser = new XMLParser<PersonResponse>(xml);
138
- const result = parser.toJSON();
139
-
140
- console.log(result?.person.name.$value);
141
- ```
142
-
143
- ### XML elements with attributes
144
-
145
- XML elements can contain both text and attributes. The parser represents such values using XML node wrappers.
146
-
147
- Use `valueOf()` when you need the primitive value of a node.
148
-
149
- ```typescript
150
- const xml = '<person id="42">Alex</person>';
151
-
152
- const parser = new XMLParser(xml);
153
- const result = parser.toJSON();
154
-
155
- const person = result.person;
156
-
157
- console.log(person.valueOf()); // 'Alex'
158
- console.log(person.$id); // '42'
159
- ```
160
-
161
- **Important: use `valueOf()` for strict comparisons.**
162
-
163
- An XML node with attributes is an object wrapper, not a primitive string. JavaScript does not perform type coercion for strict equality (`===`).
164
-
165
- ```typescript
166
- const value = person;
167
-
168
- console.log(value == 'Alex'); // true
169
- console.log(value === 'Alex'); // false
170
-
171
- console.log(value.valueOf() === 'Alex'); // true
172
- ```
173
-
174
- When comparing XML node content, prefer `valueOf() === expectedValue`.
175
-
176
- ### Checking XML tags
177
-
178
- `XMLParser.isXMLTag()` checks whether a string represents a valid XML tag.
179
-
180
- ```typescript
181
- console.log(XMLParser.isXMLTag('<person>')); // true
182
- console.log(XMLParser.isXMLTag('person')); // false
183
- ```
184
-
185
- ## XMLBuilder
186
-
187
- `XMLBuilder` serializes a JavaScript object into an XML string.
188
-
189
- ```typescript
190
- new XMLBuilder(obj).toXML(config?);
191
- ```
192
-
193
- ### Basic usage
194
-
195
- ```typescript
196
- import { XMLBuilder } from 'xmljslib';
197
-
198
- const builder = new XMLBuilder({
199
- person: {
200
- name: 'Alex',
201
- age: 30,
202
- },
203
- });
204
-
205
- console.log(builder.toXML());
206
- // <person><name>Alex</name><age>30</age></person>
207
- ```
208
-
209
- ### Attributes
210
-
211
- Attributes are represented by properties prefixed with `$`.
212
-
213
- ```typescript
214
- const builder = new XMLBuilder({
215
- person: {
216
- $id: '42',
217
- $active: true,
218
- name: 'Alex',
219
- },
220
- });
221
-
222
- console.log(builder.toXML());
223
- // <person id="42" active="true"><name>Alex</name></person>
224
- ```
225
-
226
- ### Element text
227
-
228
- Use `$value` to specify an element's text content.
229
-
230
- ```typescript
231
- const builder = new XMLBuilder({
232
- person: {
233
- $id: '42',
234
- $value: 'Alex',
235
- },
236
- });
237
-
238
- console.log(builder.toXML());
239
- // <person id="42">Alex</person>
240
- ```
241
-
242
- ### Repeated elements
243
-
244
- Use arrays to create repeated XML elements.
245
-
246
- ```typescript
247
- const builder = new XMLBuilder({
248
- people: {
249
- person: [
250
- { name: 'Alex' },
251
- { name: 'Sam' },
252
- ],
253
- },
254
- });
255
-
256
- console.log(builder.toXML());
257
- // <people><person><name>Alex</name></person><person><name>Sam</name></person></people>
258
- ```
259
-
260
- ### XML declaration
261
-
262
- Pass `{ declaration: true }` to prepend an XML declaration.
263
-
264
- ```typescript
265
- const builder = new XMLBuilder({
266
- root: {
267
- name: 'Example',
268
- },
269
- });
270
-
271
- console.log(builder.toXML({ declaration: true }));
272
- // <?xml version="1.0" encoding="UTF-8"?><root><name>Example</name></root>
273
- ```
274
-
275
- ## WSDLTypeGenerator
276
-
277
- `WSDLTypeGenerator` generates TypeScript definitions from WSDL and XSD schemas.
278
-
279
- 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`.
280
-
281
- ### Generate TypeScript definitions
282
-
283
- ```typescript
284
- import { WSDLTypeGenerator } from 'xmljslib';
285
-
286
- const generator = new WSDLTypeGenerator();
287
-
288
- await generator.generate(
289
- 'https://example.com/service?wsdl',
290
- './src/types/service.ts',
291
- );
292
- ```
293
-
294
- The generator retrieves the WSDL, processes its schemas, and writes the resulting TypeScript definitions to the specified file.
295
-
296
- ### Compile definitions without writing a file
297
-
298
- Use `compileToTypeScript()` to retrieve the generated TypeScript source as a string.
299
-
300
- ```typescript
301
- const generator = new WSDLTypeGenerator();
302
-
303
- const source = generator.compileToTypeScript();
304
-
305
- console.log(source);
306
- ```
307
-
308
- ### Generated type helpers
309
-
310
- The generator provides reusable helper types:
311
-
312
- | Type | Description |
313
- |---|---|
314
- | `XMLLeaf<T, Attrs>` | XML leaf element with a value and optional attributes. |
315
- | `XMLBranch<Fields, Attrs>` | XML element containing nested fields and optional attributes. |
316
- | `canBeArray<T>` | A value that can be either a single item or an array. |
317
-
318
- The generator also exposes counters:
319
-
320
- ```typescript
321
- generator.complexTypeCount;
322
- generator.simpleTypeCount;
323
- ```
324
-
325
- These report the number of generated complex and simple types.
326
-
327
- ### Using generated types
328
-
329
- Generated definitions can be used directly with the parser and builder.
330
-
331
- ```typescript
332
- import { XMLParser, XMLBuilder } from 'xmljslib';
333
- import type { WSDLPortTypes } from './types/service';
334
-
335
- type Operation = WSDLPortTypes['ServicePort']['SomeOperation'];
336
-
337
- type Request = Operation['input'];
338
- type Response = Operation['output'];
339
-
340
- const request = new XMLBuilder<Request>({
341
- // Request fields generated from the WSDL schema
342
- });
343
-
344
- const requestXML = request.toXML();
345
-
346
- const response = new XMLParser<Response>(
347
- '<response>...</response>',
348
- );
349
-
350
- const responseData = response.toJSON();
351
- ```
352
-
353
- Replace `ServicePort` and `SomeOperation` with the names generated from your WSDL.
354
-
355
- ### Schema support
356
-
357
- The generator supports:
358
-
359
- - WSDL messages and port types.
360
- - Operations and their input/output message references.
361
- - Named XSD complex types.
362
- - Named XSD simple types and enumerations.
363
- - Nested and inline complex types.
364
- - XSD attributes.
365
- - External XSD imports.
366
-
367
- The exact TypeScript output depends on the structures defined in the source WSDL/XSD documents.
368
-
369
- ## CLI
370
-
371
- `xmljslib` ships with a CLI that generates TypeScript types — together with the documentation of the original schema — from a WSDL/XSD document.
372
-
373
- ### Installation
374
-
375
- ```bash
376
- npm install -g xmljslib # global installation
377
- npx xmljslib --help # run without installing
378
- ```
379
-
380
- ### Usage
381
-
382
- ```bash
383
- xmljslib --import <wsdl-url> [output-path]
384
- ```
385
-
386
- | Option | Description |
387
- |---|---|
388
- | `--import <url>` | URL of the WSDL/XSD document to convert. Required. |
389
- | `[output-path]` | Path of the generated `.ts` file. Default: `./src/types`. |
390
- | `--help`, `-h` | Print the usage information. |
391
-
392
- ### Examples
393
-
394
- ```bash
395
- # Generate types into ./src/types
396
- xmljslib --import http://www.dneonline.com/calculator.asmx?WSDL
397
-
398
- # Generate types into a custom file
399
- xmljslib --import https://example.com/service?WSDL ./src/types/service.types.ts
400
-
401
- # Run through the npm script of the project
402
- npm run import -- https://example.com/service?WSDL ./src/types/service.types.ts
403
- ```
404
-
405
- The command exits with code `1` when no types could be generated, so it can be used in CI.
406
-
407
- ### What is generated
408
-
409
- - TypeScript interfaces for complex types and unions for simple types.
410
- - `WSDLPortTypes` — an interface with the operations of every port type, including their input and output types.
411
- - JSDoc comments taken from `wsdl:documentation` and `xs:documentation`.
412
-
413
- Runnable examples:
414
-
415
- - [Generating types with the CLI](src/examples/cli/generateTypes.ts)
416
- - [Generating documented types without the CLI](src/examples/cli/generateDocumentation.ts)
417
-
418
- ## Examples
419
-
420
- The repository contains standalone examples demonstrating the library's API.
421
-
422
- - [Examples directory](src/examples/)
423
- - [Parsing simple elements](src/examples/xmlParser/simpleElements.ts)
424
- - [Building elements with attributes](src/examples/xmlBuilder/attributes.ts)
425
- - [Generating documented types with the CLI](src/examples/cli/generateTypes.ts)
426
-
427
- Each example can be used as a starting point for experimenting with the parser, the builder or the WSDL generator.
428
-
429
- ## Limitations
430
-
431
- `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.
432
-
433
- In particular:
434
-
435
- - It does not send HTTP requests.
436
- - It does not manage authentication or SOAP headers at the transport level.
437
- - It does not replace an HTTP client such as `fetch` or `axios`.
438
- - It does not expose the internal XML parsing tree as a public API.
439
-
440
- You can use any HTTP client to send the XML produced by `XMLBuilder` and pass the received response to `XMLParser`.
441
-
442
- ## Development
443
-
444
- ```bash
445
- npm install
446
- ```
447
-
448
- Available scripts:
449
-
450
- | Command | Description |
451
- |---|---|
452
- | `npm test` | Run tests silently. |
453
- | `npm run test:verbose` | Run tests with output. |
454
- | `npm run test:coverage` | Run tests with coverage. |
455
- | `npm run lint` | Check code with Biome. |
456
- | `npm run lint:fix` | Apply Biome fixes. |
457
- | `npm run typecheck` | Run TypeScript type checking. |
458
- | `npm run build` | Build ESM and CJS bundles. |
459
- | `npm run dev` | Start development mode with tsx. |
460
- | `npm run import -- <url> [output]` | Generate types from a WSDL URL with the CLI. |
461
- | `npm run example:cli` | Run the CLI example (generates documented types). |
462
- | `npm run example:types` | Run the library API example (generates documented types). |
463
-
464
- ## License
465
-
466
- See [LICENSE](LICENSE).
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).