@@ -70,6 +70,7 @@ export class Element {
7070 public nsName ?;
7171 public prefix ?: string ;
7272 public schemaXmlns ?;
73+ public definitionsXmlns ?: IXmlNs ;
7374 public valueKey : string ;
7475 public xmlKey ;
7576 public xmlns ?: IXmlNs ;
@@ -132,6 +133,10 @@ export class Element {
132133 if ( ChildClass ) {
133134 const child = new ChildClass ( nsName , attrs , options , schemaXmlns ) ;
134135 child . init ( ) ;
136+ const root = stack [ 0 ] ;
137+ if ( root instanceof DefinitionsElement ) {
138+ child . definitionsXmlns = root . xmlns ;
139+ }
135140 stack . push ( child ) ;
136141 } else {
137142 this . unexpected ( nsName ) ;
@@ -240,10 +245,9 @@ export class ElementElement extends Element {
240245 if ( type ) {
241246 type = splitQName ( type ) ;
242247 const typeName : string = type . name ;
243- const ns : string = xmlns && xmlns [ type . prefix ] ||
244- this . xmlns [ type . prefix ] ||
245- ( ( definitions . xmlns [ type . prefix ] !== undefined || definitions . xmlns [ this . targetNSAlias ] !== undefined ) && this . schemaXmlns [ type . prefix ] ) ||
246- definitions . xmlns [ type . prefix ] ;
248+ const useSchemaXmlns = ! ! findNs ( type . prefix , this . definitionsXmlns , definitions . xmlns ) ||
249+ ! ! findNs ( this . targetNSAlias , this . definitionsXmlns , definitions . xmlns ) ;
250+ const ns = findNs ( type . prefix , xmlns , this . xmlns , useSchemaXmlns ? this . schemaXmlns : undefined , this . definitionsXmlns , definitions . xmlns ) ;
247251 const schema = definitions . schemas [ ns ] ;
248252 const typeElement = schema && ( this . $type ? schema . complexTypes [ typeName ] || schema . types [ typeName ] : schema . elements [ typeName ] ) ;
249253 const typeStorage = this . $type ? definitions . descriptions . types : definitions . descriptions . elements ;
@@ -419,7 +423,7 @@ export class RestrictionElement extends Element {
419423 if ( desc && this . $base ) {
420424 const type = splitQName ( this . $base ) ;
421425 const typeName = type . name ;
422- const ns = xmlns && xmlns [ type . prefix ] || definitions . xmlns [ type . prefix ] ;
426+ const ns = findNs ( type . prefix , xmlns , this . definitionsXmlns , definitions . xmlns ) ;
423427 const schema = definitions . schemas [ ns ] ;
424428 const typeElement = schema && ( schema . complexTypes [ typeName ] || schema . types [ typeName ] || schema . elements [ typeName ] ) ;
425429
@@ -462,7 +466,7 @@ export class ExtensionElement extends Element {
462466 if ( this . $base ) {
463467 const type = splitQName ( this . $base ) ;
464468 const typeName = type . name ;
465- const ns = xmlns && xmlns [ type . prefix ] || definitions . xmlns [ type . prefix ] ;
469+ const ns = findNs ( type . prefix , xmlns , this . definitionsXmlns , definitions . xmlns ) ;
466470 const schema = definitions . schemas [ ns ] ;
467471
468472 if ( typeName in Primitives ) {
@@ -662,15 +666,15 @@ export class MessageElement extends Element {
662666 delete this . parts ;
663667
664668 const nsName = splitQName ( part . $element ) ;
665- const ns = nsName . prefix ;
666- let schema = definitions . schemas [ definitions . xmlns [ ns ] ] ;
669+ const ns = findNs ( nsName . prefix , this . definitionsXmlns , definitions . xmlns ) ;
670+ let schema = definitions . schemas [ ns ] ;
667671 this . element = schema . elements [ nsName . name ] ;
668672 if ( ! this . element ) {
669673 debug ( nsName . name + ' is not present in wsdl and cannot be processed correctly.' ) ;
670674 return ;
671675 }
672- this . element . targetNSAlias = ns ;
673- this . element . targetNamespace = definitions . xmlns [ ns ] ;
676+ this . element . targetNSAlias = nsName . prefix ;
677+ this . element . targetNamespace = ns ;
674678
675679 // set the optional $lookupType to be used within `client#_invoke()` when
676680 // calling `wsdl#objectToDocumentXML()
@@ -705,7 +709,7 @@ export class MessageElement extends Element {
705709
706710 if ( this . element . $type ) {
707711 const type = splitQName ( this . element . $type ) ;
708- const typeNs = schema . xmlns && schema . xmlns [ type . prefix ] || definitions . xmlns [ type . prefix ] ;
712+ const typeNs = findNs ( type . prefix , schema . xmlns , this . definitionsXmlns , definitions . xmlns ) ;
709713
710714 if ( typeNs ) {
711715 if ( type . name in Primitives ) {
@@ -737,7 +741,7 @@ export class MessageElement extends Element {
737741 }
738742 assert ( part . name === 'part' , 'Expected part element' ) ;
739743 const nsName = splitQName ( part . $type ) ;
740- const ns = definitions . xmlns [ nsName . prefix ] ;
744+ const ns = findNs ( nsName . prefix , this . definitionsXmlns , definitions . xmlns ) ;
741745 const type = nsName . name ;
742746 const schemaDefinition = definitions . schemas [ ns ] ;
743747 if ( typeof schemaDefinition !== 'undefined' ) {
@@ -1257,3 +1261,14 @@ function buildAllowedChildren(elementList: string[]): { [k: string]: typeof Elem
12571261 }
12581262 return rtn ;
12591263}
1264+
1265+ /**
1266+ * Return the first matching namespace for the provided prefix.
1267+ */
1268+ function findNs ( prefix : string , ...xmlnss : Array < IXmlNs | undefined > ) : string | undefined {
1269+ for ( const xmlns of xmlnss ) {
1270+ if ( xmlns ?. [ prefix ] ) {
1271+ return xmlns [ prefix ] ;
1272+ }
1273+ }
1274+ }
0 commit comments