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>
<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:
- 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 to as an 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 :
|<3topic3></3topic3>||Incorrect since elements are begin with invalid characters|
|<three,topic3></three,topic3>||Incorrect since elements contain invalid characters|
|<three;topic3></three;topic3>||Incorrect since elements contain invalid characters|
|<three_topic:topic3></three_topic:topic3>||Incorrect, reference to undeclared namespace prefix|
|<!topic></ !topic>||Will generate an 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).
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework