XML: Comments and declarations

By : |November 22, 2000 0

In the previous section we learnt how to create elements and declare their
content scheme. Let’s begin this session with attributes.

Attributes are modifiers of elements, which have names and values following
the same rules as Element names. That is, they are ‘case-sensitive’, must start
with a letter or an underscore character, etc. Elements can have one or more
attributes. An attribute is defined in the start tag with the attribute name
separated from the value by an equals ( = ) sign and the value inside single or
double quotes.

For example, in 314-666-1897; “number” is the
element name, “main” is the attribute name and “true” is the
value of “main”.

Attribute list declarations
Attribute list declarations identify which elements may have attributes,
what attributes they may have, what values the attributes may hold, and what
value is the default. A typical attribute list declaration looks like this:





Type ( comedy | tragedy ) ‘good’ >

As we see in the above example, the element called “movies” has
three attributes:

  • Language, which is an ID and is required
  • Title, which is a string (character data) and is not required and
  • Type, which must be either comedy or tragedy and
    defaults to good, if no value is specified.

Each attribute in a declaration has three parts: a name, a type and a default
value. You are free to select any name you wish, subject to some slight
restrictions, but names cannot be repeated on the same element.

That’s about attribute list declarations. Now, let us see the different
types of attributes.

  1. ID: The value of an ID attribute must be a name. All of the ID values used
    in a document must be different. IDs uniquely identify individual elements
    in a document. Elements can have only a single ID attribute.
  2. CDATA: The CDATA attributes are strings and any text form is allowed.
  3. IDREF: An IDREF attribute’s value must be the value of a single ID
    attribute on some element in the document. The value of an IDREF attribute
    may contain multiple IDREF values separated by white spaces.
  4. ENTITY: An ENTITY attribute’s value must be the name of a single entity.
    The value of an “entities” attribute may contain multiple entity
    names separated by white space. (We will discuss entities in detail in the
    next session)
  5. NMTOKEN: The name token attributes are a restricted form of string
    attribute. In general, an “nmtoken” attribute must consist of a
    single word. But it does not have any other constraint like it does have to
    match another attribute or declaration. The value of an “nmtokens”
    attribute may contain multiple “nmtokens” values separated by
    white space.

You can specify that the value of an attribute must be taken from a specific
list of names. The different types in this are:

  1. #REQUIRED: The attribute must have an explicitly specified value on every
    occurrence of the element in the document.
  2. #IMPLIED: The attribute value is not required, and no default value is
    provided. If a value is not specified, the XML processor must proceed
    without one.
  3. #FIXED “value”: An attribute declaration may specify that an
    attribute has a fixed value. In this case, the attribute is not required,
    but if it occurs, it must have the specified value.

The XML parser ignores comments. Comments, as usual, are used for remarks by the
author for easy understanding of the code. The start tag for a comment is
. Comments must always be set outside the XML
markup, as the following example shows:


No Comments so fars

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.