XML Attributes: Name-Value Pairs and Usage

An XML attribute is a name-value pair written in an element's start tag. Attributes are useful for compact metadata or identifiers associated with an element.

XML attribute syntax Top ↑

<address type="residential">
  <door_no>234-ac</door_no>
  <street>Great Wall</street>
  <city>Alabama</city>
</address>

Here, type is the attribute name and residential is its value.

Important attribute rules Top ↑

  • Attribute values must be quoted with single or double quotes.
  • An element cannot contain two attributes with the same name.
  • Attribute names are case-sensitive.
  • Special characters must be escaped when required, such as &amp; for an ampersand in ordinary attribute data.
  • The order of attributes normally does not change the element's meaning.

Attribute or child element? Top ↑

There is no universal rule that all “data” must be elements and all “metadata” must be attributes. The best structure depends on the XML vocabulary and how the data will be processed.

Attributes work well for compact values such as identifiers, categories, flags or types. Child elements are often easier when values can repeat, contain structured content, or need to grow into a richer structure.

<member id="M42">
  <name>Ravi</name>
  <address type="office">...</address>
</member>

Existing XML sample with attributes Top ↑

Open sample XML file with attributes

Frequently Asked Questions Top ↑

Must XML attribute values use quotes?
Yes. Attribute values must be quoted.

Can two attributes on one element have the same name?
No. Duplicate attribute names on the same element make the XML not well-formed.

Should all values be XML attributes?
No. Choose attributes or child elements according to the structure and semantics of the XML vocabulary.

What is XML? XML file structure





plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer