@@ -71,6 +71,63 @@ export function popSchemaStack(seenSchemasStack, context) {
7171 if ( context ) seenSchemasStack . pop ( ) ;
7272}
7373
74+ export function getXMLAttributes ( schema ) {
75+ return {
76+ name : schema ?. xml ?. name || '' ,
77+ prefix : schema ?. xml ?. prefix || '' ,
78+ namespace : schema ?. xml ?. namespace || null ,
79+ attribute : schema ?. xml ?. attribute ?? false ,
80+ wrapped : schema ?. xml ?. wrapped ?? false ,
81+ } ;
82+ }
83+
84+ export function applyXMLAttributes ( result , schema = { } , context = { } ) {
85+ const { value : oldValue } = result ;
86+ const { propertyName : oldPropertyName } = context ;
87+ const { name, prefix, namespace, attribute, wrapped } =
88+ getXMLAttributes ( schema ) ;
89+ let propertyName = name || oldPropertyName ? `${ prefix ? prefix + ':' : '' } ${ name || oldPropertyName } ` : null ;
90+
91+ let value = typeof oldValue === 'object'
92+ ? Array . isArray ( oldValue )
93+ ? [ ...oldValue ]
94+ : { ...oldValue }
95+ : oldValue ;
96+
97+ if ( attribute && propertyName ) {
98+ propertyName = `$${ propertyName } ` ;
99+ }
100+
101+ if ( namespace ) {
102+ if ( typeof value === 'object' ) {
103+ value [ `$xmlns${ prefix ? ':' + prefix : '' } ` ] = namespace ;
104+ } else {
105+ value = { [ `$xmlns${ prefix ? ':' + prefix : '' } ` ] : namespace , [ '#text' ] : value } ;
106+ }
107+ }
108+
109+ if ( schema . type === 'array' ) {
110+ if ( wrapped && Array . isArray ( value ) ) {
111+ value = { [ propertyName ] : [ ...value ] } ;
112+ }
113+ if ( ! wrapped ) {
114+ propertyName = null ;
115+ }
116+
117+ if ( schema . example !== undefined && ! wrapped ) {
118+ propertyName = schema . items . xml ?. name || propertyName ;
119+ }
120+ }
121+ if ( schema . oneOf || schema . anyOf || schema . allOf || schema . $ref ) {
122+ propertyName = null ;
123+ }
124+
125+ return {
126+ propertyName,
127+ value,
128+ } ;
129+ }
130+
74131function hashCode ( str ) {
75132 var hash = 0 ;
76133 if ( str . length == 0 ) return hash ;
0 commit comments