XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these.
Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag.
Following is the syntax to write an XML element −
<element-name attribute1 attribute2>
- element-name is the name of the element. The name its case in the start and end tags must match.
- attribute1, attribute2 are attributes of the element separated by white spaces. An attribute defines a property of the element. It associates a name with a value, which is a string of characters. An attribute is written as −
name is followed by an = sign and a string value inside double(" ") or single(' ') quotes.
An empty element (element with no content) has following syntax −
<name attribute1 attribute2.../>
Following is an example of an XML document using various XML element −
<?xml version = "1.0"?><contact-info><address category = "residence"><name>Tanmay Patil</name><company>TutorialsPoint</company><phone>(011) 123-4567</phone></address></contact-info>
XML Elements Rules
Following rules are required to be followed for XML elements −
- An element name can contain any alphanumeric characters. The only punctuation mark allowed in names are the hyphen (-), under-score (_) and period (.).
- Names are case sensitive. For example, Address, address, and ADDRESS are different names.
- Start and end tags of an element must be identical.
- An element, which is a container, can contain text or elements as seen in the above example.
This chapter describes the XML attributes. Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.
An XML attribute has the following syntax −
<element-name attribute1 attribute2 >
where attribute1 and attribute2 has the following form −
value has to be in double (" ") or single (' ') quotes. Here, attribute1 and attribute2 are unique attribute labels.
Attributes are used to add a unique label to an element, place the label in a category, add a Boolean flag, or otherwise associate it with some string of data. Following example demonstrates the use of attributes −
<?xml version = "1.0" encoding = "UTF-8"?><!DOCTYPE garden [
<!ELEMENT garden (plants)*><!ELEMENT plants (#PCDATA)><!ATTLIST plants category CDATA #REQUIRED>
<garden><plants category = "flowers" /><plants category = "shrubs"></plants></garden>
Attributes are used to distinguish among elements of the same name, when you do not want to create a new element for every situation. Hence, the use of an attribute can add a little more detail in differentiating two or more similar elements.
In the above example, we have categorized the plants by including attribute category and assigning different values to each of the elements. Hence, we have two categories of plants, one flowers and other shrubs. Thus, we have two plant elements with different attributes.
You can also observe that we have declared this attribute at the beginning of XML.
Following table lists the type of attributes −
It takes any literal string as a value. CDATA is a StringType. CDATA is character data. This means, any string of non-markup characters is a legal part of the attribute.
This is a more constrained type. The validity constraints noted in the grammar are applied after the attribute value is normalized. The TokenizedType attributes are given as −
ID − It is used to specify the element as unique.
IDREF − It is used to reference an ID that has been named for another element.
IDREFS − It is used to reference all IDs of an element.
ENTITY − It indicates that the attribute will represent an external entity in the document.
ENTITIES − It indicates that the attribute will represent external entities in the document.
NMTOKEN − It is similar to CDATA with restrictions on what data can be part of the attribute.
NMTOKENS − It is similar to CDATA with restrictions on what data can be part of the attribute.
This has a list of predefined values in its declaration. out of which, it must assign one value. There are two types of enumerated attribute −
NotationType − It declares that an element will be referenced to a NOTATION declared somewhere else in the XML document.
Enumeration − Enumeration allows you to define a specific list of values that the attribute value must match.
Element Attribute Rules
Following are the rules that need to be followed for attributes −
- An attribute name must not appear more than once in the same start-tag or empty-element tag.
- An attribute must be declared in the Document Type Definition (DTD) using an Attribute-List Declaration.
- Attribute values must not contain direct or indirect entity references to external entities.
- The replacement text of any entity referred to directly or indirectly in an attribute value must not contain a less than sign (<)