SOAP WSDL Associative Arrays

浪尽此生 提交于 2019-12-19 17:30:51

问题


How can I define an associative array in a SOAP wsdl file? This is how I define an array element type so far:

<wsdl:types>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="webservice.wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
        <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
        <xsd:complexType name="ArrayOfString">
            <xsd:complexContent>
                <xsd:restriction base="soapenc:Array">
                    <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="tns:arrayElement"/>
                </xsd:restriction>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:schema>
</wsdl:types>

Thanks!


I'm talking about PHP associative arrays, and I want to use any number of any key=>value string pairs, that will be converted back to associative arrays on the other side of the communication party. As an alternative, I could send the serialized array, or json representation as string, but I'd like to know how to do this in wsdl also.

Thanks!


回答1:


to transfer a php associative array over soap you will need to define following in your wsdl:

<xsd:complexType name="KeyValueData">
      <xsd:sequence>
        <xsd:element minOccurs="1" maxOccurs="1" name="id" type="string"/>
        <xsd:element minOccurs="1" maxOccurs="1" name="name" type="string"/>
        <xsd:element minOccurs="1" maxOccurs="1" name="data" type="string"/>
      </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ArrayOfKeyValueData">
    <xsd:sequence>
      <xsd:element minOccurs="0" maxOccurs="unbounded"
               name="keyval" type="tns:KeyValueData"/>
    </xsd:sequence>
</xsd:complexType> 

now specify your new defined ArrayOfKeyValueData type as the type of your result or as parameter

<message name='getPostStatsResponse'>
  <part name='Result' type='ArrayOfKeyValueData'/>
</message>

and specify your operation with someting like

<operation name='getPostStats'>
    <input message='tns:getPostStatsRequest'/>
    <output message='tns:getPostStatsResponse'/>
</operation>

this will work fine with some kind of web service written in php that returns something like

return array("k1" => "v1", "k2" => "v2");

if you are using php as client, you will get exactly the same array on the client side. other languges or soap libraries may produce other structure, as not every language has this kind of "associative array" concept.




回答2:


WSDL cannot describe the associative nature of an associative array. The best you could do would be to define an array of name/value.

Can you define a PHP service with an operation that returns an associative array, then see what WSDL that produces? You could then follow the same pattern in your own, hand-written WSDLs.




回答3:


If you want to use an array of strings you may just declare in the type that needs the array:

<xs:complexType name="SomeTypeThatUsesAnArrayOfStrings">
    <xs:sequence>
        <xs:element name="TheStringValue" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

And by the way, what do you mean with "associative array"? something like a c++ map or a python dictionary?



来源:https://stackoverflow.com/questions/618736/soap-wsdl-associative-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!