Bookmark and Share

XML Attribute

XML Attributes are very similar to HTML attributes that you may already have a grasp of. An attribute appears within the opening tag of an element.

Advertise on Tizag.com

XML Attribute Syntax

Unlike in HTML, XML requires that all XML attributes have a value. This means all attributes have to be equal to something! Below is the correct way to create an attribute in XML.

XML Code:

<student active="true">
	<name>Robert</name>
	<grade>A+</grade>
</student>

The attribute active is valid because we set it to a value true. Here is an example of shorthand that is sometimes used in HTML, but is not valid XML.

XML Code:

<student active>
	<name>Robert</name>
	<grade>A+</grade>
</student>

This XML attribute is wrong because it was not set to a value.

XML Attribute Quotation Usage

The types of quotes that you use around your attribute values is entirely up to you. Both the single quote (') and the double quote (") are perfectly fine to use in your XML documents.

The only instance you might want to use one quotation over the other is if your attribute value contains a quotation or apostrophe of its own. Below are a couple of real world examples.

XML Code:

<student nickname='Rob "The Dog"'>
	<name>Robert</name>
	<grade>A+</grade>
</student>

XML Code:

<student group="Pete's Pandas">
	<name>Robert</name>
	<grade>A+</grade>
</student>
Bookmark and Share




Download Tizag.com's XML Book

If you would rather download the PDF of this tutorial, check out our XML eBook from the Tizag.com store.

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials