XML Documents structure
In this tutorial we are going to learn about XML Document. XML Document consists of many parts. XML Document have 2 main parts i.e Document Information Followed by Document Body.
Consider Following Document -
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE document system "Person.dtd"> <!-- Here is a comment --> <?xml-stylesheet type="text/css" href="Styles.css"?> <Person> <Male> <name>Pritesh</name> <age>22</age> </Male> <Female> <name>Pooja</name> <age>22</age> </Female> </Person>
Explanation : XML Document
Part 1 : Prolog (optional)
1.1 XML Declaration :
- XML Declaration is Optional.
- XML Declaration must be First Line in XML Document if we write Declaration.
- XML Declaration tells that Document Written is in XML.
- XML Declaration tells XML Version used to Write Document.
- XML Declaration tells Encoding Style Used to Encode XML Document.
- If XML Document is standalone i.e if it does not depends on other external document then we need to specify standalone=”yes”.
- W3C recommends to include XML Declaration.
<?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
1.2 Document Type Definition (DTD)
- Document Type definition is used to Define XML Document.
- DTD is used when you Validate your XML document.
- DTD can be Internal or External.
- DTD rule tells which Element is allowed to nest inside Other Element.
<!DOCTYPE document system "Person.dtd">
1.3 Comment
- Comments are Optional part of XML Document.
- Comments in XML are similar to HTML . <!- and ->
- Content Written inside Comment is ignored by Parser. (Comment part is not parsed by Parser)
- Comments can appear anywhere inside XML Document.
1.4 Styling and Processing Instruction
- Processing Instructions begin with <? and ends with ?>
- Processing Instructions are instructions for the XML processor.
- Processing instructions are processor dependant so not all processors understand all processing instructions.
<?xml-stylesheettype="text/css"href="Styles.css"?>
1.5 White Space
- White Space can be created using Carriage Return , Line Feed and Tab.
- White Space cannot affect Parsing of Document.
- User is Free to Use White Space anywhere inside document.
- XML recommendation specifies that XML documents use the UNIX convention for line endings.
- It means that you should use a linefeed character only (ASCII code 10) to indicate the end of a line.
Summary :
XML Declaration |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
Document Type Definition (DTD) |
<!doctype document system "Person.dtd"> |
Comment |
<!-- Here is a comment --> |
Processing Instructions |
<?xml-stylesheet type="text/css" href="Styles.css"?> |
White Space |