XML Elements

Estudies4you
XML Naming Rules

XML Elements

  • An XML document contains XML Elements.
  • Element can contain other elements, simple text or a mixture of both
  • Elements can also have attributes
<bookstore> <book category="CHILDREN">
<title>Harry Potter</title>
<author>Rowling</author>
<year>2006</year>
 <price>49.39</price> </book>
<book category="WEB">
<title>Learning XML</title>
<author>chris bates</author>
<year>2004</year>
<price>42.35</price>
</book> </bookstore>

In the example here:
  • <bookstore> and <book> have element contents, because they contain other elements
  • <author> has text content because it contains text
  • <book> has an attribute (category="CHILDREN")

XML Naming Rules 
XML elements must follow these naming rules:
  • Names can contain letters, numbers, and other characters
  • Names must not start with a number or punctuation character
  • Names must not start with the letters xml  (or XML, or Xml, etc)
  • Names cannot contain spaces Any name can be used, no words are reserved
  • Make names descriptive
  • Names with an underscore separator are nice
For example: <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
  • 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 problems if your software 
XML Elements are Extensible
XML elements can be extended to carry more information.
Look at the XML example:
<note>
<to>Raju</to>
<from>Ravi</from> <
body>Don't forget to meet me this weekend!</body>
</note>

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

MESSAGE To: Raju
From: Ravi
Don't forget me this weekend!
Consider that the author of the XML document added some extra information to it:
<note>                
<date>2013-01-02</date>
<to>Raju</to>
<from>Ravi</from>
<heading>Reminder</heading>
<body>Don't forget to meet me this weekend!</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 often be extended without breaking applications.

To Top