Julian Habekost
Julian Habekost

Reputation: 272

Can element and attribute names be the same in XML?

Is

<root>
    <x x="y" />
</root>

valid XML?

Upvotes: 2

Views: 2585

Answers (1)

kjhughes
kjhughes

Reputation: 111726

Yes, it's fine to have elements and attributes with the same name, but note the following:

  • It's not commonly done because it can be confusing and rarely models real world entities accurately.
  • Terminology:
    • Well-formed XML may have elements and attributes with the same names because there's nothing in the W3C Recommendation prohibiting it.
    • Valid XML may have elements and attributes with the same name if permitted by an associated schema (DTD, XSD, etc) because validity is function of the relationship between an XML document and a schema, not an intrinsic property of an XML document.

Upvotes: 3

Related Questions