Topic :XML Basic syntax: Prolog Tags & Elements Attributes Text Reference

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 Syntax rules : infobrother

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.



XML TAG : infobrother
<p> This is the Content </p>




The XML Prolog Does not have a Closing tag.
This is not an Error. The Prolog is not a part of the XML Document.


  • Rules for Tags & Elements:

    XML Tags are Case Sensitive:
    XML Tags are Case Sensitive. The Tab <Info> is different from the tag <info>. Opening and Closing tags must be written with the same case.

    <Info> This is Correct </Info>
    <Info> This is Incorrect </info>


    XML Elements Must be Properly Nested.

    An XML Element can contain multiple XML-Elements as its children, but the children elements must not overlap, i.e. and end tag of an element must have the same Name as that of the most recent unmatched start tag.

    INCORRECT:

    <?xml version = "1.0"?>
    <Tutorials>
        <website>InfoBrother.com
    </Tutorials>
         </website>

    CORRECT

    <?xml version = "1.0"?>
    <Tutorials>
        <website>InfoBrother.com</website>
    </Tutorials>


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.


  • Rules for Attributes:

    XML Attribute Values must be Quoted:
    IN XML, the Attribute Values must Always be Qutoed.

    INCORRECT

    <Message date = 30/03/2017>
        <from> InfoBrother </from>
        <to> Learners </to>
    </Message>

    CORRECT

    <Message date = "30/03/2017">
        <from> InfoBrother </from>
        <to> Learners </to>
    </Message>


    Same Attribute Can't Have Two Values in a syntax:

    In XML, Same attributes cannot have two values in a syntax. the following example shows incorrect syntax because the attribute Name is specified twice.

    <Message Name = "InfoBrohter" date = "30/03/2017" Name = "Omar">
        <from> InfoBrother </from>
        <to> Learners </to>
    </Message>




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 CharacterReplacement EntityDescription
<&lt;Less than.
>&gt;Greater than.
&&amp;Ampersand.
'&apos;Apostrophe.
"&quot;Quotation Mark.



Only < and & strictly Illegal in XML, but it is good habit to replace > with &gt; as well.


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, &gt; where amp is name. The name refers to a predefined string of text and/or markup.

Character References − These contain references, such as &#65;, 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".



White-Space is preserved in XML. XML does not Truncate Multiple white-space just like HTML.
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement