XML Notes
XML Notes
Xml syntax rules: - The syntax rules of xml are very simple & strict. So creating
that can read & manipulate xml is very easy.
à All xml elements must have a closing tag with xml.
Ex: - <p> this is a paragraph </p> //valid
<p> --------- // invalid in xml (it illegal to omit the closing tag)
à Xml tags are case sensitive:
Opening & closing tags must be written with the same case
Ex: - <Letter> tag is different from the tag <letter>
à All elements must be properly nested. Improper nesting of tags makes no sense to
xml. In xml all elements must be properly nested within each other like this
Ex: - <b> <i> </i> </b> // valid
<b> <i> </b> </i> // invalid
à All xml documents must have root element (parent element). All xml documents
must contain a single tag pair to define a root element. All other element must be
within this root element.
Ex: - <root>
<child>
<sub child> ------- </sub child>
</child>
</root>
à Within xml, white space is preserved (continued). So, the white space in xml
documented is not truncated.
Element naming: - Xml elements must follow these naming rules (while defining
own tags)
1) Names can contain letters, numbers & other characters.
2) Names must not start with a number or punctuation character.
3) Names must not start with the letters (xml or Xml or XML…).
4) Names can not contain spaces
<message> -------- </message> // valid
<\mess> ------- </mess> // invalid
<first mess> ------- </first mess> // invalid
3
à Attributes values must always be quoted with xml, it is illegal to omit quotation
marks around attribute values.
Xml elements can have attribute in name / value pairs just like html.
<day date=”12/11/2008”>
This is correct: “12/11/2008”
This is in correct: 12/11/2008
à <? xml version=”1.0” encoding=”ISO-8859-1”?>
æ æ
Compulsory not compulsory
This is the 1st element of every xml program.
Program: -
<?xml version="1.0"?>
<student age="25">
<sname> Sudheer Reddy </sname>
<sid> 630130 </sid>
<course> Java package </course>
<address>
<stname> Ram Nagar </stname>
<city> ATP </city>
<state> AP </state>
</address>
</student>
Program: -
<?xml version="1.0"?>
<book>
<title> HTML black book </title>
<author> Sudheer </author>
<price> 450.94 </price>
<prod id="3454" media="paper"> </prod>
<chapter>
Introduction to html
<topic> html basic Structure </topic>
<topic> html tags </topic>
<topic> html table </topic>
</chapter>
<chapter>
Introduction to xml
<topic> Xml Syntax rules </topic>
<topic> Xml validation </topic>
4
</chapter>
</book>
Xml validations: - Well formed xml document is a xml document that confirms to
syntax rules of xml. Valid xml must be well form document & also confirms to
syntax rules of either xml DTD or xml Schema facility.
Xml DTD: - xml DTD is the old facility that validates xml documents. DTD defines
document structure with list of (legal) elements & their data types to store. The
purpose of DTD is defining components of xml documents. It represents a group of
elements. DTD can be declared inline in xml documents or as external references.
Internal DTD: - It DTD structure is included in xml documents; It should be
wrapped or embedded in DOCTYPE declaration as shown below
<! DOCTYPE rootele [declare] >
External DTD: - It DTD is external to xml document; it should be wrapped (placed)
within DOCTYPE declaration as shown below
<! DOCTYPE rootele SYSTEM”.dtd”>
Declaring root element: - Element with one or more child elements is declared with
element declaration as shown below
<! ELEMENT rootele (child)> (or)
<! ELEMENT rootele (child1,child2,child3,-------)>
Ex: - <! ELEMENT letter (to,from,message)>
Declaring child element: - Element with simple content is declared by element
declaration as shown below
<! ELEMENT childele (data type)>
<! ELEMENT to(#PCDATA)>
<! ELEMENT from(#PCDATA)>
<! ELEMENT message(#PCDATA)>
<letter>
<message> hai </message>
<message> how r u </message>
</letter> // invalid
Declaring elements with mixed elements: - We can declare element with mixed
content (both text & child elements) by ELEMENT declaration as shown below
<! ELEMENT elename (#PCDATA | eleneme1 | elename2|------)*>
Ex: - <! ELEMENT chapter (#PCDATA | topic)*>
Xml doc: - <chapter>
<topic> first topic </topic>
<topic> second topic </topic>
</chapter>
Declare element with empty content: - We can declare element with empty content
by using keyword EMPTY as shown below
7
Declare attributes in DTS: - In DTD one or more attributes are declare with
ATTLIST declaration as shown below
<! ATTLIST elename attrname attrtype attrvalue>
Attrvalue description
1) Default default value
2) #REQUIRED attrvalue must be included
3) #IMPLID attrvalue doesn’t have to be included
4) #FIXED attrvalue is fixed or constant
1) default: -
Ex: - <! ELEMENT company EMPTY>
<! ELEMENT company cname CDATA “Micro soft”>
Xml doc: - <company cname=”Micro soft”> </company> // valid
<company cname=”Sudheer”> </company> // valid
2) #REQUIRED: -
Ex: - <! ELEMENT company EMPTY>
<! ELEMENT company cname CDATA “#REQUIRED”>
Xml doc: - <company> </company> // invalid
<company cname=”Sudheer”> </company> // valid
3) #IMPLID: -
Ex: - <! ELEMENT company EMPTY>
<! ELEMENT company cname CDATA “#IMPLID”>
Xml doc: - <company> </company> // valid
<company cname=”Sudheer”> </company> // valid
4) #FIXED: -
Ex: - <! ELEMENT company EMPTY>
<! ELEMENT company cname CDATA “#FIXED”>
Xml doc: - <company cname=”Micro soft”> </company> // valid
<company cname=”Sudheer”> </company> // valid
Declaring entities: - Entities are names, use to define short cut for same common
text. In DTD entities are declare with ENTITY declaration as shown below
<! ELEMENT author (#PCDATA)>
<! ENTITY entityname “common text”>
Create a DTD file: - Open xml spy software
Xml spy file new dtd Click ok
Program: -
<?xml version="1.0" encoding="UTF-8"?> // is the default statement of
document type definition.
<!ELEMENT bookinfo (book+)>
<!ELEMENT book (title,author+, price,prod,chapter+)>
8
Save: - booksinfo.dtd
Program: -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookinfo SYSTEM "D:\psr\Xml programs\ bookinfo.dtd" > // is the
default statement of Extensible Markup Language
<bookinfo>
<book>
<title> Computer Networks </title>
<author> SUDHEER </author>
<price> &cost ;</price>
<prod id="123" media=""></prod>
<chapter> introduction to network
<topic> what is net work </topic>
<topic> different types of network </topic>
</chapter>
</book>
<book>
<title> layers </title>
<author> Sunil </author>
<price> &cost; </price>
<prod id="321" media=" "></prod>
<chapter> introduction to layers
<topic> what is layer </topic>
<topic> different types of layers </topic>
</chapter>
</book>
</bookinfo>
Save: - bookinfo.xml
DTD limitations: - 1) xml dtd doesn’t provide restrictions (freshets) on data, because
it has no primitive data types.
2) DTD doesn’t support name space.
9
Name space: - name space provides methods to avoid elements name comfit (comfit
means confusion). Since elements names are not predefined in xml, element name
comfit will occur when two different documents have same element names.
Ex: - <table>
<td> mango </td>
<td> orange </td>
<td> grapes </td>
</table>
------------------------------------
<table>
<name> heritage </name>
<color> brown </color>
<price> 232 </price>
<size> 453 </size>
</table>
Here both documents are having same root element. So there will be element
name comfit occur.
When above 2 documents are combined there will be element have comfit,
because both documents have same element name.
Save: - emp.xml
Code: -
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v2004 rel. 4 U (http://www.xmlspy.com) by Sunil
(inetsolv) -->
<xs:schema targetNamespace="sunil.info.emp" elementFormDefault= "qualified"
attributeFormDefault="unqualified" xmlns= "sunil.info.emp" xmlns:xs="
http://www.w3.org/2001/XMLSchema">
<xs:element name="emp">
<xs:annotation>
<xs:documentation>Comment describing your root
element </xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="ename" type="xs:string"/>
<xs:element name="eid" type="xs:int"/>
<xs:element name="sal" type="xs:float"/>
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element name="street"
type="xs:string"/>
<xs:element name="city"
type="xs:string"/>
<xs:element name="state"
type="xs:string"/>
12
<xs:element name="phno"
type="xs:long"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Save: - films.xsd
D:\psr\XMLPRO~1\filmsinfo.xsd">
<movies>
<title> Yama Donga </title>
<hero> N.T.R</hero>
<heroein> Priya Mani </heroein>
<vilan> Jaya Prakash Reddy </vilan>
<directer> Rajamouli </directer>
<producer> xxx </producer>
<artists> xxxx </artists>
</movies>
<movies>
<title> Sivaji </title>
<hero> Rajani </hero>
<heroein> Shriya </heroein>
<vilan> xxx </vilan>
<directer> Sankar </directer>
<producer> B Suresh </producer>
<artists> xxxx </artists>
</movies>
</filmsinfo>
Save: - films.xml