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:
XML
<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:
XML
<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:
XML
<text category = "message" >Hello Geeks.</text>
<text category = "message">How are you.</text>
Attribute Types:
There are three types of attributes described 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.
Similar Reads
XML declarations An XML declaration is a statement placed at the beginning of an XML document that provides essential information about the document's encoding, version, and standalone status. It serves as a metadata header and helps parsers and processors understand how to interpret the XML content correctly. Synta
3 min read
XML Namespaces XML namespaces prevent naming conflicts between elements and attributes in XML documents, especially when various XML vocabularies are joined or elements with the same name come from different sources. Table of Content Default Namespace DeclarationPrefixed Namespace DeclarationDefault Namespace Decl
1 min read
What is XML ? Extensible Markup Language (XML) is a type of markup language that establishes a set of guidelines for encoding texts in a way that is both machine- and human-readable. For storing and transferring data on the web and in many other applications, XML is widely used. XML steps in as a versatile tool f
5 min read
DTD Entities When you are writing an XML document, pieces of information need to be used several times. At that time, you can use XML entities to store that piece of information to reuse it again and again by referring to it, avoiding writing that information multiple times. For example, if you have a default in
3 min read
What is DTD in XML ? DTD is a document-type definition. DTD contains a set of rules that control the structure and elements of XML files. When any XML file refers DTD file, it validates against those rules. DTD has validated elements and attributes that are defined inside the DTD document. It serves as a formal specific
3 min read
XML | Syntax Prerequisite: XML | Basics In this article, we are going to discuss XML syntax rule which is used while writing an XML document or an XML application. It is a very simple and straight forward to learn and code. Below is a complete XML document to discuss each component in detail. XML <?xml versi
3 min read