2013-05-23 29 views
5

Tôi là một lập trình viên java mới cho các dịch vụ web và XML. Tôi đang cố gắng tạo một tài liệu xml phù hợp với một XSD (mà tôi không viết) có chứa đoạn mã dưới đây. Những gì tôi muốn làm là gọi dịch vụ web này để sao chép thư. Có một phần tử được gọi là bản sao, là loại trừu tượng "tns: CopyRequest". Vì kiểu phần tử là trừu tượng, Eclipse nói với tôi rằng tôi không thể tạo ra nó. Có một loại CopyMailingRequest (có vẻ như đó là những gì tôi muốn), nhưng tôi không chắc chắn làm thế nào để nhanh chóng nó, vì không có yếu tố mở rộng loại đó. Tôi đang thiếu gì?Làm thế nào để khởi tạo một phần tử XSD của một kiểu trừu tượng

<xs:element name="copy" nillable="true" type="tns:CopyRequest"/> 

<xs:complexType name="CopyMailingRequest"> 
    <xs:complexContent> 
    <xs:extension base="tns:CopyRequest"> 
     <xs:sequence> 
     <xs:element name="fromId" type="tns:MailingId"/> 
     </xs:sequence> 
    </xs:extension> 
    </xs:complexContent> 
</xs:complexType> 

<xs:complexType name="StandardMailingId"> 
    <xs:complexContent> 
    <xs:extension base="tns:MailingId"/> 
    </xs:complexContent> 
</xs:complexType> 

<xs:complexType name="MailingId"> 
    <xs:complexContent> 
    <xs:extension base="tns:ObjectId"/> 
    </xs:complexContent> 
</xs:complexType> 

<xs:complexType name="CopyRequest" abstract="true"> 
    <xs:sequence> 
    <xs:element name="newName" type="xs:string"/> 
    </xs:sequence> 
</xs:complexType> 

Trả lời

9

Để làm cho nó dễ dàng hơn để làm theo, dưới đây là một sơ đồ biến đổi tối thiểu (bổ sung các yếu tố schema với targetNamespace tùy ý, và thêm một định nghĩa giả cho ObjectId):

<?xml version="1.0" encoding="utf-8" ?> 
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> 
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns:tns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="copy" nillable="true" type="tns:CopyRequest"/> 

    <xs:complexType name="CopyMailingRequest"> 
     <xs:complexContent> 
      <xs:extension base="tns:CopyRequest"> 
       <xs:sequence> 
        <xs:element name="fromId" type="tns:MailingId"/> 
       </xs:sequence> 
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="StandardMailingId"> 
     <xs:complexContent> 
      <xs:extension base="tns:MailingId"/> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="MailingId"> 
     <xs:complexContent> 
      <xs:extension base="tns:ObjectId"/> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="ObjectId"/> 

    <xs:complexType name="CopyRequest" abstract="true"> 
     <xs:sequence> 
      <xs:element name="newName" type="xs:string"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

Đây là những gì một XML mẫu được tạo tự động (và hợp lệ) sẽ trông giống như cho XSD:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<copy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CopyMailingRequest" xmlns="http://tempuri.org/XMLSchema.xsd"> 
    <newName>newName1</newName> 
    <fromId/> 
</copy> 

Điểm chính ở đây là xsi:type="CopyMailingRequest"; đây là cách bạn cung cấp một loại cụ thể, trong kịch bản của bạn.

Các vấn đề liên quan