Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

Attributes

An attribute is the association of an attribute name with an attribute value. For example, the markup describing this book might be represented as follows:

<book edition="1">
<title>Sams Teach Yourself XML in 10 Minutes</title>
</book>

The book element has an edition attribute, which has the value of 1.

An attribute is allowed only in the start tag of an element. The value of an attribute must be enclosed between paired double quotation marks, such as

<book edition="1">

or between paired apostrophes:

<book edition='1'>

It is an error to mix these two types of delimiters. The next two line of code would cause a well-formedness error because the delimiters of the attribute value are not paired.

In the following line, the quotation mark before the attribute value is not paired with a matching quotation mark:

<book edition="1'>

Here, the apostrophe before the attribute value does not have a matching apostrophe to delimit the end of the attribute value:

<book edition='1">

Attributes Must Be Unique

The start tag of any XML element must not contain duplicate attribute names.

For example, the following code will generate an error because there are two number attributes in the start tag of the chapter element.

<chapter number="1" author="DPT" number='1'>
<!-- Some text would go here -->
</chapter>

No External Entity References

Attribute values are not allowed to contain external entity references.

For example, imagine that you had declared an external entity called copyright:

<!ENTITY copyright SYSTEM "copyright.xml">

You could not use it in an attribute value, such as in the following code:

<book status="&copyright;">
<!-- Content goes here -->
</book>

However, attribute values are allowed to contain references to internal parsed entities.

For example, imagine that the internal subset of the DTD included an entity declaration as follows:

<!ENTITY BigText "font-size:72">

It could be used to define the style of text nested in an SVG text element, as follows:

<text style="&BigText;">This text is big!!</text>

No < in Attribute Values

The value of an XML attribute is not allowed to include the < character, either directly or indirectly.

It is not legal to write this:

<math comparison="3<4"></math>

It is also an error to include an internal entity reference to an entity, such as

<!ENTITY lessthan "3<4">

which is referenced like this:

<math comparison="&lessthan;"></math>

After the entity reference was replaced by its replacement text, it would result in the same illegal code:

<math comparison="3<4"></math>

Following the well-formedness constraints described for elements and attributes in the preceding sections will help you avoid many of the common well-formedness errors. As your documents become more complex, other well-formedness constraints might become important.

  • + Share This
  • 🔖 Save To Your Account