Understanding XML: Elements vs Attributes

  • Thread starter Thread starter DaveC426913
  • Start date Start date
  • Tags Tags
    xml
Click For Summary
The discussion centers on the design differences between elements and attributes in XML. The main consideration for deciding whether to use an element or an attribute is the nature of the data being represented. Elements are typically used for user-facing content, while attributes are often reserved for metadata or behind-the-scenes values. A common guideline suggests that data should be placed in elements, whereas metadata belongs in attributes. Preferences vary among users, with some favoring attributes for simpler data unless the data is large or contains special characters, in which case elements with CDATA sections are preferred. The conversation highlights that the choice is not always clear-cut and depends on design and practicality.
DaveC426913
Gold Member
Messages
24,108
Reaction score
8,241
What's the design difference between elements and attributes? What I'm asking is: what makes you decide that something should be an element versus an attribute?

Code:
<pet>
  <name>Spike</name>
</pet>
i.e: which is better?
Code:
<pet name="Spike">
</pet>
Does the difference lie in the fact that 'Spike' is likely to be user-facing content - as opposed to some behind-the-scenes value such as a width of 200?
 
Last edited:
Technology news on Phys.org
Thank you. The fact that this is a common question is good to know. And I do seem to be right on the money:

You can use either, it's a matter of design and practicality which you decide to use. But in general, the rule of thumb seems to be:
"...data goes in elements, metadata in attributes..."
 
Last edited:
Generally, i prefer attributes, unless the data is large or when the data may contain some special characters, such as <, >, in which case i opt for text nodes with <![CDATA[...]]>.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 80 ·
3
Replies
80
Views
7K
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
29
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
5K
  • Poll Poll
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K