XML Menu


XML Attributes




Attributes are one of the two main building blocks of XML. Attributes are name-value pairs associated with an element. Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements, they define properties of elements.

Syntax
<element-name attribute1="value1" attribute2="value2">
....content..
</element-name> 

example
<student gender="female">
....content..
</student> 

Attributes rules

  • Attributes consist of a name and a value separated by an equals sign. The name follows the same rules as element names.
  • The attribute value must be in quotes. You can use either single or double quotes, but you can’t mix them in a single attribute
  • There must be a value part, even you cant left value just empty.
  • Attribute names must be unique per element.

example
<department>
  <student gender="female">
    <name>.....</name>
	<rollno>....</rollno>
  </student>
</department>  

Elements vs. Attributes

example-1
<department>
  <student gender="female">
    <name>.....</name>
	<rollno>....</rollno>
  </student>
</department>  

example-2
<department>
  <student>
    <name>.....</name>
	<rollno>....</rollno>
	<gender>female</gender>
  </student>
</department>  

In the first example, gender is an attribute. In the second example, gender is an element. Both examples provide the same information. There are no rules about when to use attributes or when to use elements in XML.

Avoid XML Attributes because they cannot contain multiple values, they cannot contain tree structures and they are not easily expandable


Next Topic :Document Type Declaration(DTD)