我成功地使用SOAP服务交换消息但是对于某种消息类型,反序列化的响应对象中缺少某些属性。
服务的WSDL文件很简单,因为所有类型都包含在外部XSD文件中,但XSD文件结构非常复杂。
我试图尽可能简化我的示例:在我从PHP本机SoapClient获取的响应对象中,下面给出的示例消息缺少属性SubAttribute
。
可以做什么以便正确地反序列化所有属性?
1 / 以下是收到的SOAP消息
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope ...>
<soap:Header>
...
</soap:Header>
<soap:Body>
<MainElement MainAttribute="test">
<SubElements>
<SubElement SubAttribute="test">
...
</SubElement>
</SubElements>
</MainElement>
</soap:Body>
</soap:Envelope>
2 / WSDL定义如下
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="ServiceName" ...>
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">ServiceName</wsdl:documentation>
<types>
<xsd:schema>
<xsd:import namespace="http://www.example.com/Version" schemaLocation="GetMainElementMessage.xsd"/>
...
</xsd:schema>
</types>
<message>
<message name="GetMainElementMessage">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
<part name="Request" element="myns:GetMainElementMessage"/>
</message>
<message name="SetMainElementMessage">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
<part name="Response" element="myns:SetMainElementMessage"/>
</message>
</message>
<portType name="ServiceName">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
<operation name="GetMainElement">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Operation description.</wsdl:documentation>
<input message="myns:GetMainElementMessage"/>
<output message="myns:SetMainElementMessage"/>
</operation>
...
</portType>
<binding>
...
</binding>
<service>
...
</service>
</definitions>
3 / 第一个XSD文件:GetMainElementMessage.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema ...>
<xs:include schemaLocation="CommonAttributes.xsd"/>
<xs:include schemaLocation="SubElementCommonType.xsd"/>
<xs:element name="MainElement">
<xs:complexType>
<xs:attributeGroup ref="MainElementsStdAttributes"/>
<xs:element name="SubElements" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="SubElement" maxOccurs="unbounded">
<xs:complexType>
<xs:complexContent>
<xs:extension base="SubElementCommonType">
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:schema>
4 / 第二个XSD文件:SubElementCommonType.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema ...>
<xs:include schemaLocation="CommonAttributes.xsd"/>
<xs:complexType name="SubElementCommonType">
<xs:attributeGroup ref="SubElementsStdAttributes"/>
...
</xs:complexType>
</xs:schema>
5 / 此示例的最后一个XSD:CommonAttributes.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema ...>
<xs:attributeGroup name="MainElementsStdAttributes">
<xs:attribute name="MainAttribute" type="xs:string" use="optional" />
</xs:attributeGroup>
<xs:attributeGroup name="SubElementsStdAttributes">
<xs:attribute name="SubAttribute" type="xs:string" use="optional" />
</xs:attributeGroup>
...
</xs:schema>