XML Element


XML documents contain XML elements.


What is an XML Element?

An XML element refers to the part from (and including) the start tag to (and including) the end tag.

An element can contain:

  • other elements
  • text
  • attributes
  • or a mix 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> both haveelement content, because they contain other elements. The <book> element also hasattributes(category="CHILDREN"). The <title>, <author>, <year>, and <price> havetext 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 mark
  • Names cannot start with the letters xml (or XML, Xml, etc.)
  • Names cannot contain spaces

Any name can be used, there are no reserved words.


Best Naming Practices

Make names descriptive. Names with underscores are also good: <first_name>, <last_name>.

Names should be short and simple, for example: <book_title>, rather than: <the_title_of_the_book>.

Avoid the "-" character. If you name things in this way: "first-name", some software might think you want to subtract name from first.

Avoid the "." character. If you name things in this way: "first.name", some software might think "name" is a property of the object "first".

Avoid the ":" character. Colons are converted to be used as namespaces (introduced later).

XML documents often have a corresponding database, where the fields correspond to elements in the XML document. A practical rule of thumb is to use the database's naming rules to name elements in the XML document.

In XML, non-English letters such as éòá are perfectly legal, but you need to be aware of problems that may arise if your software vendor does not support these characters.


XML Elements are Extensible

XML elements are extensible to carry more information.

Look at the following XML example:

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

Let's imagine that we created an application that extracts the <to>, <from>, and <body> elements from the XML document and produces the following output:

MESSAGE

To: Tove
From: Jani

Don't forget me this weekend!

Imagine that the author of the XML document adds some extra information:

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

Will this application break or crash?

No. This application will still find the <to>, <from>, and <body> elements in the XML document and produce the same output.

One of the advantages of XML is that it can be extended without breaking the application.

Other Extensions