xml-toolkit 1.1.2 → 1.1.3

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/lib/XMLSchema.js CHANGED
@@ -15,10 +15,26 @@ const XMLSchema = class extends Map {
15
15
 
16
16
  this [TYPES] = new Map ()
17
17
 
18
- {(this.parent = parent).set (this.targetNamespace = targetNamespace, this)}
18
+ this.targetNamespace = targetNamespace
19
+ this.parent = parent
19
20
 
20
21
  this.setSource (node)
21
22
 
23
+ if (this.parent.has (targetNamespace)) {
24
+
25
+ const existing = this.parent.get (targetNamespace)
26
+
27
+ for (const [k, v] of this.entries ()) existing.set (k, v)
28
+
29
+ for (const [k, v] of this [TYPES].entries ()) existing [TYPES].set (k, v)
30
+
31
+ }
32
+ else {
33
+
34
+ this.parent.set (targetNamespace, this)
35
+
36
+ }
37
+
22
38
  }
23
39
 
24
40
  adjustNode (node) {
@@ -51,6 +67,7 @@ const XMLSchema = class extends Map {
51
67
  case 'group':
52
68
  splitNs ('ref')
53
69
  splitNs ('type')
70
+ splitNs ('substitutionGroup')
54
71
  break
55
72
 
56
73
  case 'extension':
@@ -50,8 +50,8 @@ const XMLSchemata = class extends Map {
50
50
 
51
51
  }
52
52
 
53
- getType ([localName, namespaceURI]) {
54
-
53
+ getType ([localName, namespaceURI]) {
54
+
55
55
  const schema = this.get (namespaceURI); if (schema == null) throw new Error ('Unknown namespace: ' + namespaceURI)
56
56
 
57
57
  const node = schema.getType (localName)
@@ -234,12 +234,22 @@ const XMLSchemata = class extends Map {
234
234
 
235
235
  const schema = new XMLSchema (this, targetNamespace, node)
236
236
 
237
- for (const {localName, namespaceURI, attributes: {schemaLocation, namespace}} of schema._src.children)
237
+ for (const {localName, namespaceURI, attributes: {schemaLocation, namespace}} of schema._src.children) {
238
238
 
239
- if (localName === 'import' && namespaceURI === XMLSchema.namespaceURI && namespace !== NamespacesMap.XMLNamespace)
239
+ if (localName === 'import' && namespaceURI === XMLSchema.namespaceURI && namespace !== NamespacesMap.XMLNamespace) {
240
240
 
241
241
  this.addFile (path.join (options.dirname, schemaLocation), {targetNamespace: namespace})
242
242
 
243
+ }
244
+
245
+ if (localName === 'include' && namespaceURI === XMLSchema.namespaceURI) {
246
+
247
+ this.addFile (path.join (options.dirname, schemaLocation), {targetNamespace})
248
+
249
+ }
250
+
251
+ }
252
+
243
253
  }
244
254
 
245
255
  addFile (fn, options = {}) {
@@ -6,11 +6,22 @@ class MatchElement extends Match {
6
6
 
7
7
  const {node: {attributes: {name}, targetNamespace}} = this.occurable
8
8
 
9
- if (localName !== name || namespaceURI != targetNamespace) return null
9
+ if (localName === name && namespaceURI == targetNamespace) {
10
+
11
+ this.occured ++
12
+
13
+ return this.occurable.node
14
+
15
+ }
16
+
17
+ const el = this.occurable.xs?.getSchema (namespaceURI)?.get (localName)
18
+ const sg = el?.attributes?.substitutionGroup
19
+
20
+ if (!sg || sg [0] !== name || sg [1] !== targetNamespace) return null
10
21
 
11
22
  this.occured ++
12
23
 
13
- return this.occurable.node
24
+ return el
14
25
 
15
26
  }
16
27
 
@@ -116,13 +116,39 @@ class XMLValidationState {
116
116
 
117
117
  }
118
118
 
119
+ resolveAttribute (name, attributesMap) {
120
+
121
+ const {attributes} = this.anyType
122
+
123
+ if (attributes.has (name)) {
124
+
125
+ const def = attributes.get (name)
126
+
127
+ if (!def.targetNamespace || def.targetNamespace === attributesMap.getNamespaceURI (name)) return def
128
+
129
+ }
130
+
131
+ const localName = attributesMap.getLocalName (name); if (localName === name) return null
132
+
133
+ const def = attributes.get (localName); if (!def) return null
134
+
135
+ if (def.targetNamespace !== attributesMap.getNamespaceURI (name)) return null
136
+
137
+ return def
138
+
139
+ }
140
+
119
141
  validateAttributes (attributesMap) {
120
142
 
121
143
  const {attributes, isAnyAttributeAllowed} = this.anyType
122
144
 
145
+ const matched = new Set ()
146
+
123
147
  for (const [name, value] of attributesMap) {
124
148
 
125
- if (!attributes.has (name)) {
149
+ const def = this.resolveAttribute (name, attributesMap)
150
+
151
+ if (!def) {
126
152
 
127
153
  if (isAnyAttributeAllowed) continue
128
154
 
@@ -131,8 +157,10 @@ class XMLValidationState {
131
157
  throw Error (`Unknown attribute: "${name}"`)
132
158
 
133
159
  }
134
-
135
- const def = attributes.get (name), {attributes: {fixed, type}} = def
160
+
161
+ matched.add (def)
162
+
163
+ const {attributes: {fixed, type}} = def
136
164
 
137
165
  if (typeof fixed === 'string' && value !== fixed) throw Error (`The attribute "${name}" must have the value "${fixed}", not "${value}"`)
138
166
 
@@ -164,7 +192,7 @@ class XMLValidationState {
164
192
 
165
193
  }
166
194
 
167
- for (const [name, def] of attributes) if (!attributesMap.has (name)) {
195
+ for (const [name, def] of attributes) if (!matched.has (def)) {
168
196
 
169
197
  if (def.attributes.use === 'required') throw Error (`Missing required attribute: "${name}"`)
170
198
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-toolkit",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "XML parser, marshaller, SOAP adapter",
5
5
  "main": "index.js",
6
6
  "files": [