XML Attribute

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

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>