XSD Text-Only
Elements containing only text content
📝 What are Text-Only Elements?
Text-only elements contain simple text content without child elements. They can have attributes but their content is pure text, making them perfect for storing simple data values.
<!-- Text-only examples -->
<name>John Doe</name>
<price currency="USD">29.99</price>
Defining Text-Only Elements
Text-only elements use simpleContent to define text with optional attributes. The element contains only text data, no child elements, but can include attributes for metadata.
🔸 Simple Text Element
<xs:element name="message" type="xs:string" />
Valid XML:
<message>Hello World!</message>
🔹 Text with Attributes
Most text-only elements include attributes to provide additional context or metadata about the text content. Use simpleContent with extension to add attributes to text elements.
<xs:element name="price">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="currency" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Valid XML:
<price currency="USD">99.99</price>
<price currency="EUR">89.50</price>
🔹 Restricted Text Content
You can restrict text content using patterns, enumerations, or ranges to ensure data validity and consistency:
<xs:element name="status">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="active" />
<xs:enumeration value="inactive" />
<xs:enumeration value="pending" />
</xs:restriction>
</xs:simpleType>
</xs: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>
Valid XML:
<status>active</status>
<age>25</age>
🔹 Complete Example: Contact Information
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="email">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[^@]+@[^@]+\.[^@]+" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="phone">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Valid XML:
<contact>
<name>Jane Smith</name>
<email>[email protected]</email>
<phone type="mobile">555-1234</phone>
</contact>
🔹 Common Data Types
xs:string
Any text content
<name>John</name>
xs:integer
Whole numbers
<quantity>5</quantity>
xs:decimal
Decimal numbers
<price>19.99</price>
xs:boolean
True or false values
<active>true</active>