XSD Empty Elements

Defining elements with no content

📭 What are XSD Empty Elements?

Empty elements contain no text or child elements. They can have attributes but no content between opening and closing tags. Perfect for flags, markers, or self-closing elements.


<!-- Empty element example -->
<image src="photo.jpg" />
<linebreak />
                                    

Defining Empty Elements

Empty elements are defined in XSD by restricting the content model. You can create empty elements that have no content but may contain attributes for additional information.

🔸 Basic Empty Element

<xs:element name="linebreak">
  <xs:complexType />
</xs:element>

XML Usage:

<linebreak />

🔹 Empty Element with Attributes

Most empty elements include attributes to carry data. The element itself is empty, but attributes provide necessary information like IDs, URLs, or configuration values.

<xs:element name="image">
  <xs:complexType>
    <xs:attribute name="src" type="xs:string" use="required" />
    <xs:attribute name="alt" type="xs:string" />
  </xs:complexType>
</xs:element>

XML Usage:

<image src="logo.png" alt="Company Logo" />

🔹 Multiple Empty Elements Example

Here's a practical schema with several empty elements commonly used in web documents:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="br">
    <xs:complexType />
  </xs:element>

  <xs:element name="hr">
    <xs:complexType>
      <xs:attribute name="width" type="xs:string" />
    </xs:complexType>
  </xs:element>

  <xs:element name="input">
    <xs:complexType>
      <xs:attribute name="type" type="xs:string" use="required" />
      <xs:attribute name="name" type="xs:string" />
    </xs:complexType>
  </xs:element>

</xs:schema>

Valid XML:

<br />
<hr width="100%" />
<input type="text" name="username" />

🔹 Common Use Cases

🖼️

Media Elements

Images, videos, audio files

<img src="pic.jpg" />
📝

Form Inputs

Input fields, checkboxes

<input type="text" />
🔗

Links & References

External resource links

<link href="style.css" />
⚙️

Configuration

Settings and flags

<config enabled="true" />

🧠 Test Your Knowledge

Can an empty element have attributes?