0% found this document useful (0 votes)
105 views13 pages

XML Notes

The document discusses XML (Extensible Markup Language) syntax rules and validation. Some key points: - XML has no predefined tags and elements must be defined. It allows storage and exchange of data but not display of data. - XML documents must follow strict syntax rules like having a single root element, proper nesting of tags, and closing all elements. Attribute values must be quoted. - XML can be validated against a Document Type Definition (DTD) which defines legal elements and their structure. DTDs can be internal or referenced externally. - DTDs use symbols like +, *, ? to define element occurrence quantities like one or more, zero or more, and zero or one. Elements can
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
105 views13 pages

XML Notes

The document discusses XML (Extensible Markup Language) syntax rules and validation. Some key points: - XML has no predefined tags and elements must be defined. It allows storage and exchange of data but not display of data. - XML documents must follow strict syntax rules like having a single root element, proper nesting of tags, and closing all elements. Attribute values must be quoted. - XML can be validated against a Document Type Definition (DTD) which defines legal elements and their structure. DTDs can be internal or referenced externally. - DTDs use symbols like +, *, ? to define element occurrence quantities like one or more, zero or more, and zero or one. Elements can
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 13

1

Xml: - Extensible markup language. It is a Meta language. Meta language is a


mother language of all other languages. Xml has no predefined elements & tags. So
we must define our own tags. We can store limit amount of data. Xml documents
are defined as .xml extension.
Xml is mainly useful to store, carry & exchange data, but it is not designed to
display data.
Note: - xml is language neutral, software, hardware independent tool for
transmitting data.
Xml documents can also be used to store data in files or databases but some
applications (xml parser, xml beans) can be written to store data & retrieve data
from database.

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>

Xss: - xml style sheets.


Dtd; - document type definition.
Css: - cascading style sheets.
Xsl=xml document + css commands.
DHTML page=html page+ JavaScript code+ css commands.

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)>

à Data types are 2 types


1) PCDATA parsed character data
2) CDATA character data
All elements are declared in PCDATA. All attributes are declared in CDATA.
Converting 1 data type into another data type is called parse.
PCDATA it represents the text that found between begin tag & end tag of element.
But the text parsed by a parser. Generally we can dell are child elements with
PCDATA.CDATA it also represents the text but that will not parsed by a parser.
Note: - using CADTA attributes cam be declared attributes.

Internal DTD Ex: -


<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (#PCDATA)>
<!ELEMENT sname (#PCDATA)>
<!ELEMENT sid (#PCDATA)>
<!ELEMENT course (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT stname (#PCDATA)>
5

<!ELEMENT city (#PCDATA)>


<!ELEMENT state (#PCDATA)>
]>
<Student>
<sname> Sudheer Reddy </sname>
<sid> 630130 </sid>
<course> java package </course>
<address>
<stname> Ram Nagar </stname>
<city> ATP </city>
<state> AP </state>
</address>
</Student>
(Or)
<!ELEMENT student (#PCDATA)>
<!ELEMENT sname (#PCDATA)>
<!ELEMENT sid (#PCDATA)>
<!ELEMENT course (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT stname (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>

Save: - Student form.dtd

External DTD Ex: -


<?xml version="1.0"?>
<!DOCTYPE student SYSTEM "D:\Student form.dtd ">
<Student>
<sname> Sudheer Reddy </sname>
<sid> 630130 </sid>
<course> java package </course>
<address>
<stname> Ram Nagar </stname>
<city> ATP </city>
<state> AP </state>
</address>
</Student>

à DTD provides 3 Symbols 1) + ---- 2) * ----- 3)? -----


+=One or more occurrence
*=0 or more occurrence
?=0 or one occurrence
Ex: -
<! ELEMENT author (#PCDATA)>
à Declaring one or more occurrence of same elements.
<! ELEMENT letter (message +)>
6

<! ELEMENT message (#PCDATA)>


In above example + sign defines that the message element must occur one or
more times inside letter element.
Xml doc:-
<letter>
<message> hai </message>
<message> how r u </message>
</letter>
<letter> </letter> //invalid

à Declaring 0 or more occurrence of same line.


Ex:- <! ELEMENT letter (message *)>
<! ELEMENT message (#PCDATA)>
In the above example & declares that the message element can occur 0 or
more lines inside letter element.
Ex: - <letter> </letter> //valid
<letter>
<message> hai </message>
<message> how r u </message>
</letter>

à declaring 0 1 occurrence of element


Ex:- <! ELEMENT letter (message?)>
<! ELEMENT message (#PCDATA)>
In the above example & declares that the message element can occur 0 or 1 time
inside letter element.
Ex: - <letter> </letter> //valid
<letter>
<message> hai </message>
</letter>// valid

<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

<! ELEMENT elename EMPTY>


Ex: - <! ELEMENT prod EMPTY>
Xml doc: - <prod> </prod> // valid
<prod> ---- </prod> // invalid

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

<!ELEMENT title (#PCDATA)>


<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT prod EMPTY>
<!ELEMENT chapter (#PCDATA|topic)*>
<!ELEMENT topic (#PCDATA)>
<!ATTLIST prod id CDATA "#REQUIRED"
media CDATA "#IMPLID">
<!ENTITY cost "534.654">

Save: - booksinfo.dtd

File  new  Extensible Markup Language  dtd  ok  browse 


booksinfo.dtd  click ok

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.

 Avoid element name comfit using prefix notation.


Ex: - <f:table>
< f:td> mango </ f:td>
< f:td> orange </ f:td>
< f:td> grapes </ f:td>
</ f:table>
------------------------------------
<g:table>
< g:name> heritage </ g:name>
< g:color> brown </ g:color>
< g:price> 232 </ g:price>
< g:size> 453 </ g:size>
</ g:table>
Avoid element name comfit using name space. For this they invented one attribute
called attributes xmlns, to give meaningful name of xml document.
Syntax: - xmlns:prefix=”URI/URL”
 
Attribute name attribute value.
Ex: - <f:table xmlns:f=”Sunil.table.fruits”>
<f:td> grapes </f:td>
<f:td> mango </f:td>
</f:table>
Xmlns=” “  It has default prefix. It is used to no declare the prefix in the child
tags.
10

Xml schema: - It is newly defined facility that validates xml documents.


Xml schema is based alternative to DTD facility. It is recommended by W3C people.
Just like DTD, xml schema also provides documents structure with the list of legal
elements. Xml schema documents are referred to as .xsd extension.
It provides group of primitive data types (int, float, double, String, char,
date, time, etc ...). It supports name spaces. We can identify the extension is xs:
predefined data type.
These are the features of the xml schema.

Creating xml scheme: -


Xml spy file  new  xsd  Click ok
"ENTER_NAME_OF_ROOT_ELEMENT_HERE" is the default root name.
Click the element & enter the root name.
Root name  right click  add child  select sequence
Sequence  right click  add child  select element
Enter element name & select the type=xs: string in details bar.
Sequence  right click  add child  select element
Enter element name & select the type=xs: int in details bar.
Sequence  right click  add child  select element
Enter element name & select the type=xs: long in details bar.
Element  right click  add child  select sequence
Sequence  right click  add child  select element
Enter element name & select the type=xs: string in details bar.

Schema design  schema setting  click

& click ok button.


Save: - emp.xsd
11

File  new  Extensible Markup Language  schema  ok  browse 


emp.xsd  click ok

<?xml version="1.0" encoding="UTF-8"?>


<emp xmlns="sunil.info.emp" xmlns:xsi= "http://www.w3.org/ 2001/ XMLSchema-
instance" xsi:schemaLocation="sunil.info.emp
D:\psr\XMLPRO~1\emp.xsd">
<ename> Sunil </ename>
<eid> 1 </eid>
<sal> 20000 </sal>
<address>
<street> Rahamat Nagar </street>
<city> HYD</city>
<state> AP </state>
<phno> 9290198907 </phno>
</address>
</emp>

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>

Xml spy file  new  xsd  Click ok

Schema design  schema setting  click

& click ok button.

Save: - films.xsd

File  new  Extensible Markup Language  schema  ok  browse 


films.xsd  click ok

<?xml version="1.0" encoding="UTF-8"?>


<filmsinfo xmlns="latha.films.info" xmlns:xsi="http://www.w3.org/
2001/XMLSchema-instance" xsi:schemaLocation="latha.films.info
13

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

You might also like