XML Elements

« Previous Chapter Next Chapter »

An XML document contains XML Elements.

What is an XML Element?

An XML element is everything from (including) the element's start tag to (including) the element's end tag.

An element can contain:

  • other elements
  • text
  • attributes
  • or a mix of all of the above...
<friendsdata>
  <friendscategory="men">
    <title>Second Men</title>
    <author>Adhim</author>
    <Birth>1992</Birth>
    <Age>23</Age>
  </book>
  <friendscategory="WEB">
    <title>Learning XML</title>
    <author>Melina</author>
    <Birth>2003</Birth>
    <Age>13</Age>
  </book>
</friendsdata>

In the example above, <friendsdata> and <book> have element contents, because they contain other elements. <book> also has an attribute (category="men"). <title>, <author>, <Birth>, and <Age> have text content because they contain text.

XML Naming Rules

XML elements must follow these naming rules:

  • Names can contain letters, numbers, and other characters
  • Names cannot start with a number or punctuation character
  • Names cannot start with the letters xml (or XML, or Xml, etc)
  • Names cannot contain spaces

Any name can be used, no words are reserved.

Best Naming Practices

Make names descriptive. Names with an underscore separator are nice: <first_name>, <last_name>.

Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.

Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first.

Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first."

Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later).

XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents.

Non-English letters like ��� are perfectly legal in XML, but watch out for error if your software vendor doesn't support them.

XML Elements are Extensible

XML elements can be extended to carry more information.

Look at the following XML example:

<note>
<to>Guru</to>
<from>saina</from>
<body>Get Me my watch</body>
</note>

Let's imagine that we created an application that extracted the <to>, <from>, and <body> elements from the XML document to produce this output:

MESSAGE

To: Guru
From: saina

Get Me my watch

Imagine that the author of the XML document added some extra information to it:

<note>
<date>2015-01-10</date>
<to>Guru</to>
<from>saina</from>
<heading>Reminder</heading>
<body>Get Me my watch</body>
</note>

Should the application break or crash?

No. The application should still be able to find the <to>, <from>, and <body> elements in the XML document and produce the same output.

One of the beauties of XML, is that it can be extended without breaking applications.


« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!