Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantic difference between element and complexType in XSD

Tags:

jaxb

xsd

Given this XSD:

<xsd:element name="ServiceList">
    <xsd:complexType>
        <xsd:sequence>
            ...
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:complexType name="ServiceList">
    <xsd:sequence>
        ...
    </xsd:sequence>
</xsd:complexType>

What is kind of the semantic difference between these two? I.e. named elements and complexTypes which are direct children of a schema.

The reason for me asking is that I tried doing this in an XSD:

<xsd:element name="AvailableServices" type="cm:ServiceList" />
<xsd:element name="ExistingServices" type="cm:ServiceList" />
<xsd:complexType name="ServiceList">
    <xsd:sequence>
        ...
    </xsd:sequence>
</xsd:complexType>

But when this was compiled into Java classes using the Maven JAXB plugin, I am only able to create a new ServiceList(). AvailableServices and ExistingServices doesn't seem to even exist among the generated classes. So, what's going on here?

like image 405
Svish Avatar asked Nov 01 '12 14:11

Svish


People also ask

What does complexType mean in XSD?

A complex type is essentially a type definition for elements that may contain attributes and elements. An element can be declared with a type attribute that refers to a complexType element that defines the structure, content, and attributes of that element.

What is simple type and complexType 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.

How can element be defined within an XSD?

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


1 Answers

Classes Correspond to Complex Types

In JAXB (JSR-222) Java classes correspond to complex types. Named complex types and anonymous complex types of global elements correspond to root level classes. Nested complex types by default are generated as static inner classes. You can change this default behaviour:

  • https://stackoverflow.com/a/13175419/383861

Global Elements

If a global element is uniquely associated with a complex type (global element with anonymous complex type) it will be annotated with @XmlRootElement. Global elements that correspond to global types will correspond to @XmlElementDecl annotations in the ObjectFactory class.

For More Information

  • http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html
like image 168
bdoughan Avatar answered Oct 21 '22 11:10

bdoughan