Welcome back to CodeYourCraft! Today, we're diving into the world of XML Schema Complex Elements. Let's get started! šÆ
In XML, elements can be either simple or complex. Simple elements have a single value and no sub-elements, while complex elements can have multiple sub-elements, attributes, and a mix of simple and complex content.
š Note: Complex elements are essential for creating well-structured and meaningful XML documents.
To define a complex element in an XML schema, we use the <complexType> element. Inside this, we can define sub-elements, attributes, and their types.
<complexType name="book">
<sequence>
<element name="title" type="string"/>
<element name="author" type="string"/>
<element name="year" type="int"/>
</sequence>
</complexType>In the above example, we've created a complex type book with three sub-elements: title, author, and year.
The <complexContent> element allows us to define complex content within a complex type. It consists of <extension> and <restriction> elements for extending or restricting the content of an existing complex type.
The <extension> element allows us to add new sub-elements to an existing complex type while maintaining its original structure.
<complexType name="book">
<complexContent>
<extension base="bookType">
<sequence>
<element name="publisher" type="string"/>
</sequence>
</extension>
</complexContent>
</complexType>In this example, we've extended the bookType complex type to include a new sub-element publisher.
The <restriction> element allows us to limit the content of an existing complex type by defining simple type restrictions.
<complexType name="postalCode">
<simpleContent>
<restriction base="nonNegativeInteger">
<pattern value="[0-9]{5}"/>
</restriction>
</simpleContent>
</complexType>Here, we've created a complex type postalCode that restricts the nonNegativeInteger data type to only contain five digits.
Mixed content allows complex types to contain both simple and complex data within the same element. This is achieved using the <choice> and <sequence> elements inside the <complexContent> element.
<complexType name="mixedContent">
<complexContent>
<extension base="mixedType">
<choice minOccurs="0" maxOccurs="unbounded">
<element name="note" type="string"/>
<element name="subElement" type="subElementType"/>
</choice>
</extension>
</complexContent>
</complexType>In the above example, the mixedContent complex type can contain either zero or more occurrences of simple note elements or complex subElement elements.
Let's create a simple XML document using the complex types we've defined.
<library>
<book id="1" bookType="novel">
<title>The Catcher in the Rye</title>
<author>J.D. Salinger</author>
<year>1951</year>
<publisher>Little, Brown and Company</publisher>
</book>
<book id="2" bookType="textbook">
<title>Introduction to Computer Science</title>
<author>Harold Abelson</author>
<year>1996</year>
<postalCode>02139</postalCode>
</book>
<book id="3" bookType="mixedContent">
<title>The Art of Computer Programming</title>
<author>Donald E. Knuth</author>
<year>1968</year>
<note>Volumes 1-3</note>
</book>
</library>What does the `<complexType>` element do in an XML schema?
Keep learning, and happy coding! š