XML Attributes

Most XML documents tend to look a bit more complicated than previous examples suggest, owing to the presence of attributes.

Markup language attributes specify additional parameters that modify a given element; multiple attributes can modify a single element, and each attribute can specify multiple values.

In general, markup language attributes appear within the opening tag of a given element:

<tag attribute="value">content</tag>

Attributes can also be present within self-closing elements:

<tag attribute="value" />

As a practical example, the paragraph (<p>) element is used to designate a paragraph of text in an HTML document:

<p>The quick brown fox jumps over the lazy dog.</p>

When interpreted by a web browser, the sentence between the <p> tags of the paragraph element would appear as its own paragraph, like so:

The quick brown fox jumps over the lazy dog.

But the paragraph element can be modified by the style attribute, to produce all kinds of stylistic effects:

<p style="font-family:cursive; font-weight:bold;">The quick brown fox jumps over the lazy dog.</p>

Here, the style attribute (highlighted in red), as applied to the paragraph element, specifies multiple values that will cause all content within this particular paragraph element to use the cursive font family; the text will also be bolded. When interpreted by a web browser, the above paragraph element would appear like so:

The quick brown fox jumps over the lazy dog.

Recall that elements can be nested; this means that any given element can include other elements.

For example: the unordered list (<ul>) and list item (<li>) elements are used to create lists in HTML documents. Once the unordered list element is opened, any list item elements within are interpreted by web browsers to create bullet points on a list, like so:

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>

Interpreted by a web browser, this produces a bulleted list that would appear as follows:

  • Item 1
  • Item 2
  • Item 3

As each of the three list item elements are nested within the unordered list element, anything that modifies the unordered list element also modifies the list item elements. For example:

<ul style="font-family:cursive; font-weight:bold;">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>

Again, the style attribute has been used to modify the unordered list element (and everything nested within it), specifying bolded text using the cursive font family. This produces the following results

  • Item 1
  • Item 2
  • Item 3

Note that attributes can also be used to further modify or even override the attributes of parent elements on an individual basis. For example, the style attribute could be used to make Item 1 blue or Italicized, or it could be used to specify a different font family. These parameters would take precedence over those specified by the style attribute modifying the unordered list element.

Having made it this far, you are well on your way to understanding the basics of XML. To learn about XML namespaces and prefixes, click the links below.