9️⃣ 🔢 XSD Data Types
Estimated reading: 4 minutes 323 views

XSD String – Define and Restrict Textual Content in XML

Introduction – Why Use xs:string in XSD?

In XML, most real-world data is stored as text—names, titles, descriptions, IDs, and more. XSD provides a powerful set of string-based types like xs:string, xs:normalizedString, and xs:token to define and validate textual content. These types can also be restricted by length, pattern, enumeration, and whitespace handling to ensure data integrity.

In this guide, you’ll learn:

  • How to use xs:string and related types in your XML Schema
  • Differences between string, token, and normalizedString
  • How to apply restrictions like length, pattern, and enumeration
  • Real-world examples and best practices

What Is xs:string?

xs:string is the default XSD data type for text. It allows any sequence of Unicode characters including whitespace.

Basic usage:

<xs:element name="title" type="xs:string"/>

Valid XML:

<title>Advanced XML Guide</title>

Other String-Related Types

TypeDescription
xs:stringAny text including line breaks and whitespace
xs:normalizedStringWhitespace normalized to spaces (no tabs, newlines)
xs:tokenLike normalizedString, but also trims leading/trailing spaces
xs:languageISO language codes (e.g., "en", "fr", "de")
xs:NameValid XML names (no colons or invalid characters)
xs:NMTOKENName tokens (like identifiers)
xs:NCNameXML name without colons

Restricting String Content

Length Restriction

<xs:element name="username">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:minLength value="4"/>
      <xs:maxLength value="12"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Valid:

<username>john123</username>

Enumeration

<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>

Only the specified values are allowed.


Pattern Matching (Regex)

<xs:element name="zipCode">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="\d{5}(-\d{4})?"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Allows formats like 12345 or 12345-6789.


Best Practices for XSD String Types

  • ✔️ Use xs:string for freeform text like names and descriptions
  • ✔️ Use xs:token to eliminate extra whitespace (good for codes and keys)
  • ✔️ Apply pattern and enumeration to validate fixed formats
  • ✔️ Use xs:language and xs:Name for semantic constraints
  • Avoid using xs:string alone for structured IDs or codes—add rules

Summary – Recap & Next Steps

XSD string types let you define and validate text values with precision. Whether it’s a simple label or a regex-matched identifier, you can control every aspect of text input with xs:string and its related types.

Key Takeaways:

  • Use xs:string as the base type for all textual data
  • Apply minLength, maxLength, pattern, or enumeration for stricter control
  • Use token, normalizedString, and language for specialized cases

Real-world relevance: Used in usernames, titles, descriptions, ISO codes, postal codes, tags, IDs, and labels across XML documents.


FAQs – XSD String

What’s the default string type in XSD?
It’s xs:string—the most basic type for any textual content.

When should I use xs:token instead of xs:string?
Use token when you want to trim spaces and normalize input.

Can I validate email addresses with XSD?
Yes, using xs:pattern with a regular expression.

Is whitespace significant in xs:string?
Yes. If you want to normalize whitespace, use normalizedString or token.

Can I reuse a restricted string type?
Yes. Define it as a global <xs:simpleType> and assign it via type="...".


Share Now :
Share

XSD String

Or Copy Link

CONTENTS
Scroll to Top