Open In App

XML | Syntax

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: XML | Basics In this article, we are going to discuss XML syntax rule which is used while writing an XML document or an XML application. It is a very simple and straight forward to learn and code. Below is a complete XML document to discuss each component in detail. 

XML




<?xml version="1.0" encoding="UTF-8"?>
<message>
   <to>Students</to>
   <from>Teacher</from>
   <subject>Regarding assignment submission</subject>
   <text>All students will have to submit assignment by tomorrow.</text>
</message>


Syntax rules for XML declaration or XML Prolog: 

XML




<?xml version="1.0" encoding="UTF-8"?>


Below is the explanation of each point.

  • This line is called XML Prolog or XML declaration.
  • This line is optional i.e, it can be either used or not in an XML document. However, it should be the very first line if used.
  • The version=”1.0″ is the version of the XML currently used. There are various versions of XML available.
  • The encoding=”UTF-8″ specifies the character encoding used while writing an XML document, for example, êèé is for French and so on. Its default value is “UTF-8”. For more about character encoding click here.
  • This declaration is case sensitive for example “xml” should be in lower case in .

Syntax rules for Root Element:

  • Every XML files should have one or more Root elements to avoid error. For example below code is wrong because it’s not containing Root element. 

XML




<to>Students</to>
<from>Teacher</from>
<subject>Regarding assignment submission</subject>
<text>All students will have to submit assignment by tomorrow.</text>


  • In the first example the Root element is <message> and all the remaining elements <to>, <from> etc is the child element and reside within the root element.
  • It is case sensitive.
  • The XML element should have a closing element for example <text category = “message”>Hi</text> is correct but <text category = “message”>Hi is not correct because it does not contain the closing element and it will throw an error and vice-versa.
  • The elements in XML should be nested properly otherwise it will throw an error. For example <to><from>Geeks</from></to> is nested correctly but <to><from>Geeks</to></from> is wrong because if <from> is opened inside the <to> element then this should also end inside of the </to> element.
  • It is also case sensitive i.e, the starting and closing element should be in the same case. For example <to>….</to> is correct but <to>…..</To> is not correct and it will throw an error.
  • The XML attribute is having two part one is Name and other is its value. It resides inside of the opening of an XML element. For example: <text category = “message”>All students will have to submit the assignment by tomorrow.</text> Here category is the attribute name and message is its value and the attribute value should either be in a single quotation or in double quotation otherwise it will throw an error. The Attribute Name is written without any quotation.
  • The XML attribute is also case sensitive.
  • An XML element can have multiple attributes but can not have the same attribute names in the same element. For example: <text category =”message” purpose = “greet”>GeeksforGeeks</text> Above attributes is correct because of having multiple attributes with the different attribute name. <text category =”message” category = “greet”>GeeksforGeeks</text> Above attribute is wrong because of having the same attribute name in a single element.

XML Comments: Correct syntax for writing XML comments are: <!– It is comment section –>
Incorrect comments: <!– It is comment — section –> i.e. Two dashes in between the comment is not allowed.



Last Updated : 22 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads