Latest web development tutorials

XML elements

XML document contains XML elements.


What is an XML element?

XML element is the time from (and including) the start tag until (and including) the end of the label.

An element can comprise:

  • Other elements
  • text
  • Attributes
  • Or a mixture of all of the above ...
<bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

In the example above, <bookstore> and <book> haveelement content, because they contain other elements.<book> element can also haveattributes (category = "CHILDREN").<title>, <author>, <year> and <price> hastext content because they contain text.


XML naming rules

XML elements must follow these naming rules:

  • The name can contain letters, numbers and other characters
  • The name can not start with a number or punctuation
  • The name can not start with the letters xml (or XML, Xml etc.)
  • The name can not contain spaces

You can use any name, no reserved words.


Best naming conventions

To make the name descriptive. Use underscores name is also very good: <first_name>, <last_name>.

Name should be short and simple, such as: <book_title>, instead: <the_title_of_the_book>.

Avoid "-" character. If you are named in such a way: "first-name", some software assumes that you want to subtract name from first inside.

Avoid. "" Characters. If you are named in such a way: "first.name", some software may think that "name" is the object "first" of the property.

Avoid ":" character. Colon will be converted to use namespaces (described later).

XML documents often have a corresponding database, in which fields corresponding to XML elements in the document. There is a practical experience, using naming database named XML elements in the document.

In XML, eoa and other non-English letters are perfectly legal, but it needs attention, your software vendor does not support these characters may occur when issues.


XML is extensible element

XML elements can be extended to carry more information.

Consider the following XML instance:

<note>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>

Let us imagine that we created an application that will <to>, <from> and <body> elements extracted from the XML document out and produces the following output:

MESSAGE

To: Tove
From: Jani

Do not forget me this weekend!

Imagine some additional information author XML document added:

<note>
<date>2008-01-10</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Then this application will break or crash?

will not. This application can still be found in the XML document <to>, <from> and <body> element, and produce the same output.

One of the advantages of XML, that can be extended without application outage.