Should i use attributes in xml




















In the last, sex is a child element. Both examples provide the same information. There are no rules about when to use attributes, and when to use child elements. Use child elements if the information feels like data. If you use attributes as containers for data, you end up with documents that are difficult to read and maintain.

Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data. Sometimes I assign ID references to elements.

This question have already made me scratch my head too. For me, it's a matter of semantics. It seems more natural for me to do. I generally use attributes for the minimum set of fields that make a node unique. In other words, they represent the primary key. This makes some things easier if you need to correlate XML with a relational database.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. XML Elements vs. Attributes [duplicate] Ask Question. Asked 11 years, 9 months ago.

Active 5 years, 3 months ago. Viewed 7k times. Improve this question. Community Bot 1 1 1 silver badge. Although the example is similar the question is slightly different. I found the answers to the question only somewhat useful while I found Guffa's answer and others to this question to be very helpful. Add a comment. Active Oldest Votes. Improve this answer. Guffa Guffa k gold badges silver badges bronze badges.

You could do it with elements -- the machine doesn't care. This is a stylistic issue, for the benefit of humans. Other than 65mph put's the unit of measure into the value and makes the 65 useless without parsing as a number.

Drew: the machine could care if there are millions of records over a slow connection. Not everyone has Gbps internet to their house, some places still have to deal with dialup. Consider trying to construct an XPath expression that looks for speeds greater than 65 mph or a transform that converts from mph to kph. Show 2 more comments. The points you list about elements are correct, and I would add the following: elements generally make prettier more readable diffs when you need to compare revisions of a file But sometimes using an element to model a data point is overkill -- particularly when you have a lot of small, heterogeneous data points within a single parent element.

We just launched W3Schools videos. Get certified by completing a course today! If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:.

Report Error. Your message has been sent to W3Schools. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.



0コメント

  • 1000 / 1000