XML Syntax Rules

Estudies4you
XML Syntax Rules
XML Syntax Rules
The XML syntax rules are very simple and logical.
  • XML Tags
  • XML Closing Tags
  • Nested XML Elements  
  • XML Documents
  • XML Attribute Values
XML Tags
XML elements are defined using XML tags.
  • The tags are case sensitive
For example, the tag <Letter> is different from the tag <letter>
  • Opening and closing tags must be written with the same case
For example, <Message> This is incorrect </message>
<message> This is correct </message>
Note: "Opening and closing tags" are often referred to as "Start and end tags". Use whatever you prefer. It is exactly the same thing.

XML Closing Tags
All XML elements must have a closing tag.
  • In HTML, you will often see elements that don't have a closing tag For example,
<p>This is a paragraph <p>This is another paragraph
  • In XML, it is illegal to omit the closing tag. All elements must have a closing tag
For example, <p>This is a paragraph</p>
<p>This is another paragraph</p>
Note: You might have noticed from the previous example that the XML declaration did not have a closing tag. This is not an error. The declaration is not a part of the XML document itself, and it has no closing tag.

Nested XML Elements
  • In HTML, you will often see improperly nested elements:
<b><I>This text is bold and italic</b></i>
  • In XML, all elements must be properly nested within each other:
<b><i>This text is bold and italic</i></b>
In the example above, "Properly nested" simply means that since the <i> element is opened inside the <b> element, it must be closed inside the <b> element.

XML Documents
XML documents must contain one element that is the parent of all other elements.
This element is called the root element.

<root>
<child>
<subchild> ….. </subchild>
</child>
</root>

XML Attribute Values
  • XML elements can have attributes in name/value pairs just like in HTML.
  • In XML the attribute value must always be quoted. Study the two XML document examples below. The first one is incorrect, the second is correct:
Example 1:
<note date=06/05/2020>
<to>Raju</to>
<from>Ravi</from>
</note>

Example 2:
<note date="06/05/2020">
<to>Raju</to>
<from>Ravi</from>
</note>
The error in the first document is that the date attribute in the note element is not quoted.


To Top