Im starting to learn a bit of xml and xslt, and I read on w3c W3Schools that they recommend you use elements instead of attributes. Whats your practice and why would you not want attributes -
is there any point where you want them really?
Attributes are specifications to the data contained by the element. For example: A title is an element, the title's language an attribute.
<title lang="en">A simple title</title>
Attributes should never need to be rendered. They should contain additional information on the data, not data itself.
The primary technical restrictions on attributes:
Attribute names are unique within the scope of the element they decorate.
Attribute order is not significant; XML processors are not required to process an element's attributes in the physical order in which they appear in the document.
Attribute values must be text content. They cannot contain elements, comments, or processing instructions. They cannot contain CDATA. They cannot contain unescaped markup characters.
These restrictions pretty strictly govern what it is and is not appropriate to use attributes for. Using attributes to model a map or dictionary whose keys are XML names and whose values can sensibly be represented as strings: good. Using attributes to serialize data whose order of processing is important: bad. (I'm looking at you, XAML.) Storing serialized XML in an attribute value: possible, but usually indicates that you're doing something wrong.
The idea that elements are for data and attributes are for metadata is a throwback to the days when XML wasn't used as a general-purpose serialization format. It's broadly ignored, largely because, in a lot of cases, the ability to represent name/value pairs tersely is of more obvious benefit than separation between data and metadata.
精彩评论