0% found this document useful (0 votes)
37 views108 pages

Extensible Markup Language

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views108 pages

Extensible Markup Language

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 108

XML

eXtensible Markup Language

1
Diff b/w HTML & XML

HTML XML
1. designed to display data, 1. describe data

2. displaying information. 2. store data

3. focus on how data looks. 3. Carry data

4. exchange data

3
1. It describes the data
• In HTML, the markup <b>415-555-1234</b> is quite
meaningless.
– what is this number?
• Is it a product number, a phone number, or an employee
number?
• Markup like
<phone_number>415-555-1234<phone_number>
is much more helpful. This is exactly what XML was designed
for.

4
2. It can store the data

• XML can also be used to store data in files or in databases.

• Applications can be written to store and retrieve information


from the store, and
• generic applications can be used to display the data.

5
3. It can be used to exchange data

• With XML, data can be exchanged between


incompatible systems.
1. In the real world, computer systems and databases contain data in
incompatible formats.
• Challenges for developers has been to exchange data between
such systems over the Internet.

2. Solution: Convert the data to XML


• This can greatly reduce this complexity and
• Can be read by many different types of applications.

6
Goal: Strengthen Partnership by
exchanging the data

Partner 1 Partner 2
DTD
information information

XML XML

7
4. It can carry the data
• Other clients and applications can access your
XML files as data sources, like they are
accessing databases.

8
XML Can be Used to Create New
Languages.

Ex:- The mathematics markup language


( MathML)encodes equations.

Representation of Newton’s Law of


Gravitation F=GMm / r2 by using MathML.

9
EX:- ( MathML )
<?xml version=“1.0”>
<math>
<mi>F</mi>
<mo>=</mo>
<mi>G</mi>
<mfrac>
<mnum>
<mi>M</mi>
<mi>m</mi>
</mnum>

<apply>
<power>
<mi>r</mi>
<mn>2</mn>
</power>

</apply>
</mfrac>
</math>

10
• One application might use this input to display the equation.

• Another application might use it to solve the equation with a


series of values.

This is a sign of XML’s power.

11
Ex:-2
• The Wireless Markup Language (WML), used
to markup Internet applications for handheld
devices like mobile phones, is written in XML.

12
XML Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Fred</to>
<from>James</from>
<heading>Reminder</heading>
<body>Do send me the documents this weekend!</body>
</note>

Output 

13
XML Building Blocks

1. XML Elements 2. Control 3. Entities


Information
Conditions

a. Processing Internal
a. Attributes Instructions
Prolog
b. Document External
b. Element Type Definition
Naming (DTD)

Internal DTD

External DTD

Elements

c. Comments
1. Elements
 Delimited by angle brackets
General format: <element> … </element>
Empty element: </empty-Element>
Ex. </ingredient>

15
Conditions for using Elements
Condition Example
1. All XML elements must have a closing tag <br>This is a paragraph (valid in HTML)

<phone>This is a paragraph</phone> (in XML)
 
2. XML tags are case sensitive With XML, the tag <Letter> is different from the
Opening and closing tags must therefore be tag <letter>.
written with the same case: <Message>This is incorrect</message>
<message>This is correct</message>
3. All XML elements must be properly nested <b><i>This text is bold and italic</i></b>
Improper nesting of tags makes no sense to
XML.
4. All XML documents must have a root tag All other elements must be nested within the root
The first tag in an XML document is the root element.
tag. <root>
All elements can have sub elements <child>
(children). <subchild>.....</subchild>
</child>
</root>

16
Condition Example
5. XML Elements are extensible and they <note>
have relationships. <date>1999-08-01</date>
XML documents can be extended to carry <to>Tove</to>
more information. <from>Jani</from>
<heading>Reminder</heading>

6. XML Elements have simple naming rules. Book Title: My First XML
Elements are related as parents and Chapter 1: Introduction to XML
children. What is HTML
What is XML

<book>
<title>My First XML</title>
<chapter_no>Chapter 1</chapter_no>
<chapter>Introduction to XML
<para>What is HTML</para>
<para>What is XML</para>
</chapter>
</book>

17
1.A Element Attributes
Name-value pairs that occur inside start-tags after element name, like: <element attribute=“value”>

Element Attributes conditions:

1. Attribute values must <?xml version="1.0"?>


always be quoted <note date="12/11/99">
With XML, it is illegal to <to>Tove</to>
omit quotation marks <from>James</from>
around attribute values. <heading>Reminder</heading>
<body>Mail me this weekend!</body>
</note>
This is correct: date="12/11/99".
This is incorrect: date=12/11/99.

18
1.b. Element Naming
XML elements must follow these naming rules:
a. Names can contain letters, numbers, and other characters
b. Names must not start with a number or other punctuation
characters
c. Names must not start with the letters xml (or XML or Xml ..)
d. Names cannot contain spaces

19
2. Control
Information

2. Control Information
a. Processing
Instructions
• Prolog: The part of an XML document Prolog
b. Document Type
that precedes the XML data Definition (DTD)
Includes
c. Comments
a. Processing Instructions :
XML declaration for version [, encoding, standalone]

b. Document Type declaration i.e DTD ( optional )


Example
<?xml version="1.0" encoding="ISO-8859-1"?> <!– a 
<!DOCTYPE note SYSTEM “InternalNote.dtd"> <!– b 
20
2. a Processing Instructions:
XML Declaration for version

version :- Tells the XML parser which version to use .


XML parser: program responsible for processing XML document.

encoding :- Defines the character encoding used in the


document.
1. ASCII :- 7 bits character set -128 characters.

2. ISO-8859-1:- 8 bits character set – 256 characters

( ASCII + Western European letters and symbols ).

21
Standalone :- tells the XML processor whether
they are any other files to load.

Syntax :-

<?xml name1=value1 name2=value2 name3=value3?>

Examples

22
2.B Document Type Definition ( DTD)
DTD defines the grammatical rules for the document.
• DTD either held in a separate file so that it can be used by many
documents, or placed in the same file.
Ex. <!DOCTYPE Recipes SYSTEM “recipe.dtd”>

• Here various parameters will be specified


1. DTD declaration ( internal / external ).
2. Name of the root element.
3. System/Public
4. url of DTD
5. Entity declaration.

• Syntax:- ( External DTD )


<!DOCTYPE root-element SYSTEM/PUBLIC url-of-dtd >

23
Contents of ( DTD)

• Defines structure of the documents


1. Allowable tags and their attributes
2. Tag nesting order
3. Number of occurrence of tags
4. Constraints on attribute values
5. Entity definitions

24
Ex:- ( External DTD )

<!DOCTYPE authorslist SYSTEM “authors.dtd”>

SYSTEM – DTDs defined by individuals and organizations.


PUBLIC – DTDs defined by International Organizations like W3C
(standards)

some DTDs are available as international standards, such as those


recommendations of W3C which relate to HTML.
Other DTDs are developed by individuals and organizations for their own use.

25
Internal DTD
Ex:- ( Internal DTD )
<!DOCTYPE authorslist
[
DTD definition
entity-definitions
]
>

26
<?xml version=“1.0” encoding=“US-ASCII” standalone=“no” ?>
<!DOCTYPE authorslist SYSTEM “authors.dtd” >
<authorslist>
<name>
<firstname>chris</firstname>
<lastname>bates</lastname>
<book>web programming</book>
<year>1999</year>
</name>
<name>
<firstname>subramanyam</firstname>
<lastname>allamaraju</lastname>
<book>java server programming</book>
<year>2000</year>
</name>
</authorslist>

27
authors.dtd file

<!ELEMENT authorslist (name+)>


<!ELEMENT name(firstname, lastname, book, year)>
<!ELEMENT firstname(#PCDATA)>

<!ELEMENT lastname(#PCDATA)>

<!ELEMENT book(#PCDATA)>

<!ELEMENT year(#PCDATA)>

28
Operators used for element names
1. +: one/more occurrences
2. *: any number of occurrences
3. ?: 0/1 occurrence
4. #PCDATA: parsed character data, may contain XML markup
5. CDATA : plain text character data, not passed thru’ parser
engine
EX. Partial dtd for cookbook

<!ELEMENT ingredient(qty, item)> <!


ELEMENT qty ( EMPTY)>
<!ATTLIST qty
amount CDATA #REQUIRED
unit CDATA “ml”>
<!ELEMENT item (#PCDATA)>
29
Why Use a DTD?
1. Each of our XML files can carry a description of its own
format.

2. Independent groups of people can agree to use a standard


DTD for interchanging data.

3. Application can use a standard DTD to verify that the data we


receive from the outside world is valid.

4. We can also use a DTD to verify our own data.

30
An XML Example

• We will define a person like this:


– A person is required to be either male or female
– A person has a name which consists of:
– A first name
– A last name
– One (optional) nickname
– A person has an occupation

31
Representating in Xml
<person gender=“male” >
<name>
<first>Charles</first>
<last>Myers</last>
<nickname>C.R.</nickname>
</name>
<occupation> college professor </occupation>

</person>

32
An Inline DTD example:
<?xml version="1.0"?>
<!DOCTYPE person [
<!ELEMENT person (name, occupation) >
<!ELEMENT name (first, last, nickname?)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT last (#PCDATA)>
<!ELEMENT nickname (#PCDATA)>
<!ELEMENT occupation (#PCDATA)>
<!ATTLIST person gender (male | female) #REQUIRED>
]>

<person gender=“male”>
<name>
<first>Charles</first>
<last>Myers</last>
<nickname>C.R.</nickname>
</name>
<occupation>college professor</occupation>
</person>
33
An External DTD example:

<?xml version="1.0"?>
<!DOCTYPE person SYSTEM “persons.dtd">

<person gender=“male”>
<name>
<first>Charles</first>
<last>Myers</last>
<nickname>C.R.</nickname>
</name>
<occupation>college professor</occupation>

</person>

34
persons.dtd

<!ELEMENT person (name, occupation)>


<!ELEMENT name (first, last, nickname?)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT last (#PCDATA)>
<!ELEMENT nickname (#PCDATA)>
<!ELEMENT occupation (#PCDATA)>
<!ATTLIST person gender (male | female) #REQUIRED>

35
Element declarations

Syntax :-
<!ELEMENT name content-model >

content – model :–Represents what kind of content


can be used inside that element.

36
Five different kinds of content models
(elements)
1. Empty elements
Declares an empty element—It uses the keyword EMPTY
<!ELEMENT name EMPTY>

2. Elements with no content restrictions


Declares an element that can contain any thing—it uses the keyword ALL <!
ELEMENT name ALL>

3.Elements containing only character data


For elements that contain character data but not elements. Use the content
model (#PCDATA)
<!ELEMENT name (#PCDATA)>

#PCDATA – “parsed character data”


– Characters will be checked by an XML parser for entity references, if finds
entities, then they will be replaced with entity values.

37
4.Elements containing only elements
expressed using a formula in special notation.

<ELEMENT authorslist (name+) >


<ELEMENT name (firstname,lastname,book,year) >

5. Elements with mixed content

<ELEMENT element_name (#PCDATA | element_name …) >

38
39
Grouping Elements

40
Defining attributes

Syntax :-
<!ATTLIST element_name
attr1_name att_type default_value
attr1_name att_type default_value
...........
>

41
Attribute type categories

String Tokenized Enumerated


Types Types Types

CDATA
ID
Enumeration

IDREF

IDREFS

42
Attribute default values

43
Inline DTD for Newspaper
1. <!DOCTYPE NEWSPAPER [

2. <!ELEMENT NEWSPAPER (ARTICLE+)>


3. <!ELEMENT ARTICLE (HEADLINE,BYLINE,LEAD,BODY,NOTES)>
4. <!ELEMENT HEADLINE (#PCDATA)>
5. <!ELEMENT BYLINE (#PCDATA)>
6. <!ELEMENT LEAD (#PCDATA)>
7. <!ELEMENT BODY (#PCDATA)>
8. <!ELEMENT NOTES (#PCDATA)> 

9. <!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED>


10. <!ATTLIST ARTICLE EDITOR CDATA #IMPLIED>
11. <!ATTLIST ARTICLE DATE CDATA #IMPLIED>
12. <!ATTLIST ARTICLE EDITION CDATA #IMPLIED>
13. ]>
44
String
Types
CDATA CDATA

– Any string of non-markup characters is legal


– markup characters ( < > ” ’ & ) will not be allowed.
– Content won’t be interpreted as markup

Ex:-

45
Tokenized
Types

ID ID
– Used to establish a unique
IDREF
identifier for an element.
Ex:- IDREFS

<!ATTLIST book myID ID #REQUIRED>

46
Tokenized
Types
IDREF

• Used to refer to the ID of another element. ID

IDREF
<!ATTLIST title ref IDREF #IMPLIED>
IDREFS

47
Tokenized
Types

IDREFS
ID

– the value is a list of other ids


IDREF

Ex:-
IDREFS

<!ATTLIST title refs IDREFS #IMPLIED>

48
Enumerated
Types

Enumerated value list


Enumeration

– An enumerated list of values that the attribute value must


match.
Ex:-

<!ATTLIST schedule
day ( mon | tue | wed | thu | fri | sat | sun ) “sun” >

49
3. Document Entities
Some markup elements can contain complex data.
These elements are called Entities.

• They refer to a data item, typically text.


• They may be parsed or non-parsed.
• All complex data items which do not need to be parsed should be defined
as non-parsed.
Syntax:
– General entity references start with & and end with ;
– The entity reference is replaced by its true value when parsed.
– The characters < > & “ ‘ require entity references to avoid
conflicts with the XML application ( parser )
50
Ex:-

<?xml version=“1.0” ?>


<!ENTITY copyright “2005, Prentice Hall”>
<book>
<title> Core Servlets &copyright;</title>
</book>

Output: Core Servlets 2005, Prentice Hall

51
Types of Entity
1. Internal Entity
• Included in XML file
• Used to create small pieces of data;
• This can be used repeatedly through the schema.
• Ex: <!ENTITY POS “pinch of salt”>
• Instruction: <item> Finally add the &POS;</item>

2. External Entity
• Included in the different file and referred in the xml file.
• Ex: <!ENTITY myimage SYSTEM “winter.jpg” NDATA
JPEG>
• NDATA: notation for the type of data.
52
Namespaces
• The flexibility that XML brings can also be the cause for
conflicts when combining documents.
• Ex:
one document may use element named record to store data
about vehicle registration, another may use record to
represent employee details.

• Namespace: Way of keeping the names used by


applications separate from each other.

53
Name Conflicts
• In XML, element names are defined by the developer. This often results in
a conflict when trying to mix XML documents from different XML
applications.

1. This XML carries HTML table information:


<table>
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>

2. This XML carries information about a table (a piece of furniture):


<table>
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>

Both contain a <table> element, but the elements have different content
and meaning. 54
Solving the Name Conflict
Using a Prefix
• Name conflicts in XML can easily be avoided using a name prefix.

1. This XML carries information about an HTML table, and a piece of furniture:
<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two <table> elements
have different names.

55
XML Namespaces –
The xmlns Attribute
1. When using prefixes in XML, a so-called namespace for the prefix
must be defined.

2. The namespace is defined by the xmlns attribute in the start tag of


an element.

3. The namespace declaration has the following syntax.

xmlns:prefix="URI".

56
• <root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table xmlns:f="http://www.w3schools.com/furniture">
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
</root>

• In the example above, the xmlns attribute in the <table> tag give the
h: and f: prefixes a qualified namespace.
• When a namespace is defined for an element, all child elements with
the same prefix are associated with the same namespace.
57
Namespaces can be declared in the elements where
they are used or in the XML root element:
• <root
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">

<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

</root>

58
XML Namespace

Its a collection of names, identified by a URI , which are used


in XML document as element names and attribute names.

Steps to create a namespace


1. To begin using a namespace , you must first publicly declare it.
2. The namespace declaration occurs with an element .
3. It can be applied to just a single element, or it can be applied to an
entire document by placing the declaration within a document’s root
element.

59
Syntax :-

<name: element_name xmlns :name = “URI”


>

– namespace prefix “name” is a user defined name.


– the value of the xmlns: attribute is a URI, usually
belongs to the organization that maintains the namespace.

60
Example of a document combining two
namespaces , myns and eq

<?xml version=“1.0”?>
<myns: journal xmlns:myns= “http://www.psycholabs.org/mynamespace/”>

<myns:experiment>
<myns:date>March 4,2001</myns:date>
<myns:abstract>xxxxxxxxxx</myns:abstract>

<eq:formula xmlns:eq= “http://www.mathsstuff.org/”>


<eq:valiable>p</eq:variable>
<eq:valiable>m</eq:variable>
<eq:valiable>M</eq:variable>
</eq:formula>
</myns:experiment>
</myns:journal>

61
XML Validation
• "Well Formed" XML document
--correct XML syntax
• "Valid" XML document
– “well formed”
– Conforms to the rules of a DTD (Document Type Definition)
• XML DTD
– defines the legal building blocks of an XML document
– Can be inline in XML or as an external reference
• XML Schema
– an XML based alternative to DTD, more powerful
– Support namespace and data types

62
XML Schema
Limitations of DTD

1. Language :- DTDs are written in a language which is dissimilar to XML


syntax. Schemas are written in XML syntax.

2. Data Constraints:- DTDs have minimal data constraints available.


for ex:-
using an XML DTD , an element for pin code can be constrained as
PCDATA, as in:
<!ELEMENT pincode #PCDATA>
unfortunately , this sets up the possibility that:
<pincode>ABC-123345-fbx</pincode>
though it does not represent a pincode in any form.

63
3. fixed set of content models :-

Designers are limited to a fixed set of content models.

Schemas provide
User –Defined data types, which allow greater flexibility in
limiting and expressing content.

64
XML Schema

1. Its an XML-based alternative to DTD.

2. Describes the structure of an XML document.

3. The XML Schema language is also referred to as XML


Schema Definition (XSD).

65
XML Schema
An XML Schema defines:
1. elements that can appear in a document

2. attributes that can appear in a document

3. which elements are child elements

4. the order of child elements

5. the number of child elements

6. whether an element is empty or can include text

7. data types for elements and attributes

66
There are two element types

1. Simple
2. Complex

– A simple type is one that does not contain attributes and other
elements in the content.
– Otherwise, that element is called complex type.
Syntax:-
Simple type :-
<namespace_reference :element name=“element_name”

type=“namespace_reference:datatype” />

67
Complex Type
<namespace_reference :element name=“name”>
<namespace_reference:complexType >
<namespace_reference:attribute name=“name”
type=“namespace_reference:datatype” />
</namespace_reference:complexType>
</namespace_reference :element>

68
A Simple XML Document

Ex-1
<?xml version="1.0"?>
<note>
<to> Tony </to>
<from> Jani </from>
<heading> Reminder </heading>
<body> Don't forget me this weekend! </body>

</note>

69
A DTD File

<!ELEMENT note (to, from, heading, body)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

70
An XML Schema
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >

<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
71
EX-2

<?xml version="1.0"?>
<!DOCTYPE person SYSTEM “persons.dtd">

<person gender=“male”>
<name>
<first>Charles</first>
<last>Myers</last>
<nickname>C.R.</nickname>
</name>

</person>

72
An XML Schema
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >

<xs:element name=“person">
<xs:complexType>
<xs:attribute name=“gender” type=“xs:string”/>
<xs:sequence>
<xs:element name=“first" type="xs:string"/>
<xs:element name=“last" type="xs:string"/>
<xs:element name=“nickname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

73
Diff b/w DTD & XSD
1. DTD supports types ID,IDREF,CDATA etc.,
2. Schema supports all primitive and user defined
data types
3. Schema hava minOccurs and maxOccurs
attributes
4. XML Schemas are extensible to future additions
5. XML Schemas are richer and more powerful than
DTDs
6. XML Schemas are written in XML

74
XML Parsers
• The parser is the engine for interpreting our XML
documents
• The parser reads the XML and prepares the information
for your application.
How to use a parser
• 1. Create a parser object
• 2. Pass your XML document to the parser
• 3. Process the results

75
Types of Parsers

– DOM is the Document Object Model

– SAX is the Simple API for XML

76
Document Object Model (DOM)
• DOM uses a tree based structure.
• DOM reads an entire XML document and builds a
Document Object.
– The Document object contains the tree structure.
– The top of the tree is the root node.
– The tree grows down from this root, defining the child
elements.
– DOM is a W3C standard.
• Using DOM, we can also perform insert nodes,
update nodes, and deleting nodes.

77
XML example to construct DOM tree
<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>
1   <book category="cooking">
Books.xml file
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
4 <book category="web" cover="paperback">
  </book>
    <title lang="en">Learning XML</title>
2   <book category="children">
    ……  </book>     <author>Erik T. Ray</author>
    <year>2003</year>

3
  <book category="web">     <price>39.95</price>
…..    </book>   </book>

</bookstore>
 
78
XML DOM Tree Example
XML DOM views an XML document as a node-tree.
All the nodes in the tree have a relationship to each other.

79
Look at the following XML fragment:
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday
Italian</title>
    <author>Giada De
Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
</bookstore>

80
XML DOM Parsing
1. Most browsers have a build-in XML parser to read and manipulate XML.

2. The parser converts XML into a JavaScript accessible object.

Parsing XML
1. XML DOM contains methods (functions) to traverse XML trees, access, insert, and
delete nodes.

2. Before an XML document can be accessed and manipulated, it must be loaded into
an XML DOM object.

3. parser reads XML into memory and converts it into an XML DOM object that can be
accessed with JavaScript.

81
Load an XML Document
The following JavaScript fragment loads an XML document ("books.xml"):

var xmlDoc;
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(“books.xml”);

var allbooks=xmlDoc.getElementsByTagName(“book”);

var book=allbooks[0].childNodes[0].nodeValue;

Output: Everyday Italian

82
DOM methods & Properties
1. Document.getDocumentElement()
– Returns the root element of the document
2. Node.firstChild() and Node.lastChild()
– Returns the first or last child of a given Node
3. Node.nextSibling() and Node.previousSibling()
– Returns the next or previous sibling of a given Node.

4. Node.childNodes()
– Returns the child nodes of given Node
5. Node.getNodeName
– Returns tagname of a Node
6. Node.getNodeTypedValue
– Returns content of a Node

83
Example for XML and JavaScript userinformation.xml
1. <script language="javascript"> contain:
2. function validateFromXml()
userinfo
3. {
userID (must be
4. var xmlDoc;
unique value)
5. xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
username
6. xmlDoc.async=false;
Address
7. xmlDoc.load("userinformation.xml");
H.no.
8. var allusers=xmlDoc.getElementsByTagName("username");
9. var uname=allusers[0].childNodes[0].nodeValue;
Lane no,
10. document.writeln("name is "+uname);
Street no.
11. return; Area
12. } city
13. </script> State
14. </HEAD> Pincode
Phone number
15. <BODY onLoad="validateFromXml()"> Residence
16. </BODY> Mobile
17. </HTML> userinfo
.
.
.
. 84
XML DOM Manipulations
Operation Example
Get the Value of an Element x=xmlDoc.getElementsByTagName("title")
[0].childNodes[0];
txt=x.nodeValue;

get Value of an Attribute txt=xmlDoc.getElementsByTagName("title")


[0].getAttribute("lang");
Change the Value of an x=xmlDoc.getElementsByTagName("title")
Element [0].childNodes[0];
x.nodeValue="Easy Cooking";
Change the Value of an x=xmlDoc.getElementsByTagName("book");
Attribute for(i=0;i<x.length;i++)
The setAttribute() method can  {
be used   x[i].setAttribute("edition","first");
 }

85
Operation Example
Create an Element newel=xmlDoc.createElement("edition");
createElement():creates a new element newtext=xmlDoc.createTextNode("First");
node. newel.appendChild(newtext);
createTextNode(): creates a new text x=xmlDoc.getElementsByTagName("book");
node. x[0].appendChild(newel);
appendChild(): adds a child node to a Example explained:
node (after the last child). Create an <edition> element
Create a text node with value = "First"
To create a new element with text Append the text node to the <edition> element
content, it is necessary to create both Append the <edition> element to the first
an element node and a text node. <book> element

Remove an Element x=xmlDoc.getElementsByTagName("book")[0];


removeChild(): removes a specified x.removeChild(x.childNodes[0]);
node (or element).
The following code fragment will remove
the first node in the first <book>
element:

86
SAX
• SAX parsers are event-driven

– The parser fires an event as it parses each XML


element/item.
– It is up to you to decide what you want to do with those
events, if you ignore them the information in the event
is discarded.
– The developer can write a java code that handles the
events.

87
SAX events
The SAX API defines a number of events
• startDocument
– Signals the start of the document.
• endDocument
– Signals the end of the document
• startElement
– Signals the start of an element. The parser fires this event
when all of the contents of the opening tag have been
processed. That includes the name of the tag and any
attributes it might have.
• endElement
– Signals the end of an element.

88
Diff b/w DOM & SAX
DOM SAX
• Uses more memory and has • Uses less memory and provides
more functionality less functionality
• The entire file is stored in an • The developer must handle each
internal Document object. This SAX event before the next
may consume many resources event is fired.

• Slow execution • Fast execution


• Generally used at client -side • General used at sever-side

89
Displaying XML

• XML documents do not carry information about how to display


the data

• We can add display information to XML with


– CSS (Cascading Style Sheets)
– XSL (eXtensible Stylesheet Language) --- preferred

90
Displaying by using CSS

CSS important properties:


Position
left
right
top
font-style
font-size
color
background-color

91
display

Value Description

none The element will not be displayed

block The element will be displayed as a block-


level element, with a line break before
and after the element

inline Default. The element will be displayed as


an inline element, with no line break
before or after the element

92
Employee.html Style.css
<?xml version="1.0"?>

<?xml-stylesheet type="text/css“
href="style.css"?> first {
position:absolute;
left:300;
<person gender=“male”>
color:blue;
<name> }
<first>Charles</first> last {
<last>Myers</last> display:block;
<nickname>C.R.</nickname> color:red;
</name> }

</person>

93
Output

94
XSL
• XSL stands for eXtensible Stylesheet Language.
• CSS = HTML Style Sheets
• XSL = XML Style Sheets
– More Than a Style Sheet Language
– XSL consists of three parts:
• XSLT - a language for transforming XML documents
• XPath - a language for navigating in XML documents
• XSL-FO - a language for formatting XML documents .

95
XSLT
• XSLT stands for Extensible Stylesheet Language
Transformations.

• XSLT is used to transform XML documents into other


kinds of documents--usually, but not necessarily,
HTML

• XSLT uses two input files:


– The XML document containing the actual data
– The XSL document containing information about how to
display the xml elements.
96
• There are two programming styles that can be
used with XSLT.
– Procedural programming style
– Rule based programming style
• It also possible to mix both programming
styles.

97
Quick Overview of XSL
XSL

XSLT XPath XSL-FO

Uses files: .xml and .xsl

Procedural Rule-Based
programming programming types
Style style

98
Procedural programming style
• Procedural operations
– Control structures : XSLT instructions such as
xsl:for-each, xsl:if and xsl:choose etc.
provide control structures like conditional
execution or loops.
– Accessing content : The xsl:value-of instruction
writes the content of the current node to the
target document.
• ex :-

99
Myxsl.xsl file xml file
<?xml version="1.0"?>
<HTML xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<?xml version="1.0"?>
xsl:version="1.0">
<?xml:stylesheet type="text/xsl“
<BODY> href="myxsl.xsl"?>
<H1>Book Order</H1> <bookOrder>
<TABLE border="1" cellpadding="6"> <shipTo country="US">
<TR> <name>Venus </name>
<TH>Title</TH> <street>Chapel Street</street>
<TH>ISBN</TH> <city>papermoon</city>
<TH>Price</TH> </shipTo>
</TR>
<xsl:for-each select="bookOrder/orderlist/item"> <billTO country="UK">
<name>Rick </name>
<TR>
<street> Marine </street>
<TD><xsl:value-of select="title"/></TD> </billTO>
<TD><xsl:value-of select="@ISBN"/></TD>
<TD><xsl:value-of select="price/@currency"/> <note>Special Valentine wrapping!</note>
<xsl:value-of select="price"/> <orderlist>
</TD> <item ISBN="5855-9">
</TR> <title>The mint lawn</title>
<quantity>1</quantity>
</xsl:for-each> <price currency=“$">19</price>
<note>On stock</note>
</TABLE> </item>
</BODY> </orderlist>
</HTML> </bookOrder>

100
Changes made in xsl file
<BODY bgcolor="cyan">
<H1>Book Order</H1>
<TABLE border="1" cellpadding="6" cellspacing="10">
<TR>
<TH>Title</TH>
<TH>ISBN</TH>
<TH>Price</TH>
</TR>
<xsl:for-each select="bookOrder/orderlist/item">
<TR>
<TD><H2><xsl:value-of select="title"/></H2></TD>
<TD><xsl:value-of select="@ISBN"/></TD>
<TD><xsl:value-of select="price/@currency"/>
<xsl:value-of select="price"/>
</TD>
</TR>
</xsl:for-each>

</TABLE>
</BODY>
101
Number of items are increased in xml file
<note>Special Valentine wrapping!</note>
<orderlist>
<item ISBN="5855-9">
<title>The mint lawn</title>
<quantity>1</quantity>
<price currency="$">19</price>
<note>On stock</note>
</item>

<item ISBN="5855-10">
<title>Web TEchnologies</title>
<quantity>2</quantity>
<price currency="$">45</price>
<note>On stock</note>
</item>
</orderlist>
</bookOrder>

102
Rule based programming style
– Consists of set of templates.
– xsl:template -defines a set of rules for transforming
elements .
– The match attribute is used to associate a template
with an XML element.
Syntax:-
<xsl:template match=element_name >
Rules for transforming element
</xsl:template>
– match="/" defines the template for root element.

103
• xsl:apply-templates –applies all the
templates defined in the document recursively.

• select attribute --to specify the child nodes


which you want to use inside a parent.

104
<xsl:stylesheet version="1.0“
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body> <?xml version="1.0"
<h2>My CD Collection</h2> encoding="ISO-8859-1"?>
<xsl:apply-templates /> <?xml:stylesheet type="text/xsl"
</body>
</html> href="mycd.xsl"?>
</xsl:template> <catalog>
<xsl:template match="cd">
<p> <xsl:apply-templates select="title"/> <cd>
<xsl:apply-templates select="artist"/> <title>Empire Burlesque</title>
</p> <artist>Bob Dylan</artist>
</xsl:template>
<country>USA</country>
<xsl:template match="title"> <company>Columbia</company>
Title: <span style="color:#ff0000"> <price>10.90</price>
<xsl:value-of select="."/></span>
<br />
<year>1985</year>
</xsl:template> </cd>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br /> </catalog>
</xsl:template>

</xsl:stylesheet> mycd.xsl file

105
Output

106
XML Advantages/Goals
• 1. XML is used to Exchange Data
– With XML, data can be exchanged between
incompatible systems
• 2. XML and B2B
– With XML, financial information can be
exchanged over the Internet.
• 3. XML can be used to Share Data
– With XML, plain text files can be used to share
data.

107
• 4. XML is free and extensible
– XML tags are not predefined. we must "invent" your
own tags.
• 5. XML can be used to Store Data
– With XML, plain text files can be used to store data.
• 6. XML can be used to Create new Languages
– XML is the mother of WAP and WML.
• 7. HTML focuses on "look and feel”
– XML focuses on the structure of the data.

108
END

109

You might also like