Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Xsd Complex Elements in XML

Xsd Complex Elements in XML

? XSD Complex Elements in XML

In XSD (XML Schema Definition), complex elements are elements that can contain other elements and/or attributes. They allow for nesting and structuring data hierarchically — which is essential for real-world XML documents.


? What Is a Complex Element?

A complex element can:

  • Contain child elements

  • Contain attributes

  • Contain both elements and attributes

  • Be empty (but still complex)


? Declaring a Complex Element

Here’s the basic structure in XSD:

<xs:element name="person">  <xs:complexType>    <xs:sequence>      <xs:element name="name" type="xs:string"/>      <xs:element name="age" type="xs:integer"/>    </xs:sequence>  </xs:complexType></xs:element>

This allows an XML like:

<person>  <name>John</name>  <age>30</age></person>

? Content Models for Complex Elements

  1. <xs:sequence>
    Child elements must appear in a specific order.

    <xs:sequence>  <xs:element name="firstName" type="xs:string"/>  <xs:element name="lastName" type="xs:string"/></xs:sequence>
  2. <xs:choice>
    Only one child element can appear.

    <xs:choice>  <xs:element name="email" type="xs:string"/>  <xs:element name="phone" type="xs:string"/></xs:choice>
  3. <xs:all>
    All child elements must appear once each, but in any order.

    <xs:all>  <xs:element name="city" type="xs:string"/>  <xs:element name="zip" type="xs:string"/></xs:all>

? Complex Element with Attributes

You can also define attributes within a complex element:

<xs:element name="book">  <xs:complexType>    <xs:sequence>      <xs:element name="title" type="xs:string"/>      <xs:element name="author" type="xs:string"/>    </xs:sequence>    <xs:attribute name="isbn" type="xs:string" use="required"/>  </xs:complexType></xs:element>

Corresponding XML:

<book isbn="9781234567890">  <title>XML in Depth</title>  <author>Jane Doe</author></book>

? Anonymous vs Named Complex Types

  • Anonymous (inline): Defined directly within the element declaration.

  • Named: Defined separately and reused.

Named example:

<xs:complexType name="AddressType">  <xs:sequence>    <xs:element name="street" type="xs:string"/>    <xs:element name="city" type="xs:string"/>  </xs:sequence></xs:complexType><xs:element name="address" type="AddressType"/>

? Summary Table

FeatureComplex Element Example
Contains elementsYes
Contains attributesYes
Allows nestingYes
Use typescomplexType, with sequence, choice, all

Would you like help creating a full XML/XSD example or validating one?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql