Many times, the XML elements contains extra information. This extra information is stored in some new attributes. The attributes in XML are extra information that we provide for the elements. Element can have any number of attributes with unique names. But before that, we must describe how attributes look like and how to use them.
What are attributes?
The attributes are for elements, as it enhances the feature of element by providing some extra information in the form of name and value pair.
From the above example, you can see that the XML attributes are name/value pairs. You can put any number of attributes for element, and you can also, put attributes in any order, but the name comes first and then the value of the attribute.
The value of an XML attribute is enclosed with a single quote or a double quote. All the attributes in an element are separated by spaces and you can write them in any order.
<address type="Home"> <city>Banglore</city> </address>
In the above example, we have an attribute type which describes the type of address for the element <address>. The value is Home which is enclosed within double quotes. Similarly, we can enclose an attribute value with single quotes.
Here we have another example of attribute value enclosed within a single quote.
Sometime the value contains quote within the text, in that case, use opposite kind of quote.
<greeting tag="It's simple welcome">Welcome</greeting> Second example, <greeting birthday='my "Birthday" today'> Happy Birthday</greeting>
The above code use opposite quote type when the internal quote is either double quote or single quote. The outer quote for attribute must be opposite of quote used in the value string. The quotes can be replaced with HTML symbol , when used as value. These values are given in the following table.
|Character Special||Symbol||HTML code|
|Single left quote||‘||‘ or '|
|Single right quote||‘||’ or '|
|Double left quote||“||“ or "|
|Double right quote||“||” or "|
We can replace the quotes with any of the code in XML attribute values and it will work fine. This will also reduce the confusion with quotes because quotes inside of the text is now a specific code.
Consider the following example.,
<greeting tag="thank's">Thank you</greeting>
Number of XML attributes
As I mentioned earlier, you can have many XML attributes for a single element, however, there are some rules to follow while describing the attributes.
Do not repeat same attribute
You should not repeat the same attribute with different values. Consider the following XML element that repeats same attribute multiple times.
<?xml version="1.0" encoding="UTF-8"?> <student name="Ram" name="Seeta" name="Lakshman"> <course>Ramayana</course> </student>
Declaring the XML element with same attribute with different value is invalid. Here is the result of validation check for the above XML document.
Image Source: Validate XML – Online XML Tools
The error indicates that the attribute is redefined multiple times which is true. Instead of this, you can use single attribute to declare multiple values.
<?xml version="1.0" encoding="UTF-8"?> <student name="Ram Seeta Lakshman"> <course>Ramayana</course> </student>
This will pass the validation test.
Note: Some parser will not allow such declarations and consider multiple values invalid.
The best practice is to declare multiple attributes as child elements of the parent element. Therefore, we can use our example and redefine the XML document as:
<?xml version="1.0" encoding="UTF-8"?> <student> <name>Ram</name> <name>Seeta</name> <name>Lakshman</name> <course>Ramayana</course> </student>
If you validate the document now, it passes the validation test.
Attributes with DTD
In this section, you will learn about certain attributes that are constrained to certain types if you use DTD. There are two special attributes used called the ID and IDREF attribute. This attribute tells that the element has a unique ID to an XML processor within the document. The IDREF attribute is reference to ID attribute in the document. There are few point you must remember about ID and IDREF attributes.
- ID values must start with underscore or letter, not numbers, and contains only letters and numbers with some special symbols.
- Two elements cannot have same ID attributes.
- IDREF value must match the value of ID attribute. No non-existent ID allowed.
- It is not guaranteed that the ID and IDREF will be parsed by XML Processors that don’t use DTD. For example, SOAP do not use DTD information.
You can declare an attribute in DTD with restricted values, such as “month” that contains only month names as values. So month = “Saturn” will be rejected if not in the list. Such is the case with restricted attributes.
Reserved XML Attributes
The reserved XML attributes are for specific purposes and begins with xml:. There are many such XML attributes, but I will talk about only four important ones here.
The xml:lang and xml:space is reserved for xml version 1.0. The xml:link and xml:base are defined by XML link which is standard to describe how to link XML documents.
This attribute classifies an element based on the language of its content. In a document, xml:lang =”en” will describe an element with English content. The XML parser can render contents based on language only, and user will receive document based on his language preference.
This attribute preserves whitespaces in the content of an element. The whitespace in general sense is means space between the content of an XML element. However, the whitespace means newline, carriage return, tab, and space here. The XML specifies whitespaces as:
Significant Whitespace – the general whitespace between contents and must be preserved.
Insignificant Whitespace – These are whitespaces outside of content area and preserved using the xml:space attribute.
It only works if the XML processor recognize it and parse it. Use of this attribute like xml:lang must be declared in xml schema or DTD There are two values for xml:space , preserve and default.
When you use preserved value, all whitespaces (insignificat) is preserved between parent element and the child elements and their children as well.
The default value accepts the processing of xml:space according to XML processor options.
Tells the xlink processor that the element is a link element. More on this when we talk about xlink.
This attribute provides a base URL for the xlink:href attribute. The xlink:href attribute will only contains the html file. Consider the following example, to understand this.
<student xml:base = "https://www.my-schoool-example.com/" > <course xlink:type="locator" xlink:label="CSE" xlink:href="courses/cse.html" </course> </student>
In this article, you learned about XML attribute which is name/value pairs and how to use them in XML documents. Also, some attributes are restricted from DTD or schema file. There special reserved XML attributes in XML that are useful and must be declared inside a DTD before use.