Xsd Elements in XML
? XSD Elements in XML
In XML Schema Definition (XSD), elements define the building blocks of an XML document. An element can hold text, other elements, or both — and its structure, type, and constraints are defined using <xs:element>.
? Basic Element Declaration
? In XSD:
<xs:element name="name" type="xs:string"/>? In XML:
<name>John</name>This says the <name> element must contain a string.
? Element Types
1. Simple Type
Contains only text, no child elements or attributes.
<xs:element name="age" type="xs:integer"/>2. Complex Type
Can contain elements, attributes, or both.
<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>? Element Occurrence Constraints
Use minOccurs and maxOccurs to control how often an element appears.
<xs:element name="phone" type="xs:string" minOccurs="0" maxOccurs="3"/>Means: the <phone> element is optional and can appear up to 3 times.
? Default and Fixed Values
? Default value (can be overridden in XML):
<xs:element name="country" type="xs:string" default="USA"/>? Fixed value (cannot be changed):
<xs:element name="currency" type="xs:string" fixed="USD"/>? Named vs Anonymous Elements
? Named Element (Global scope)
<xs:element name="title" type="xs:string"/>? Anonymous (Inline type definition)
<xs:element name="price"> <xs:simpleType> <xs:restriction base="xs:decimal"> <xs:minInclusive value="0.0"/> </xs:restriction> </xs:simpleType></xs:element>? Example: Complete Element in XSD
? XML:
<book> <title>XML Made Easy</title> <author>Jane Doe</author></book>? XSD:
<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:complexType></xs:element>? Summary Table
| Feature | Description |
|---|---|
type | Defines data type (e.g., xs:string) |
minOccurs / maxOccurs | Controls occurrence |
default / fixed | Sets default or fixed values |
simpleType | Element with text only |
complexType | Element with children/attributes |
Would you like a downloadable XSD/XML sample or a guide on how to validate XML against an XSD?