Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate an XSD Element of an abstract type

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>
like image 780
phil-hig Avatar asked May 23 '13 16:05

phil-hig


People also ask

How an element can be defined within an XSD?

Each element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.

What is complexType and simpleType in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What is the difference between element and attribute in XSD?

An attribute provides extra information within an element. Attributes are defined within an XSD as follows, having name and type properties. An Attribute can appear 0 or 1 times within a given element in the XML document. Attributes are either optional or mandatory (by default the are optional).


1 Answers

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.

like image 81
Petru Gardea Avatar answered Dec 28 '22 08:12

Petru Gardea