0% found this document useful (0 votes)
347 views91 pages

Exemel

This document provides an overview of Extensible Markup Language (XML). It discusses why XML was created, how it differs from HTML, the types of XML documents, namespaces, Document Type Definitions (DTDs), and XML Schema. It also covers XPath, which is a language for addressing parts of an XML document using path expressions.

Uploaded by

aparnajanwalkar
Copyright
© Attribution Non-Commercial (BY-NC)
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)
347 views91 pages

Exemel

This document provides an overview of Extensible Markup Language (XML). It discusses why XML was created, how it differs from HTML, the types of XML documents, namespaces, Document Type Definitions (DTDs), and XML Schema. It also covers XPath, which is a language for addressing parts of an XML document using path expressions.

Uploaded by

aparnajanwalkar
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 91

Extensible Markup Language

Contents
Why XML? Introduction HTML & XML Types of XML Documents Namespaces

DTD Schema

Why XML?
It is a standard, easily extensible way to encode information To resolve issues in large scale electronic information publishing To create richly structured arbitrary document Due to its easy parseability, software can transform it in countless ways

Introduction
XML stands for eXtensible Markup Language A technology from W3C A method of creating document with User defined tags In XML, data speaks what it is? Not how it looks XML can serve web with HTML and WML transformation

HTML & XML

Comparison

HTML Data Sample


Following is an HTML Sample Code Guess what is talks about!!!

<b>Vishwanathan</b> <i>Nalasupara</i>

Umm
There is some bold and italic text But, we could still guess what data it is representing Bold text

Must be the name of some person


Must be the name of some place

Italic text

Finally, Good guesses pays.


7

Sample XML Document

Here is the XML version of the same data

<name>Vishwanathan</name> <location>Nalasupara</location>

Do you still need to guess what it is?

Comparison

HTML

Data Presentation Un-Structured Non-Case-Sensitive Vocabulary of build-in tags No error checks Current Version - 4.x Extension - .htm, .html

Comparison

XML

Data Definition 100% Structured 100% Case-Sensitive User defined tags Checks for errors Version since beginning - 1.0 Extension - .xml

10

Types of XML Documents

Two Types of XML document

Well-Formed

An XML document follows all the structuredness rules of the XML language A well-formed XML document is Valid if it conforms to its Vocabulary

Valid

Every valid XML document is a well-formed XML document but, every well-formed document is not a valid XML document
12

Well-Formedness Rules

Document should contain at least Prolog

XML Declaration (Mandatory) All tags are grouped within a single tag

Root Element (Mandatory)

13

Continued

Should follow rules given by W3C There can be only one root element Root element should include all other elements Every element should have a closing tag Empty elements should be self-closing

<empty/>

Proper nesting of tags should be there


14

Continued

Attribute values should be enclosed in double quotes Naming rules should be followed for elements and attributes

Should start with A-Z or a-z or _ Can contain A-Z or a-z or 0-9 or _ or - or . (dot) Any external data embedded in XML should also be well-formed
15

Is it a Well-Formed Document?
<?xml version=1.0 ?> <Person gender=Male> <Name>Zubair Shaikh<Name> <Age>29</Age> <City>Mumbai<City/> <Indian> </person>

16

Why to Validate XML Document?


Lets consider the Person example A well-formedness check will make it sure that there is no syntax error but, What if you want to make it sure, that

The attribute value for gender is male/female The name element is the first child of person All the elements are appeared and that too in the desired sequence

17

Valid XML Document


Follows a vocabulary Vocabulary is a user defined set of elements and rules such as

The element that can or must appear How often the element can appear How the element can be nested The allowable, required and default attributes

18

Defining Vocabulary

Vocabulary can be defined using


DTD - Document Type Definition XSD - XML Schema Definition

19

Namespaces

Why Namespaces?
Naming standards to the element/attribute are too brittle Element and Attribute names can be ambiguous There is no way to know if a name is already in use

21

Introduction
It is introduced to makes naming unique and free of ambiguity It refers to two-part name as Qualified Names or QNames It deals with the mechanics of associating a URI qualifier with element and attribute names

URIs are used because they provide a unique naming convention


22

Qualified Names
QNames are used in place of element names QName have a prefix and a local part They are like <prefix:localPart>

<person:name>

It can be interpreted as

{http://www.example.com/person}name

It is important to understand that the significance of prefixes lies in the URI


23

Declaring Namespaces

The syntax of namespace declaration is

<prefix:elementName xmlns:prefix=URI>

The following example declare the namespace person for URI http://www.example.com/persons

<persons:person xmlns:persons= http://www.example.com/persons>

24

An Example
<?xml version=1.0 ?> <persons:person xmlns:persons= http://www.example.com/persons> <persons:name>Zubair<persons:name> <persons:age>29</persons:age> <persons:city>Mumbai</persons:city> </persons:person>

25

Default Namespace Example


<?xml version=1.0 ?> <person xmlns=http://www.example.com/persons> <name>Zubair<name> <age>29</age> <city>Mumbai</city> </person>

26

Document Type Definition

DTD

Why DTD?
A DTD adds syntactical requirements in addition to the well-formed requirement It helps in eliminating errors when creating or editing XML documents It clarifies the intended semantics It simplifies the processing of XML documents

28

Introduction
Document Type Definitions (DTDs) impose structure on XML documents The DTD is a syntactic specification The DTD follows regular expressions

29

Requirements

Consider the following example

<?xml version=1.0 ?> <person> <name>Zubair Shaikh</name> <age>29</age> <city>Mumbai</city> <phone>022-22872183</phone> <phone>+91-9829748328</phone> <email>[email protected]</email> <indian/> </person>

30

Requirements
person is an element name, age and city appears in a sequence There can be one or more occurrences of phone email is the optional element Person can be either Indian or NRI

31

Specifying the Structure


person name,age,city phone+ email?

To specify the person element


To specify the sequence of name age and city To specify one or more occurrences of phone To specify email is optional (Zero or One) To specify either indian or nri
32

indain|nri

Summary of Regular Expressions


e e1,e2 e*

e?
e+ e1|e2 (e)

An element Sequence Zero or more occurrences Zero or One occurrences One or more occurrences Either Grouping

33

An Example
<!ELEMENT person (name,age,city,phone*,email?, (indian|nri))> <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT phone (#PCDATA)> <!ELEMENT email (#PCDATA)> <!ELEMENT indian EMPTY> <!ELEMENT nri EMPTY>

34

XML Schema Definition

Schema

Why XML Schema?

Have you realized any issues with DTD?


Is it an XML document Need for Data types Validating element values Specifying upper limit of occurrences Defining custom types for similar elements

36

Introduction
XML Schema is an XML based alternative to DTD An XML vocabulary for expressing your data's business rules Object-oriented'ish With the support for data types Better support for mixed content model

37

XML Path

XPath

Introduction
XPath is a W3C recommendation (1999) According to W3C

It is a Language for addressing path of an XML document

XPath models XML documents as trees of nodes using the Document Object Model XPath uses path expressions to locate nodes with XML documents

39

Introduction
XPath is an efective and powerful way of query XML documents XPath is a language for specifying parts of an XML document XPath also inludes a library of standard functions for node-sets, strings, boolean and numbers XPath uses path expressions to identify XML document parts

40

An Example
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
41

Locating Nodes
Path = /library/book/price
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
42

Wild Cards
Path = /library/book/*
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
43

Selecting Branches
Path = /library/book[1]
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
44

Selecting Branches
Path = /library/book[last()]
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
45

Select Several Paths


Path = /library/book/title|/library/book/price
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
46

Selecting Attributes
Path = //book[@isbn=43-4749]
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
47

Location Paths
One of the most important xpath expressions is the location path. A location path expression results in a node set Each location path consist of one or more location steps

48

Location Step Syntax

Each location step has


An axis A node test One or more predicates (optional) axisname::nodetest[predicate] child::book[author=Mark]

Location step syntax


49

XPath Axis
There are several axes in XPath Each of which selects a different subset of the nodes in the document That is depending on the context node

50

Location Path: Axes


Ancestor Attribute Descendent Following Namespace Preceding Self Ancestor-or-self Child Descendent-or-self Following-sibling Parent Preceding-sibling Sibling

51

Location Path: Child Axis

The child axis with library context node

library book title author price book title author price

52

Location Path: Sibling Axis

The preceding-sibling axis with price context node


library book title author price book title author price

53

Location Path: Nodes

There are seven types of node


Root nodes Element nodes Text nodes Attribute nodes Namespace nodes Processing instruction nodes Comment nodes

54

Location Path: Node Test


Node test determine what kind of nodes be selected along a given axis The node test is applied to each node in the axis Example location path with node test

child::foo - foo child element parent::text() - text node of parent descendent::comment() - comment node below
55

Location Path: Predicates


Predicate is a further test to retain nodes It filters the node set into a new node set Each node is evaluated in turn If predicate is true for a given node, it is kept in the new node set If false it is removed Predicates consist of a well-formed expressions

56

Predicate Expressions

Expression consist of

boolean operators functions numbers, strings comparison operators

Example

child::book[author=Mike] descendent::book[position()=2] descendent::library*attribute::type=isbn+


57

Predicate Expressions
Expressions describes a set of nodes in a documents Path expression may be absolute or relative

Absolute begins from the root node

/library/book/title book/author

Relative begins from the context node

58

Expressions

XPath supports four types of expression

Numerical

div

mod

Equality

=
<

!=
> <= >=

Relational

Boolean

and or

59

Functions

XPath defines a number of useful functions for converting and translating data

Node set functions

count() - returns number of nodes in node set

String functions
concat() - returns the concatenations of all its arguments contains() - returns true if the second of 2 given strings is in the first

60

Functions

Number functions

sum() - returns the total value of a set of numeric values in a node set
not() - returns true if the argument is false, and false if the argument is true

Boolean functions

61

Presenting XML

Presenting XML
XML data can be presented The available options for the same

CSS - Cascading Style Sheets XSL - XML Stylesheet Language

63

Cascading Style Sheet


CSS can be used to present XML document Most of the browsers supports CSS

Eg: MSIE, Firefox, Opera

CSS is used to view XML as if it were HTML A style must be defined for each of the XML element Or the browser displays it in its default format
64

Cascading Style Sheet


All display characteristics of each element must be explicitly defined Elements are displayed in the order they are encountered in the XML document No reordering of elements or other processing is possible Must put a processing instruction at the top of your XML file

Below XML declaration


65

XML Processing Instruction


CSS is written in a separate file Saved with the extension .css Below is an example of processing instruction

<?xml version=1.0 ?> <?xml-stylesheet type=text/css href=greeting.css ?>

66

An Example
<?xml version=1.0 ?> <?xml-stylesheet type=text/css href=greeting.css ?> <greeting> <person>Rosy Dsouza</person> <message>Happy Diwali</message> </greeting>

67

Style Sheet
greeting{ display:block; background-color:pink; } person{ font:20pt verdana; color:blue; } message{ font:24pt georgia; color:red; font-style:itlaic; }
68

XML Style Sheet Language

Transformation

Introduction
XML Style Sheet Language - Transformation A markup language and programming syntax for processing XML Most often used to

Transform XML to HTML to deliver to standard web clients Transform XML from one set of tag to another Transform XML into another syntax/system
70

Introduction
An XML document is a collection of nodes that can be identified, selected and acted upon using an XPath expression XSLT is based on the process of matching templates to nodes of the XML tree An XSLT style sheet is a collection of templates that act against specified nodes in the XML source tree

71

An Example
<?xml version=1.0 ?> <greeting> <person>Sam Mendes</person> <message>Eid Mubarak</message> </greeting>

72

Tree Nodes

/ greeting person message

Root

Root element Child elements

73

Templates
A separate template is defined for each element Syntax for defining template

<xsl:template match=element> <p><xsl:value-of select=expression/></p> </xsl:template>

74

Calling Templates
A template can call other templates Tree Processing

<xsl:apply-templates/>

Call templates of all the children nodes


Call all title templates of context node Call the named template, regardless of the source tree

<xsl:apply-templates select=title/>

<xsl:call-template name=title/>

75

Pull and Push Processing

Push Processing of templates

In this the source document controls the order of processing

Eg: <xsl:apply-templates/>

Pull Processing of templates

It address particular element in the source tree regardless of the position

Eg: <xsl:apply-templates select=//title/>

76

Selecting Element and Attributes

To select the content of a particular element or attribute element is


<xsl:value-of select=xpath expression/> Takes xpath expression to search node

For selecting an attribute, attribute name precedes with an @ symbol

77

Decision Structure

For processing data based on a specified criteria XSL provides

<xsl:if test=condition> <xsl:choose>


<xsl:when test=condition> <xsl:otherwise>

78

Looping & Sorting


XSLT provides support for looping Looping too is using xpath expression for selecting a node to perform operation on each node For looping XSL provide

<xsl:for-each select=expression>

For sorting

<xsl:sort />

79

XML Query Language

XQuery

Why XQuery?
One of the great strengths of XML is its flexibility in representing many different kinds of information from diverse sources As increasing amounts of information are stored, exchanged, & presented using XML The ability to intelligently query XML data sources becomes increasingly important To exploit this flexibility, an XML query language must provide features for retrieving and interpreting information

81

Querying XML Data

XPath

Simple Navigation through the tree Recursive Traversal The SQL of XML

XSLT

XQuery

82

What is XQuery

SQL for Database


SQL
Database

XQuery for XML Document / Database


XQuery
XML Document or Database

83

Introduction
XQuery is a query language for XML data XQuery follows FLWR (flower)

FOR LET WHERE RETURN

84

FLWOR

FOR and/or LET

Appear one or more times Optional Always present

WHERE

RETURN

85

An Example
<?xml version=1.0 ?> <library> <book isbn=37-4239> <title>Java</title> <author>Frank</author> <price>450</price> </book> <book isbn=43-4749> <title>XML</title> <author>Mark</author> <price>390</price> </book> </library>
86

FOR

FOR variable IN expression


FOR $x IN doc(library.xml)/library/book RETURN <result>{$x}</result>

Result: <result> <title>Java</title> </result> <result> <title>XML</title> </result>


87

LET

LET variable = expression


LET $x = doc(library.xml)/library/book RETURN <result>{$x}</result> Result: <result> <book>...</book> <book>...</book> </result>

88

Query Example

Find all book titles priced more than 300


FOR $x IN doc(library.xml)/library/book WHERE $x/price > 400 RETURN <result>{$x/title}</result> Output: <result> <title>Java</title> </result> <result> <title>XML</title> </result>
89

Query Example

Find all book titles priced more than 300


LET $x := doc(library.xml)/library/book [price > 300] RETURN <result>{$x/title}</result> Output: <result> <title>Java</title> <title>XML</title> </result>
90

Thank You

Zubair Shaikh
[email protected]

You might also like