The XML syntax has very simple, logical, easy to learn, and easy to use rules.
XML Syntax Rules:
XML Documents Must Have a Root Element:
The root element is the parent of all other elements and it must be present in every XML document.
<root> <child> <subchild>.....</subchild> </child> </root>
<?xml version="1.0" encoding="UTF-8"?> <note> <to>Sapna</to> <from>Tom</from> <heading>Meeting</heading> <body>At 11 AM on Monday morning.</body> </note>
In the above example, the root element is <note>.
<?xml version="1.0" encoding="UTF-8"?>
All XML Elements Must Have a Closing Tag:
In XML, all elements must have a closing tag and it is not allowed to omit the closing tag. However, there is no closing tag in an XML prolog. Because the prolog is not a part of the XML document. Thus, it is not an error.
<p>Hello!</p> <br />
XML Tags are Case Sensitive:
The <Note> tag is different from the <note> tag.
Opening and closing tags or the start and end tags should be written with the same case:
<info>I am the information.</info>
XML Elements Must be Properly Nested:
Improperly nested elements are possible in HTML.
Only the elements that are properly nested within each other are allowed in XML which means that an element opened inside the first element must be closed inside the first element.
XML Attribute Values Must Always be Quoted:
Similar to HTML, the attributes of the XML elements can have name/value pairs, however, the attribute values must always be quoted in XML elements.
<?xml version="1.0" encoding="UTF-8"?> <note date="4/06/2020"> <to>Sapna</to> <from>Tom</from> <heading>Meeting</heading> <body>At 11 AM on Monday morning.</body> </note>
In XML, some characters have a special meaning, i.e., on placing the “<” inside an XML element, an error will be generated because the parser interprets it as the start of a new element.
<info>id < 10</info>
An error will be generated.
<info>id < 10</info>
Replacing the “<” character with an entity reference will avoid the error.
In XML, we have five predefined entity references, but only < and & are strictly illegal in XML. It is, however, considered a good habit to replace > with > entity reference. These predefined entity references are:
Comments in XML:
Just like HTML, the syntax for writing comments in XML is:
<!-- I am a comment -->
In the middle of a comment, we cannot place two dashes.
<!-- Invalid -- comment -->
White-space is Preserved in XML:
Contrary to HTML, multiple white-spaces are not truncated to one single white-space in XML.
XML: Hello World HTML: Hello World
XML Stores New Line as LF:
A new line is stored as a carriage return and line feed (CR+LF), by Windows applications.
- Unix and Mac OS X: Stores a new line as LF.
- Old Mac systems: Stores a new line as CR.
- XML: Stores a new line as LF.
Conforming to all the above syntax rules means that an XML document is a “Well Formed” XML document.