XML Syntax:
The Syntax rules of XML are very simple and logical. Xml documents must contain one Root Element that is the Parent of all Other Elements:
< root >
< child >
< sub-Child > ..... < /sub-Child >
< /child >
< /root >
Example:
<message>
<to> Dear Learners </to>
<from> InfoBrother </from>
<title> Greeting </title>
<body> Welcome to InfoBrother! Enjoy Your Learning. </body>
</message>
In above Example, Message is the Root element.
In Xml Syntax, we have five things to know about. the following diagram depicts the syntax rules to write different types of Mark-up and text in an XML Document.
XML Prolog:
<?xml version="1.0" encoding="UTF-8"?>
The Above line of code in known as Xml Prolog. Where Version is the XML Version and encoding specifies the Character, encoding used in the document. UFT-8 Is the Default Character Encoding for XML Documents. to Avoid any type of errors, we should specify the Encoding used, or save our XML files as UFT-8. XML Prolog is Optional. if it exists, it must come first in the document.
“
UFT-8 is also the Default encoding for HTML5, CSS, JavaScript, PHP and SQL.
Tag & Elements:
An XML File is Structured by Several XML-Elements, also called XML Nodes or XML Tags. The Name of XML-Elements are Enclosed in Triangular Brackets <> and must have a closing tag as shown below.
<p> This is the Content </p>
This is not an Error. The Prolog is not a part of the XML Document.
Attributes:
An Attribute Specifies a Single Property for the Element, using a Name/Value pair. An XML Element can have one or more Attributes.
<a href="http://www.infobrother.com/"> InfoBrother </a>
In Above Example, href is the attribute name and http://www.infobrother.com/ is attribute value.
XML Text:
The Names of XML Elements and XML Attributes are case sensitive, which means the name of start and end elements need to be written in the same case. to avoid character encoding problems, all XML files should be saved as Unicode UTF-8 or UTF-6 files.
Whitespace characters like blanks, tabs and line-breaks between XML-elements and between the XML-attributes will be ignored.
Some characters are reserved by the XML syntax itself. Hence, they cannot be used directly. To use them, some replacement-entities are used, which are listed below
Reserved Character | Replacement Entity | Description |
---|---|---|
< | < | Less than. |
> | > | Greater than. |
& | & | Ampersand. |
' | ' | Apostrophe. |
" | " | Quotation Mark. |
XML References:
References usually allow you to add or include additional text or mark-up in an XML document. References always begin with the symbol "&" which is a reserved character and end with the symbol ";". XML has two types of references.
Entity References − An entity reference contains a name between the start and the end delimiters. For example, > where amp is name. The name refers to a predefined string of text and/or markup.
Character References − These contain references, such as A, contains a hash mark (“#”) followed by a number. The number always refers to the Unicode code of a character. In this case, 65 refers to alphabet "A".