SyntaxStudy
Sign Up
XML Element Naming Rules and Best Practices
XML Beginner 1 min read

Element Naming Rules and Best Practices

XML element names must follow specific lexical rules. A name must start with a letter (Unicode category L) or an underscore. Subsequent characters may be letters, digits, hyphens, underscores, periods, or combining characters. Names beginning with the reserved prefix "xml" (in any case combination) are reserved for use by the XML specification itself. Whitespace is never allowed inside a name. Beyond the hard rules, there are widely observed conventions. CamelCase (firstName), hyphenated-lowercase (first-name), and underscore_separated (first_name) are all used in different ecosystems. Namespace-aware applications typically use prefixes separated by a colon (xsd:element), though the colon is syntactically significant in XML namespaces and should not be used as a cosmetic separator in non-namespaced documents. Choosing meaningful, consistent names is important because XML is intended to be self-documenting. Avoid abbreviations that are not universally understood, avoid single-character names except for well-known mathematical notation, and keep names as specific as necessary to prevent ambiguity. When designing a vocabulary used by multiple systems, publish a schema and naming guide early to prevent divergent interpretations.
Example
<?xml version="1.0" encoding="UTF-8"?>
<namingExamples>

    <!-- Valid element names -->
    <firstName>Alice</firstName>
    <first-name>Alice</first-name>
    <first_name>Alice</first_name>
    <_internalId>42</_internalId>
    <item1>value</item1>

    <!-- Names with Unicode letters are valid -->
    <données>some data</données>

    <!--
        INVALID names (shown as comments to keep document well-formed):

        <1stItem>   — starts with digit
        <my item>   — contains space
        <my@item>   — contains @
        <xmlFoo>    — starts with 'xml' (reserved)
        <-bar>      — starts with hyphen
    -->

    <!-- Namespace-prefixed names (valid when namespace declared) -->
    <book xmlns:dc="http://purl.org/dc/elements/1.1/">
        <dc:title>Learning XML</dc:title>
        <dc:creator>Jane Doe</dc:creator>
        <dc:date>2025-01-15</dc:date>
    </book>

</namingExamples>