Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa 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.
XML Syntax – XML documents must have a root element..... – XML elements must have a closing tag This is a paragraph. – XML tags are case sensitive This is incorrect This is correct – XML elements must be properly nested This text is bold and italic – XML attribute values must be quoted Tove Jani
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, Tove Jani Reminder Don't forget me this weekend!
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 [ ]>
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:
DTD (Contd.) Elements are declared in the following manner: Attributes are declared like this:
Internal DTD If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition with the following syntax: <!DOCTYPE note [ ]> Tove Jani Reminder Don't forget me this weekend !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.
External DTD If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition with the following syntax: Tove Jani Reminder Don't forget me this weekend! And this is the file "note.dtd" which contains the DTD:
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
XML Nodes XML documents are treated as trees of nodes. The topmost element of the tree is called the root element. Bookstore TitleAuthor Giada De Laurentiis Year 2005 Price Book[2]Book[n] ……. … … Book[1] Everyday Italian
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] …………
Select the title of the first book Everyday Italian /bookstore/book[1]/title Bookstore Book[index] Title Everyday Italian Title Harry Potter Title XQUERY Quick Start Title Learning XML
Select all the prices /bookstore/book/price Bookstore Book[index] Price Price Price Price 39.95
Select price nodes with price> /bookstore/book[price>35]/price Bookstore Book[index] Price Price Price Price 39.95
Display books with price>35 XQuery Kick Start Learning XML /bookstore/book[price>35]/title Bookstore Books Price Price Price Price 39.95
Display author list with price<30 J.K. Rowling /bookstore/book[price<30]/author Bookstore Book[index] Price Price Price Price 39.95
XPath Operators OperatorDescriptionExampleReturn value | Computes two node-sets //book | //cd Returns a node-set with all book and cd elements + Addition Subtraction * Multiplication6 * 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
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. nodenameSelects 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
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.
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
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
How to Select Nodes From "books.xml"? doc("books.xml")/bookstore/book/title
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]
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/book where $x/price>30 order by $x/title return $x/title Result of the Query Learning XML XQuery Kick Start
Summary XML & Database