XML stands for Extensible Markup Language. XML is a markup language much like HTML. XML was designed to describe data. XML tags are not predefined in XML. You must define your own tags. XML is self describing. XML uses a DTD (Document Type Definition) to formally describe the data.
Xml Document is composed of markups and contents. Six kinds of markups can occur in xml Document.
1. Document Type Declaration
3. Entity References
5. Processing Instruction
6. Conditional Section
The elements should follow the following rules:
• Every start-tag must have a matching end-tag.
• Tags cannot overlap. Proper nesting is required.
• XML documents can only have one root element.
• Element names must obey the following XML naming conventions:
o Names must start with letters or the “_” character. Names cannot start with numbers of punctuation characters.
o After the first character, numbers and punctuation characters are allowed.
o Names cannot contain spaces.
o Names should not contain the “:” character as it is a “reserved” character.
o Names cannot start with the letters “xml” in any combination of case.
o The element name must come directly after the “<” without any spaces between them.
• XML is case sensitive.
• XML preserves white space within text.
• Elements may contain attributes. If an attribute is present, it must have a value, even if it is an empty string “”.
What is Element?
Elements are the logical components of XML documents. When all of our documents are abstracted into smaller parts, we can manipulate their content from whichever perspective we require. The smaller parts of our larger documents can be represented in XML using “elements.”
Elements consist of words that serve as the “names” for your element “tags” and are surrounded on either side by “less than” (<) and “greater than” (>) characters. These start and end tags may be used to encapsulate character data (text), as in the following example.
<Summary>Text goes in here</Summary>
What is Attributes?
Attributes provide a means of assigning “extra” information to elements in order to further describe properties of those elements. Attribute-value pairs can be associated with elements by including them inside of an element’s start tag.
XML attributes are normally used to describe XML elements, or to provide additional information about elements.
Xml Schema is an Xml based alternative to DTD. Xml Schema describes the structure of the Xml Document. Xml schema language is also referred to as Xml Schema Definition (XSD).
Xml Schema defines the following
• Elements and attributes that can appear in a Document.
• Child Elements
• The order and number of child elements
• Whether an element is empty or can include Text.
• Data types for elements and attributes
• Default and fixed values for elements and attributes.
Xml Schema Elements:
The First Element in an Xml Schema file is schema element.
Xmlns- xml Namespace
<xsd: schema xmlns: xsd =“ http://www.w3.org/2001/XMLSchema“ >
The main sub elements in xml schema
1. element: declares an element
2. attribute: declares an attribute
3. complexType: The elements that can contain other elements and attributes.
4. simpleType: the elements that can not contain other elements or attributes. These elements are the basic types, including string, data, integer etc.
The xsd: annotation and xsd: documentation tags are used for providing description of the document.
<xsd: schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd: documentation xml: lang =”en”>
Bookstore schema for book.xml
In our XML example the element bookstore is a complexType element because it contains book sub-elements.
<xsd: element name=”bookstore” type=”bookstoreinfo”/>
<xsd: complexType name=”bookstoreinfo”>
<xsd: element name=”book” type=”bookinfo”/>
Our book element is also a complexType element.
<xsd: complexType name=”bookinfo”>
<xsd: element name=”title” type=”titleinfo”>
<xsd: element name=”author” type=”xsd:string”/>
<xsd: element name=”year” type=” xsd:string”/>
<xsd: element name=”price” type=” xsd:string”/>
The sub-elements author, year and price are of type xsd:string. The sub-element title is of complexType because it has an attribute named id. To define an attribute use the following:
<xsd: attribute name=”id” type=”xsd: string” use=”required”/>
Use value required means this attribute is mandatory. By default attributes are optional.
The main difference between XML and HTML:
XML is not a replacement for HTML. XML and HTML were designed with different goals:
• XML was designed to describe data and to focus on what data is. HTML was designed to display data and to focus on how data looks.
• HTML is about displaying information, XML is about describing information.
Use of XML:
• XML can keep data separated from your HTML
• XML can be used to store data inside HTML documents
• XML can be used as a format to exchange information
• XML can be used to store data in files or in databases