Welcome to The Coding College! In this article, we’ll explore XSD Simple Elements, which are fundamental building blocks in XML Schema Definitions (XSD). They represent the most basic types of data, such as strings, numbers, or dates. Understanding how to define and use simple elements in XSD is essential for creating structured and validated XML documents.
What are XSD Simple Elements?
An XSD simple element is an XML element that contains only text, without any attributes or child elements. Simple elements are ideal for defining straightforward data, such as names, numbers, dates, or Boolean values.
Key Characteristics of Simple Elements:
- Contain only text content.
- Cannot have child elements or attributes.
- Use built-in XSD data types (like
xs:string
,xs:integer
, orxs:date
) or user-defined types.
Defining Simple Elements in XSD
To define a simple element, you use the <xs:element>
tag and specify a type. Here’s the syntax:
<xs:element name="elementName" type="dataType"/>
Examples of Simple Elements
Example 1: Defining a String Element
<xs:element name="firstName" type="xs:string"/>
This defines an element called firstName
that can contain any string.
Corresponding XML Document:
<firstName>John</firstName>
Example 2: Defining a Numeric Element
<xs:element name="age" type="xs:integer"/>
This defines an element called age
that can contain only integer values.
Corresponding XML Document:
<age>30</age>
Example 3: Defining a Date Element
<xs:element name="birthDate" type="xs:date"/>
This defines an element called birthDate
that must follow the YYYY-MM-DD
format.
Corresponding XML Document:
<birthDate>1995-07-24</birthDate>
Common XSD Data Types for Simple Elements
XSD offers a wide range of built-in data types for simple elements:
Data Type | Description | Example Value |
---|---|---|
xs:string | Textual data | "Hello" |
xs:integer | Whole numbers | 42 |
xs:decimal | Decimal numbers | 99.99 |
xs:date | Date in YYYY-MM-DD format | 2023-06-01 |
xs:boolean | Boolean values (true or false ) | true |
xs:time | Time in HH:MM:SS format | 14:30:00 |
xs:duration | Duration in ISO 8601 format | P1Y2M3D |
xs:anyURI | A URI/URL string | https://example.com |
Restricting Simple Elements
You can further restrict the values of simple elements using facets like length
, minLength
, maxLength
, minInclusive
, and maxInclusive
.
Example: Restricting a String Element
<xs:element name="zipCode">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Explanation:
- The
zipCode
element must be exactly 5 characters long.
Corresponding XML Document:
<zipCode>12345</zipCode>
Example: Restricting a Numeric Element
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Explanation:
- The
age
element must be an integer between 0 and 120.
Corresponding XML Document:
<age>25</age>
Combining Simple Elements with Attributes
Although simple elements cannot have child elements, they can include attributes when defined as part of a complex type.
Example: Simple Element with Attributes
<xs:element name="product">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="category" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Corresponding XML Document:
<product category="electronics">Smartphone</product>
Practical Example: A Complete XML Schema
Here’s a practical example to put everything together.
XML Document
<person>
<name>John Doe</name>
<age>29</age>
<birthDate>1994-08-15</birthDate>
</person>
XSD File
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="birthDate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Advantages of XSD Simple Elements
- Simplicity: Ideal for straightforward data types.
- Validation: Ensures data integrity by enforcing rules.
- Customization: Allows restrictions and patterns for precise control.
Conclusion
XSD simple elements provide the foundation for creating valid and structured XML documents. Whether you’re defining text, numbers, or dates, mastering simple elements is key to working with XML schemas.
For more in-depth tutorials, visit The Coding College and take your XML knowledge to the next level!