XML Syntax rules



XML Syntax Rules : Different XML Parsing Rules

  1. We have already discussed in our previous chapter about difference between XML and HTML.
  2. HTML tags are not case sensitive. XML is Case Sensitive.
  3. XML have some syntax rules that are somewhat simple and logical.

5 XML Rules are :

  1. XML Document must have Exactly One Root Element.
  2. XML Tags Must be Closed
  3. XML Tags are Case Sensitive.
  4. XML Tags Must be Properly Nested.
  5. XML Attribute Values Must be Quoted.

XML Syntax Rules are : –

Rule 1 : XML Document Must have One and Only One Root Element

  • XML Document Consists of Elements Arranged Like Tree.
  • XML Document Must have One and Only One Root Element.
  • XML Document Example –
<root>
  <child1>
    <subchild1>.....</subchild1>
    <subchild2>.....</subchild2>
  </child1>
  <child2>
    <subchild1>.....</subchild1>
    <subchild2>.....</subchild2>
  </child2>
</root>

All Children Nodes are nested under Parent Element.


Rule 2 : All XML Elements Must have Closing Tag

  • In HTML many elements don’t have closing tags.
  • XML Element must have Closing Tag.
  • It is illegal to have non-closing tag in XML.
<b>This is Bold text
<br>

above syntax will cause XML Parsing Error. Each opening tag must have closing tag.
Correct Way :

<b>This is Bold text</b>
<br />

Rule 3 : XML Tags are Case-Sensitive

  • All XML Tags are Case Sensitive.
  • Opening Tag must have Closing tag with Same Case and Spelling.
<Name>My Name is Khan</name>

above syntax will cause parsing error.

Correct Way :

<Name>My Name is Khan</Name>

Key Rules :

  1. Opening Tag Must have Same Spelling to that of Closing Tag.
  2. Opening Tag and Closing Tags must have same Case.

Rule 4 : XML Tags Must be Properly Nested

  • HTML does not care improperly nested element. It does not show any error.
  • XML does Strict Checking of Elements.
  • It will give parsing error if Parser finds any Improper nesting of Elements.

Proper Nesting

<p>I am <b>bold</b> and <i>Italic</i></p>

Nesting With Parsing Errors

<p>I am <b>bold</b> and <i>Italic</p></i>

Rule 5 : XML Attributes Must be Quoted

All XML Attributes must be quoted.

<letter date=12/12/12>
   <to>Pritesh</to>
   <from>Suraj</from>
</leter>

All XML Attributes must be quoted like HTML.

<letter date="12/12/12">
   <to>Pritesh</to>
   <from>Suraj</from>
</leter>