Open In App

XML | Syntax

Last Updated : 22 Feb, 2023
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.



Similar Reads

Explain the benefits of spread syntax &amp; how it is different from rest syntax in ES6 ?
Spread Operator: Spread operator or Spread Syntax allow us to expand the arrays and objects into elements in the case of an array and key-value pairs in the case of an object. The spread syntax is represented by three dots (...) in JavaScript. Syntax: var my_var = [...array]; Benefits of using Spread syntax: 1. It allows us to include all elements
5 min read
Which tag is used to find the version of XML and syntax ?
Extensible Markup Language (XML) is a markup language, defining a ruleset for encoding documents in both formats that is human-readable and machine-readable. The design goals of XML focus on simplicity, generality, and usability across the Internet. XML is designed to be self-descriptive along with storing and transporting the data. It is a textual
2 min read
ES6 Top features and syntax
ES6 or as it is officially called: ECMAScript2015 is a new JavaScript implementation and is arguably the hottest topic in the JS developer's conventions and meetups why it should not be: JavaScript rules the web and is gaining a foothold in every other field possible, be it robotics(nodebots), desktop applications(using ion framework), chatbots, et
3 min read
jQuery Syntax
It is used for selecting elements in HTML and performing the action on those elements. Syntax: $(selector).action()$ sign: It grants access to jQuery. (selector): It is used to find HTML elements. jQuery action(): It is used to perform actions on the elements. Used to hide the current element.$(this).hide() Used to hide all &lt;p&gt; elements.$("p"
1 min read
Shorthand Syntax for Object Property Value in ES6
Objects in JavaScript are the most important data-type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScript primitive data-types (Number, String, Boolean, null, undefined, and symbol) in the sense that while these primitive data-types all store a single value each (depending on their types). The sho
1 min read
SASS Syntax
SASS supports two types of syntax. Each one can be differently used to load your required CSS or even the other syntax. 1. SCSS: The SCSS syntax uses .scss file extension. It is quite similar to CSS. You can even say that SCSS is a superset of CSS, meaning that all the valid CSS is also valid SCSS too. Due to its similarity with CSS, it is the easi
2 min read
How to do HTML syntax highlighting inside PHP strings ?
Syntax highlighting is the work of code editors such as Sublime Text, Visual Studio, Dev CPP, etc, which highlights all the different parts of the source code depending on their syntax by color, modified fonts, or through graphical changes. Since color highlighting these days is integrated into all common editors and development areas. Highlighting
3 min read
How to highlight syntax in files using Node.js ?
Node.js supports modules and packages that can be installed using NPM, which allows us to exploit various functionalities. One such functionality is syntax highlighting using Node.js that helps us to render highlighted code as static HTML, or we can also use it for dynamic syntax highlighting. The following approach covers how to highlight syntax f
2 min read
How to do syntax checking using PHP ?
Syntax checking is one of the most important tasks in programming. Our compiler checks our code and shows relevant errors if there is any in the code i.e. compile time, run time, syntax, etc. We can do the same thing i.e. syntax checking in PHP. In this article, we are going to learn how we can do syntax checking in PHP. The syntax is basically a s
2 min read
Explain the arrow function syntax in TypeScript
In this article we will try to understand how use the basic arrow function syntax in TypeScript. Arrow functions are basically the shorter way provided to all the users in order to implement the long/traditional function syntax in a much more shorter manner with ease. Arrow functions as implemented in JavaScript (ES6), will be implemented in a simi
3 min read