w3resource logo

XML elements

XML Elements

Secondary Nav


Elements are the basic building blocks of an XML document.

Syntax of an XML 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>
<p><b>This is w3resource.com.</b>The largest tutorial on web 
based development.</p>
<p>We are learning <i>xml</i></p>

example xml file:

<subtopic1>Learning xml</subtopic1>
<subtopic2>Learning DTD</subtopic2>
<subtopic3>Learning XSLT</subtopic3>
<subtopic4>Learning xpath</subtopic4>
  • Children of an element reside with the open and close tags of their parents. 
<?xml version="1.0"?>
There are five continents 

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"?>

But writing it like the following is also allowed:

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

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"?>
This is the largest online tutorial on web development </w3resource>

But this is the correct use

<?xml version="1.0"?>
This is the largest online tutorial on web development 
  • 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).

Join our Question Answer community to learn and share your programming knowledge.