如何在XML输出中显示属性的名称而不是“item”

时间:2012-10-18 05:32:41

标签: xml web-services wsdl

我正在编写一个WSDL文件,它正在运行。但是,输出看起来并不像我想看的那样。所以,我已经定义了这样的输出消息:

<message name="serviceResponse">
  <part name="code" type="xsd:string"/>
  <part name="description" type="xsd:string"/>
  <part name="results" element="tns:myProperty"/>
</message>

在&lt; types&gt;中标签,我有:

<xsd:element name="myProperty">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element name="description" type="xsd:string"/>
      <xsd:element name="id" type="xsd:int"/>
      <xsd:element name="date" type="xsd:date"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

作为回应,我得到了:

<SOAP-ENV:serviceResponse>
  <code xsi:type="xsd:string">001</code>
  <description xsi:type="xsd:string">Successful</description>
  <results SOAP-ENC:arrayType="SOAP-ENC:Array[2]" xsi:type="SOAP-ENC:Array">
    <item SOAP-ENC:arrayType="xsd:ur-type[7]" xsi:type="SOAP-ENC:Array">
      <item xsi:type="xsd:string">John</item>
      <item xsi:type="xsd:string">Director</item>
      <item xsi:type="xsd:int">21</item>
      <item xsi:type="xsd:string">2012-10-16</item>
    </item>
    <item SOAP-ENC:arrayType="xsd:ur-type[7]" xsi:type="SOAP-ENC:Array">
      <item xsi:type="xsd:string">Smith</item>
      <item xsi:type="xsd:string">Programmer</item>
      <item xsi:type="xsd:int">88</item>
      <item xsi:type="xsd:string">2012-10-18</item>
    </item>
  </results>
</SOAP-ENV:serviceResponse></SOAP-ENV:Body>

我希望在“results”标签中得到类似的内容,而不是一堆“item”:

<myProperty SOAP-ENC:arrayType="xsd:ur-type[7]" xsi:type="SOAP-ENC:Array">
  <name xsi:type="xsd:string">John</name>
  <description xsi:type="xsd:string">Director</description>
  <id xsi:type="xsd:int">21</id>
  <date xsi:type="xsd:string">2012-10-16</date>
</myProperty>
<myProperty SOAP-ENC:arrayType="xsd:ur-type[7]" xsi:type="SOAP-ENC:Array">
  <name xsi:type="xsd:string">Smith</name>
  <description xsi:type="xsd:string">Programmer</description>
  <id xsi:type="xsd:int">88</id>
  <date xsi:type="xsd:string">2012-10-18</date>
</myProperty>

任何人都可以发光吗?感谢

2 个答案:

答案 0 :(得分:1)

我通过查看来自WSDL list of complexType HOWTO- define, return from a service?

的其他帖子解决了这个问题

无需DTD定义。无论如何,感谢so

我所做的略有不同。我没有使用名为“myProperty”的元素,而是使用名称“myProperty”直接定义了complexType。它刚刚起作用。

<xsd:complexType name="myProperty">
    <xsd:sequence>
        <xsd:element name="name" type="xsd:string"/>
        <xsd:element name="description" type="xsd:string"/>
        <xsd:element name="id" type="xsd:int"/>
        <xsd:element name="date" type="xsd:date"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:element name="serviceResponse">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element maxOccurs="unbounded" minOccurs="0" name="myResult" nillable="true" type="tns:myProperty"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

输出正是我的预期:

<myProperty xsi:type="ns1:myProperty">
    <name xsi:type="xsd:string">Smith</name>
    <description xsi:type="xsd:string">Programmer</description>
    <id xsi:type="xsd:int">88</id>
    <date xsi:type="xsd:date">2012-10-18</date>
</myProperty>

答案 1 :(得分:0)

正确定义myProperty标签的DTD。

像:

<!ELEMENT myProperty (name,description,id,date)>
<!ELEMENT name(#PCDATA)>
<!ELEMENT description(#PCDATA)>
<!ELEMENT id(#PCDATA)>
<!ELEMENT date(#PCDATA)>