XML Elements and Attributes

by Dinesh 2012-07-22 22:30:45

XML Elements and Attributes

Because XML is designed to describe data and documents, the W3C XML Recommendation, which can be found buried in the links at http://www.w3.org/ XML, is very strict about a small core of format requirements that make the difference between a text document containing a bunch of tags and an actual XML document. XML documents that meet W3C XML document formatting recommendations are described as being well-formed XML documents. Well-formed XML documents can contain elements, attributes, and text.

Elements:

Elements look like this and always have an opening and closing tag:

There are a few basic rules for XML document elements. Element names can contain letters, numbers, hyphens, underscores, periods, and colons when namespaces are used (more on namespaces later). Element names cannot contain spaces;underscores are usually used to replace spaces. Element names can start with a letter, underscore, or colon, but cannot start with other non-alphabetic characters or a number, or the letters xml.

Aside from the basic rules, it’s important to think about using hyphens or periods in element names. They may be considered part of well-formed XML documents, but other systems that will use the data in the element name such as relational database systems often have trouble working with hyphens or periods in data identifiers, often mistaking them for something other than part of the name.

Attributes:

Attributes contain values that are associated with an element and are always part of an element’s opening tag:

The basic rules and guidelines for elements apply to attributes as well, with a few additions. The attribute name must follow an element name, then an equals sign (=), then the attribute value, in single or double quotes. The attribute value can contain quotes, and if it does, one type of quote must be used in the value, and another around the value.

Text:

Text is located between the opening and closing tags of an element, and usually represents
the actual data associated with the elements and attributes that surround the text:
text

Text is not constrained by the same syntax rules of elements and attributes, so virtually any text can be stored between XML document elements. Note that while the value is limited to text, the format of the text can be specified as another type of data by the elements and attributes in the XML document.

Empty elements:

Last but not least, elements with no attributes or text can also be represented in an
XML document like this:

This format is usually added to XML documents to accommodate a predefined data structure.
816
like
0
dislike
0
mail
flag

You must LOGIN to add comments