w3resource logo
XML Tutorial

XML Elements

Introduction

Elements are the basic building blocks of an XML document.

Syntax of an XML Element

<name_of_the_element>text</name_of_the_element>

Following are the basic characteristics of XML Elements

  • Most of the data contained in an XML document, is enclosed within XML Elements.
  • An Element in an XML document starts with an "<" tag and ends with an ">" tag.
  • In XML documents name of the Elements are user defined.Having said that, users or authors can use Element names of their own choice.Here is an example:
  • <tutorial> We are learning fundamentals of XML </tutorial>
<html>
<body>
<head>
<title>w3resource-xml</title>
</head>
<body>
<p><b>This is w3resource.com.</b>The largest tutorial on web 
based development.</p>
<p>We are learning <i>xml</i></p>
</body>
</html>

example xml file:

<xml>
<tutorial>w3resource
<one>html</one>
<two>xml
<subtopic1>Learning xml</subtopic1>
<subtopic2>Learning DTD</subtopic2>
<subtopic3>Learning XSLT</subtopic3>
<subtopic4>Learning xpath</subtopic4>
</two>
<three>css</three>
<four>javascript</four>
<five>ajax</five>
<six>php</six>
<seven>mysql</seven>
<eight>svg</eight>
</tutorial>
</xml>
  • Children of an element reside with the open and close tags of their parents. 
<?xml version="1.0"?>
<world>
<continents>
There are five continents 
</continents>
</world>

The element world has a child element continent in this example. Note that the element continent is started and finished within the opening tag<world> and closing tag </world> of the element with tagname world, which is the parent of the continents element.

  • The text between open and close tags of an XML element is called as content. In the previous example only, "There are five continents " is the content, since it is placed between open and close tags of the continents element.
  • If an element does not have any content, it is said an empty element. An empty element is represented like <tagname/> or <tagname></tagname>.

In the following example, loss element does not have any content. So it is being written as <loss></loss>

<?xml version="1.0"?>
<series>
<loss>
</loss>
</series>

But writing it like the following is also allowed:


<?xml version="1.0"?>
<series>
<loss />
</series>

This is referred as empty element shorthand.

  • Unlike HTML, XML does not have any predefined tagname. So designers of an XML document can decide the tagnames of an XML document. For all of the examples related to an element you surely have noticed that we have used the name of the element as per our requirement.
  • Element names of an XML document are case sensitive.
<?xml version="1.0"?>
<W3RESOURCE>
This is the largest online tutorial on web development </w3resource>

But this is the correct use

<?xml version="1.0"?>
<w3resource>
This is the largest online tutorial on web development 
</w3resource>
  • Element names must begin with a letter or an underscore(_).
  • The initial character of the name of an element can be followed by any number of letters, digits, periods(.), hyphens(-), underscores or colons (:). However, because colons are used in the syntax of the namespaces in XML, they should not be used in naming an element, unless as described by that  specification.

Here is a table displaying correct an incorrect examples of names of xml elements :

<tutorial></tutorial> Correct
<topic2></topic2> Correct
<3topic3></3topic3> Incorrect since elements are begin with invalid characters
<three,topic3></three,topic3> Incorrect since elements contain invalid characters
<threetopic3></threetopic3> Correct
<three;topic3></three;topic3> Incorrect since elements contain invalid characters
<three_topic3></three_topic3> Correct
<three_topic:topic3></three_topic:topic3> Incorrect, reference to undeclared namespace prefix
<!topic></ !topic> Will generate error since the declaration has an invalid name
<[topic]></[topic]> Will generate an error that complains that an element began with an invalid character.
  • Element name that starts with a character sequence xml, are reserved for future use.
  • Contents of the elements can be textdata, no-data (referred as empty an element) and other elements(called as child element).