XML | Syntax - GeeksforGeeks

22 Apr, 2019
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 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 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, êèé 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 must 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.<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.Syntax rules for XML elements: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.Syntax rule for XML Attributes: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.Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.My Personal Notes
arrow_drop_upSave
<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.
Syntax rules for XML elements:
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.
Syntax rule for XML Attributes:
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.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
HTML and XML
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to create footer to stay at the bottom of a Web page?
How to apply style to parent if it has child with CSS?
How to Open URL in New Tab using JavaScript ?
Difference Between PUT and PATCH Request
How to execute PHP code using command line ?