Adding Attributes

Within the sample document (Listing 2.2), there are several open tags that include extra information in the form of

						name="data"

These name/value pairs are called attributes. An attribute is generally a piece of information that is related to an element, but is not necessarily part of the content. Deciding what content should be encoded as an attribute and what should be left as element content is a topic that still generates a great deal of discussion among XML experts. Without digging too deeply into the various attribute philosophies, treating attributes as “keys” to the underlying element content is a fairly standard practice among experienced XML document authors.

You should keep a few restrictions in mind when using attributes to encode information:

  • Attribute names must be unique (that is, you cannot have two attributes with the same name on a single element).

  • Markup characters (such as <>) cannot appear directly in attribute values. It is possible to include them using the built-in character references &lt; and &gt;, which are covered later in the chapter.

  • The sequence of attributes in an element cannot be guaranteed. If an element is written as <el a1="foo" a2="bar">, the attributes may be returned by the XML parser as a2 followed a1. Don't depend on attribute ordering for the logic of your application.

Attributes usually consist of simple character data. When a document is validated using a document type definition (DTD), attributes can be declared to contain special types such as IDs, entities, and name tokens. These special types are used by a validating XML parser to enforce the contents and relationships between attributes. For more information about advanced XML topics such as attribute declaration, an excellent XML reference book to consult is XML in a Nutshell.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.75.133