Prerequisite: XML | Basics
The XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element.
Syntax:
<element_name attribute1 attribute2 ... > Contents... </element_name>
In the above syntax element_name is the name of an element which can be any name. The attribute1, attribute2, … is XML attribute having unique attribute name. Then in the content section, any message can be written and at the end, the element name is ended.
Below some examples are given which illustrate the above syntax:
Example 1:
<text category = "message">Hello Geeks</text> |
In the above example, XML element is text, the category is the attribute name and message is the attribute value, Attribute name and its value always appear in pair. The attribute name is used without any quotation but attribute value is used in single ( ‘ ‘ ) or double quotation ( ” ” ).
Example 2:
<text category = "message" purpose ="Greet">Hello Geeks</text> |
In the above example, two attribute is used with different name. So, in a single element multiple attribute is used having unique attribute name.
But if we use two distinct element then we can use the attribute having the same attribute name. This can be understood with the help of below example:
Example 3:
<text category = "message" >Hello Geeks.</text> <text category = "message">How are you.</text> |
Attribute Types:
There are three types of attributes discribed below:
- String types Attribute: This type of attribute takes any string literal as a value.
- Enumerated Type Attribute: This attribute is further distributed in two types-
- Notation Type: This attribute is used to declares that an element will be referenced to a notation which is declared somewhere else in the XML document.
- Enumeration: This attribute is used to specify a particular list of values which match with attribute values.
- Tokenized Type Attribute: This attribute is further distributed in many types:
- ID: This attribute is used to identify the element.
- IDREF: This attribute is used to refer an ID which has already been named for another element.
- IDREFS: This attribute is used to refer all IDs of an element.
- ENTITY: This attribute is used to indicate the attribute which will represent an external entity used in the document.
- ENTITIES: This attribute is used to indicate the attribute which will represent external entities used in the document.
Rules for creating an attribute: There are some rules that should be followed while creating an attribute:
- An attribute should not repeat itself in a single start or empty-element tag.
- An attribute should be declared using the attribute-list declaration in the DTD (Document Type Definition).
- An attribute element is used without any quotation and the attribute value is used in a single (‘ ‘) or double quotation (” “).
- An attribute name and its value should always appear in pair.
- An attribute value should not contain direct or indirect entity references to external entities.
Recommended Posts:
- HTML | Attributes
- HTML | data-* Attributes
- HTML | DOM attributes Property
- jQuery | Set Content and Attributes
- jQuery | Get Content and Attributes
- HTML | Attributes Complete Reference
- PHP | SimpleXMLElement attributes() Function
- How to get the data attributes of an element using JavaScript ?
- Difference between 'hidden' and 'aria-hidden' attributes in HTML
- Tags vs Elements vs Attributes in HTML
- Copy all the attributes of one element and apply them to another with JavaScript
- Why are dashes preferred for CSS selectors / HTML attributes ?
- Data-toggle attributes in Twitter Bootstrap
- What is the difference between properties and attributes in HTML?
- What are these attributes `aria-labelledby` and `aria-hidden` ?
- How to filter object array based on attributes?
- SVG Element.attributes Property
- Web technologies | HTML and XML | Question 1
- Displaying XML Using CSS
- Displaying XML Using XSLT
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

