How to put space character into a string name in XML?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If by "name" you mean an XML element name or attribute name, the answer is simple: you cannot put a space there. XML names follow strict rules, so spaces are invalid in tag names and attribute names; if you need spaces in actual text values, that is allowed, but it belongs in element content or attribute values rather than in the name itself.
XML Names Cannot Contain Spaces
Element names and attribute names are XML identifiers. They are not arbitrary strings.
This is invalid XML:
So is this:
The parser fails because the space breaks the identifier into multiple tokens. If you need a human-readable label, use a valid XML name such as employeeName, employee_name, or employee-name.
Put Spaces in Values, Not in Names
Spaces are perfectly fine inside text content and attribute values.
Those spaces are data, not part of the XML identifier, so they are legal.
This distinction is the key point:
- Names identify structure
- Values carry text data
Once you separate those roles, most XML whitespace confusion disappears.
Preserve Significant Spaces in Content
If your real question is about preserving spaces inside string values, XML allows that too. In simple cases you can just write the text:
If leading or repeated spaces are significant, you may also need xml:space="preserve" depending on how the document is consumed:
That tells processors not to normalize away whitespace that you want to keep.
Use Escapes Only for Special Cases
You do not need to escape an ordinary space inside element text or attribute values. But if you are generating XML programmatically and need a numeric character reference, space is character 32, so the reference is:
That still does not make spaces legal in element names. Character references apply to content, not to identifier syntax.
Example in Python
If you are generating XML from code, create valid tag names and put the human-readable string in the text or attribute value.
This produces valid XML because the names are identifier-safe while the values freely contain spaces.
When You Need a Human-Readable Label
Sometimes developers really want the XML to display a friendly label such as "employee name." In that case, keep the XML name machine-safe and store the display label as data:
This pattern is common in configuration files and metadata formats because it separates structural identifiers from user-facing text.
Common Pitfalls
- Trying to force spaces into element or attribute names always produces invalid XML.
- Confusing tag names with string values leads to malformed output and parser errors.
- Using character references such as
 does not bypass XML naming rules. - Forgetting
xml:space="preserve"can cause leading or repeated spaces in text content to be normalized by downstream tools.
Summary
- Spaces are not allowed in XML element names or attribute names.
- If you need spaces, put them in element text or attribute values instead.
- Use valid identifier styles such as
employeeNameoremployee-namefor XML names. - Preserve significant whitespace in values with
xml:space="preserve"when needed.

