Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
AJWT » XML » XML Syntax
Suryateja Pericherla Categories: XML. No Comments on XML Syntax
0
(0)

This article explains about XML syntax. You will learn about elements, tags and attributes which are the fundamental parts in a XML document.

 

The syntax of XML can be thought of at two distinct levels. First, there is the general low-level syntax that specifies rules to be followed by all XML documents. Second, there is the high-level syntax that is distinct for each XML document which can be specified by using either DTD (Document Type Definition) or XML Schema.

 

They specify rules about which tags are allowed, what attributes are allowed with certain tags and in which order the tags are allowed to be written or laid out in the document.

 

Like (X)HTML, an XML element is the combination of opening tag, the content and the closing tag. An XML document can contain several kinds of statements. Most common of these are the data elements of the document. XML documents may also include markup declarations and processing instructions.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


All XML documents begin with an XML declaration, which is like a processing instruction but is not one. The XML declaration identifies the document as being XML and provides the version number of XML standard and the encoding scheme being used if any as shown below:

<?xml version = “1.0” encoding = “utf-8” ?>
Markup

 

Comments in XML are same as in (X)HTML as shown below:

<!– This is a comment in XML >
Markup

 

XML names are used for creating tags and attributes. An XML name must start with a letter or an underscore and can include digits, hyphens and periods (dots). XML names are case-sensitive i.e., student and Student are different in XML.

 

Every XML document must contain a root element, whose opening tag must be the first line of XML code. All other XML elements are nested inside the root element. XML tags are same as in (X)HTML, tag-name surrounded by angular brackets as shown below:

<tag-name>
Markup

 

Every XML element that can have content should have a closing tag as shown below:

</tag-name>
Markup

 

Elements that do not include any content can be closed as shown below:

<tag-name />
Markup

 

XML tags can have attributes, which can be specified as name-value assignments. All the attribute values must be enclosed in double quotes as shown below:

<tag-name  attr1=”value”  attr2=”value”>content</tag-name>
Markup

 

An XML document that strictly follows all the above mentioned rules are said to be well formed.

 

When designing a XML document, the designer is often faced with the choice between adding a new attribute to an element or defining a nested element.

 

The situation in which an attribute must be always preferred over a nested element is when identifying numbers or names of elements, exactly as the id and name attributes are used in (X)HTML.

 

Finally, attributes should be used if there is no substructure or if it is really just information about that element and nested elements should be used if the data can be structured. Below is an example of a simple well formed XML document:

<?xml version = "1.0" encoding = "utf-8" ?>
<students>
   <student id="101">
      <name>Ken</name>
      <branch>CSE</branch>
      <age>21</age>
   </student>
   <student id="201">
      <name>John</name>
      <branch>ECE</branch>
      <age>22</age>
   </student>
   <student id="301">
      <name>James</name>
      <branch>IT</branch>
      <age>21</age>
   </student>
</students>
Markup

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram