How to instantiate an XSD Element of an abstract type

18,789

To make it easier to follow, below is a minimally modified schema (added the schema element with an arbitrary targetNamespace, and added a dummy definition for 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>

This is what an automatically (and valid) generated sample XML would look like for the 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>

The main point here is xsi:type="CopyMailingRequest"; this is how you provide a concrete type, in your scenario.

Share:
18,789

Related videos on Youtube

phil-daniels
Author by

phil-daniels

Updated on June 04, 2022

Comments

  • phil-daniels
    phil-daniels almost 2 years

    I'm a java programmer new to XML and web services. I'm trying to create an xml document that conforms to an XSD (which I didn't write) that contains the below snippet. What I want to do is call this web service to copy a mailing. There's an element called copy, which is of the abstract type "tns:CopyRequest". Since the element's type is abstract, Eclipse tells me I can't create one. There's a CopyMailingRequest type (which sounds like it's what I want), but I'm not sure how to instantiate it, since there's no element that extends that type. What am I missing?

    <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>