Skip to content

[@typespec/xml] Validate XML element, attribute, and prefix names against W3C XML Name grammar #12663

Backlog

Description

@tadelesh

Clear and concise description of the problem

@Xml.name (in @typespec/xml) and @encodedName("application/xml", ...) currently accept arbitrary strings without checking whether the value is a valid XML Name per the W3C XML 1.0 specification (Name production):

NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
NameChar      ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Name          ::= NameStartChar (NameChar)*

Similarly, @Xml.ns(namespace, prefix) validates that namespace is a valid URI (ns-not-uri), but does not validate that a string prefix is a valid XML NCName.

Reproduction

import "@typespec/xml";

using TypeSpec.Xml;

@Xml.name("part\"number")
model Widget {
  @Xml.attribute
  @Xml.name("attr`tick")
  id: string;

  @Xml.name("123-leading-digit")
  count: int32;

  @Xml.ns("https://example.com/ns", "bad prefix")
  value: string;
}

Today this compiles with zero diagnostics even though none of "part\"number", "attr`tick", "123-leading-digit", or "bad prefix" can form well-formed XML markup on the wire, and invalid names propagate downstream into emitters (see Azure/typespec-azure#5936 for background).

Proposal

Add validation in @typespec/xml (and/or @encodedName for XML media types such as application/xml and text/xml) to check XML element names, attribute names, and namespace prefixes against the W3C XML Name / NCName productions and report a diagnostic when an invalid XML name is provided.

Checklist

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Activity

  1. added
    feature
    on Nov 25, 2026
  2. github-actions commented on Nov 25, 2026

    @github-actions
    Contributor

    🎯 Issue Triage
    @Xml.name, XML-targeted @encodedName, and string prefixes passed to @Xml.ns accept values that cannot form valid XML names. The request is to validate XML Name/NCName syntax before invalid names reach emitters.
    Category: Feature Request
    Area: Unable to determine

    🔍 Analysis

    The primary component is @typespec/xml, but the configured repository labels do not include an XML library area. XML-targeted @encodedName may also involve compiler encoding-name handling.

    The apparent gap is that these decorators validate argument types, and @Xml.ns validates the namespace URI, but none enforce the XML 1.0 lexical grammar for element names, attribute names, or namespace prefixes. This allows invalid wire names to propagate to emitters. No strong duplicate was found; microsoft/typespec-azure#5936 is related downstream context rather than the same request.

    📝 Suggested next steps
    • Decide whether XML-name validation belongs exclusively in @typespec/xml or also in XML-media-type handling for @encodedName.
    • Add shared XML Name and NCName validation with targeted diagnostics.
    • Add tests covering valid Unicode names, invalid starting characters, punctuation, and namespace prefixes.

    Generated by Agentic Triage

  3. added
    design:needed
    on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    design:neededfeatureneeds-areatriaged:core

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions