XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

XML Syntax

XML Syntax

shape Description

XML Syntax chapter gives the clear instructions on how to use tags and elements by following the rules to write an XML document. XML Syntax is bit more strict when compared with the syntax of HTML. Basic XML syntax looks like below. Syntax : [xml] <?xml version="1.0"?> <contact-info> <name>Mike</name> <company>SPLessons</company> <phone>+(011) 011223366</phone> </contact-info> [/xml]

XML Declaration

shape Description

XML Declaration is also called as Prolog and is optional. It indicates the version number presently in use. If the Prolog is required in the code, it must be at the top of the document. XML Declaration is case-sensitive. [xml] <?xml version="1.0"?> (or) <?xml version="1.0" encoding="UTF-8"?>[/xml]

XML Tags and Elements

shape Description

Elements : Elements are logical units of information in an XML document and are enclosed in angular brackets < > Tags : Tags are used at the start and end of elements.

Root Tag

XML Documents must have a single root tag which have to be the parent of all other tags. [xml] <root> <child> <subchild>.....</subchild> </child> </root> [/xml] In the below example, the root element is <note> [xml] <?xml version="1.0" encoding="UTF-8"?> <note> <to>Michael</to> <from>Randy</from> <heading>Invitation</heading> <body>Party Tonight!!</body> </note> [/xml]

Closing Tag

All XML elements should have a closing tag( /> ) otherwise, the error is shown. [xml] <!------illegal declaration and not considered-----> <p>This is a paragraph <!-------Legal declaration and considered-----> <p>This is a paragraph</p>[/xml]

Nested Elements

XML elements can have multiple elements of as its children which should not be overlapped. [xml] <?xml version="1.0"?> <contact-info> <company>SPLessons</company> <contact-info> [/xml]

Case-Sensitivity

XML Elements names are case-sensitive i.e. when element starts with upper case letter, it should end with upper-case only. Likewise for lower case also. [xml] <Message>This is incorrect</message> <message>This is correct</message> [/xml]

XML Attributes

shape Description

An attribute provides single property of an element. It uses a name and value combination that are separated by equal "=" sign. [xml] <a href="http://www.splessons.com/">SPLessons</a> [/xml] In the above example, href is the name of attribute and http://www.splessons.com/ is the value of attribute.

Rules

  • Attribute must be placed within the start tag only after the element name, after white space -- no comma, semicolon etc.
  • Attribute names should not be within quotation marks and are case sensitive.
  • Attribute values should be within single or double quotation marks.

XML References

shape Description

A reference is used to add additional text or markup in XML document where an error may occur if the character is typed directly. References usually start with "&" and end with the symbol ";". XML has two types of references:

Entity References

Entity reference will have name inside the start and the end delimiters. For example & amp; where amp is name. The name refers to a predefined string of text and/or markup.

Character References

Character References has reference like A, having 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".

summary

shape Key Points

  • Prolog indicates the version number.
  • XML Documents must have a single root tag
  • All XML elements should have a closing tag( /> )
  • XML Elements names are case-sensitive.
  • Attributes uses a name and value combination that are separated by equal “=” sign.