xml-toolkit 1.0.1 → 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 +2 -0
- package/lib/XMLMarshaller.js +37 -11
- package/package.json +1 -1
- package/test/20040.wsdl +1812 -0
- package/test/sign.xsd +71 -0
- package/test/test.js +59 -1
package/README.md
CHANGED
|
@@ -57,6 +57,8 @@ let xmlResult = ''; for await (const node of new XMLReader ().process (xmlSource
|
|
|
57
57
|
* [Serializing an Object According to an XML Schema](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Serializing-an-Object-According-to-an-XML-Schema)
|
|
58
58
|
|
|
59
59
|
```js
|
|
60
|
+
const {XMLSchemata} = require ('xml-toolkit')
|
|
61
|
+
|
|
60
62
|
const data = {ExportDebtRequestsResponse: {
|
|
61
63
|
"request-data": {
|
|
62
64
|
// ...
|
package/lib/XMLMarshaller.js
CHANGED
|
@@ -37,11 +37,29 @@ const XMLMarshaller = class {
|
|
|
37
37
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
stringify (content) {
|
|
40
|
+
stringify (content, name) {
|
|
41
41
|
|
|
42
|
-
const qName = this.ns.QName (this.schemaElement.name, this.schema.targetNamespace)
|
|
42
|
+
const qName = this.ns.QName (name || this.schemaElement.name, this.schema.targetNamespace)
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
let {complexType, sequence, choice, all, type} = this.schemaElement, group = sequence || choice || all
|
|
45
|
+
|
|
46
|
+
if (type) {
|
|
47
|
+
|
|
48
|
+
type = this.xs.getByReference (type)
|
|
49
|
+
|
|
50
|
+
switch (type._type) {
|
|
51
|
+
|
|
52
|
+
case 'complexType':
|
|
53
|
+
complexType = type
|
|
54
|
+
break
|
|
55
|
+
|
|
56
|
+
case 'simpleType':
|
|
57
|
+
simpleType = type
|
|
58
|
+
break
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
}
|
|
45
63
|
|
|
46
64
|
let buf = ['<' + qName]
|
|
47
65
|
|
|
@@ -50,8 +68,9 @@ const XMLMarshaller = class {
|
|
|
50
68
|
if (complexType) this._aComplexType (buf, complexType, content)
|
|
51
69
|
|
|
52
70
|
buf [0] += '>'
|
|
53
|
-
|
|
71
|
+
|
|
54
72
|
if (complexType) this._bComplexType (buf, complexType, content)
|
|
73
|
+
if (group) this._bSequence (buf, group, content)
|
|
55
74
|
|
|
56
75
|
return buf [0] + '</' + qName + '>'
|
|
57
76
|
|
|
@@ -163,7 +182,7 @@ const XMLMarshaller = class {
|
|
|
163
182
|
}
|
|
164
183
|
|
|
165
184
|
to_string (v, type, restriction = {}) {
|
|
166
|
-
|
|
185
|
+
|
|
167
186
|
const carp = () => {throw new Error (`Invalid value for ${type} type: ${v} (${typeof v})`)}
|
|
168
187
|
|
|
169
188
|
switch (type) {
|
|
@@ -178,7 +197,9 @@ const XMLMarshaller = class {
|
|
|
178
197
|
if (v.length === 10) return v
|
|
179
198
|
case 'number':
|
|
180
199
|
case 'bigint':
|
|
181
|
-
|
|
200
|
+
const d = new Date (v)
|
|
201
|
+
if (isNaN (d)) carp ()
|
|
202
|
+
v = d
|
|
182
203
|
}
|
|
183
204
|
if (v instanceof Date) {
|
|
184
205
|
return v.toJSON ().slice (0, 10)
|
|
@@ -190,6 +211,7 @@ const XMLMarshaller = class {
|
|
|
190
211
|
case 'dateTime':
|
|
191
212
|
switch (typeof v) {
|
|
192
213
|
case 'string':
|
|
214
|
+
if (v.length === 10) return v + 'T00:00:00'
|
|
193
215
|
return v
|
|
194
216
|
case 'number':
|
|
195
217
|
case 'bigint':
|
|
@@ -284,16 +306,20 @@ const XMLMarshaller = class {
|
|
|
284
306
|
}
|
|
285
307
|
|
|
286
308
|
_bSequence (buf, _sequence, content) {
|
|
287
|
-
|
|
309
|
+
|
|
288
310
|
const {element, sequence, choice, all} = _sequence, group = sequence || choice || all
|
|
289
311
|
|
|
290
312
|
if (group) this._bSequence (buf, group, content)
|
|
291
313
|
|
|
292
314
|
if (element) {
|
|
293
|
-
|
|
294
|
-
for (const e of Array.isArray (element) ? element : [element])
|
|
295
|
-
|
|
296
|
-
|
|
315
|
+
|
|
316
|
+
for (const e of Array.isArray (element) ? element : [element]) {
|
|
317
|
+
|
|
318
|
+
const c = content [e.name]
|
|
319
|
+
|
|
320
|
+
this._bElement (buf, e, c)
|
|
321
|
+
|
|
322
|
+
}
|
|
297
323
|
|
|
298
324
|
}
|
|
299
325
|
|