1

I have an XML Schema which I am parsing in JavaScript and then packing that as an object to pass to one of my backend servers.

The schema is like

<complexType name='Test'>
    <sequence>  
        <element name='testField' type='string'/>
        <element name='typeSpecificSetting' type='tns:TypeSpecific'/>
    </sequence> 
</complexType>
<complexType name="TypeSpecific">
    <choice>   
         <element name='A' type='tns:ATYPE'/>
         <element name='B' type='tns:BTYPE'/>
         <element name='C' type='tns:CTYPE'/>
         <element name='D' type='tns:DTYPE'/>
    </choice>
</complexType>

<complexType name="ATYPE">
    <element name='testATYPEField' type='string'/>
</complexType>

<complexType name="BTYPE">
     <element name='testBTYPEField' type='string'/>
</complexType>

I am reading the xml schema and then trying to construct my request object.

request = { 
    testField:  t1,
    typeSpecificSetting: t2
}

How can I construct the request object for choice? Depending upon type, I have to pack either of ATYPE or BTYPE or CTYPE or DTYPE objects? How can I achieve this?

1 Answer 1

1

Since typeSpecific is a complex type with just one choice then typeSpecificSetting property will be an object that contains one property which will one of testATYPEField, testBTYPEField, ...

 request = {
     testField: t1
     typeSpecificSetting: {
         A: { 
             testATYPEField: t2
         }
     }
 }

OR

 request = {
     testField: t1
     typeSpecificSetting: {
         B: { 
             testBTYPEField: t2
         }
     }
 }

etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.