submitted to: ms. poonam saini, asst. prof., nitttr submitted by: rohit handa 111430 me (modular)...

30
Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE

Upload: roderick-bates

Post on 17-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Submitted To:Ms. Poonam Saini,Asst. Prof., NITTTR

Submitted By:Rohit Handa111430ME (Modular) CSE2011 Batch

Page 2: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

• A "Well Formed" XML document has correct XML syntax.

• No syntax, spelling, punctuation, grammar errors, etc. in its markup. These kinds of errors can cause XML document difficult to parse.

• An XML Parser is software that reads XML documents and interprets the code according to the XML standard. A parser is needed to perform actions on XML.

Page 3: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

XML Syntax– XML documents must have a root element

<root> <child> <subchild>.....</subchild> </child> </root>

– XML elements must have a closing tag<p>This is a paragraph.</p>

– XML tags are case sensitive<Message>This is incorrect</message> <message>This is correct</message>

– XML elements must be properly nested<b><i>This text is bold and italic</i></b>

– XML attribute values must be quoted<note date="12/11/2007"> <to>Tove</to> <from>Jani</from> </note>

Page 4: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Valid XML• An XML document is valid, if the element structure and

markup of the XML document matches a defined standard of relationships, in addition to having well formed markup.

• One standard used to validate XML is a DTD, or Document Type Declaration,

<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE note SYSTEM "Note.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading>

<body>Don't forget me this weekend!</body> </note>

Page 5: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

DTD (Document Type Definition)• The purpose of a DTD is to define the structure of an XML document. • The purpose of a DTD is to define the legal building blocks of an XML

document.• A DTD defines the document structure with a list of legal elements and

attributes.

<!DOCTYPE note [ <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>

Page 6: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Rules for Creating DTDs • When creating a DTD, all the elements and attributes

need to be defined to be used in the XML documents.

• Some syntax to remember when creating DTDs are:

Page 7: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

DTD (Contd.)

• Elements are declared in the following manner: <!ELEMENT elementName ( elementParts ) >

• Attributes are declared like this: <!ATTLIST elementName attributeName attributeType attributeDefault >

Page 8: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch
Page 9: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Internal DTD• If the DTD is declared inside the XML file, it should be

wrapped in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element [element-declarations]>

<?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>

<note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend</body> </note>

!DOCTYPE note defines that the root element of this document

is note

!ELEMENT note defines that the note element contains 4 elements “to, from, heading,

body”

PCDATA means parsed character data.

The text will be examined by the parser for entities and

markup.

Page 10: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

External DTD• If the DTD is declared in an external file, it should be wrapped in a DOCTYPE

definition with the following syntax:

<!DOCTYPE root-element SYSTEM "filename"> • <?xml version="1.0"?>

<!DOCTYPE note SYSTEM "note.dtd"><note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>

And this is the file "note.dtd" which contains the DTD:• <!ELEMENT note (to,from,heading,body)>

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

Page 11: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch
Page 12: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

XPATH

• XPath is a syntax for defining parts of an XML document

• XPath uses path expressions to navigate in XML documents

• XPath is a W3C recommendation

Page 13: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

XML Nodes• XML

documents are treated as trees of nodes. The topmost element of the tree is called the root element.

Bookstore

Title Author

Giada De Laurentiis

Year

2005

Price

30.00

Book[2] Book[n]…….

… …

Book[1]

EverydayItalian

Page 14: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Select all the titles

• Everyday Italian

• Harry Potter

• XQuery Kick Start

• Learning XML

/bookstore/book/title

Bookstore

Book[2]

Title

Everyday Italian

Title

Harry Potter

Title

XQUERY Quick Start

Title

Learning XML

Book[1] Book[3] Book[4]

… … … …

Page 15: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Select the title of the first book

• Everyday Italian

/bookstore/book[1]/titleBookstore

Book[index]

Title

Everyday Italian

Title

Harry Potter

Title

XQUERY Quick Start

Title

Learning XML

Page 16: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Select all the prices

• 30.00• 29.99• 49.99• 39.95

/bookstore/book/priceBookstore

Book[index]

Price

30.00

Price

29.99

Price

49.99

Price

39.95

Page 17: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Select price nodes with price>35

• 49.99• 39.95

/bookstore/book[price>35]/priceBookstore

Book[index]

Price

30.00

Price

29.99

Price

49.99

Price

39.95

Page 18: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Display books with price>35

• XQuery Kick Start• Learning XML

/bookstore/book[price>35]/titleBookstore

Books

Price

30.00

Price

29.99

Price

49.99

Price

39.95

Page 19: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Display author list with price<30

• J.K. Rowling

/bookstore/book[price<30]/authorBookstore

Book[index]

Price

30.00

Price

29.99

Price

49.99

Price

39.95

Page 20: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

XPath Operators• Operator Description Example Return value• | Computes two node-sets //book | //cd Returns a node-set with all book

and cd elements• + Addition 6 + 4 10• - Subtraction 6 - 4 2• * Multiplication 6 * 4 24• div Division 8 div 4 2• = Equal price=9.80 true if price is 9.8

false if price is 9.90• != Not equal price!=9.80 true if price is 9.90

false if price is 9.80• < Less than price<9.80 true if price is 9.00

false if price is 9.80• <= Less than or equal to price<=9.80 true if price is 9.00

false if price is 9.90• > Greater than price>9.80 true if price is 9.90

false if price is 9.80• >= Greater than or equal to price>=9.80 true if price is 9.90

false if price is 9.70• or or price=9.80 or price=9.70 true if price is 9.80

false if price is 9.50• and and price>9.00 and price<9.90 true if price is 9.80

false if price is 8.50• mod Modulus (division remainder) 5 mod 2 1

Page 21: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

XML Selection

Selecting Nodes• XPath uses path expressions to select nodes in an XML

document. The node is selected by following a path or steps.

nodename Selects all child nodes of the named node/ Selects from the root node// Selects nodes in the document from the

current node that match the selection no matter where they are

. Selects the current node

.. Selects the parent of the current node

Page 22: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Advantages• XPath expressions can be automatically generated by the

XPath expression builder. • Uses a familiar syntax for expressing locations in an XML

document hierarchy. • Good document node selection capability across multiple

documents. • Contains easily used functions

Disadvantages• Limited set of built in functions. • Lacks programmability. • Restricted to manipulating documents at relatively large

levels of granularity.

Page 23: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch
Page 24: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

XML Query

• XQuery was designed to query XML data.• XQuery is the language for querying XML data• XQuery for XML is like SQL for databases• XQuery is built on XPath expressions• XQuery is supported by all major databases• XQuery is a W3C Recommendation

Page 25: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

• The doc() function is used to open the xml file doc("books.xml")

• XQuery uses path expressions to navigate through elements in an XML document.

• XQuery uses predicates to limit the extracted data from XML documents.

XQUERY

Page 26: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

How to Select Nodes From "books.xml"?

doc("books.xml")/bookstore/book/title

Page 27: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

Select all the book elements under the bookstore element that have a price element with a value that is

less than 30.

doc("books.xml")/bookstore/book[price<30]

Page 28: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

How to Select Nodes From "books.xml"?

• The for clause selects all book elements under the bookstore element into a variable called $x.

• The where clause selects only book elements with a price element with a value greater than 30.

• The order by clause defines the sort-order. Will be sort by the title element.

• The return clause specifies what should be returned. Here it returns the title elements.

for $x in doc("books.xml")/bookstore/bookwhere $x/price>30order by $x/titlereturn $x/title

Result of the Query

<title lang="en">Learning XML</title><title lang="en">XQuery Kick Start</title>

Page 29: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch

SummaryXML & Database

XML view Database viewXML Data

XPath Referring to a field, such asStudent.id

XQuery SQL

PropertiesSemi structured Structured

Exchange & Shairing Storage

Self describing RDBMS enforced constratints

Page 30: Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 111430 ME (Modular) CSE 2011 Batch