Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: C2.

Slides:



Advertisements
Similar presentations
XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN Outline Brief Overview Brief Overview What is XML? What is XML? Well Formed XML Well Formed XML Tag Name.
Advertisements

Chungnam National University DataBase System Lab
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: XPath, NameSpaces.
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: XQuery.
What is XML? a meta language that allows you to create and format your own document markups a method for putting structured data into a text file; these.
XML: Extensible Markup Language
Dr. Alexandra I. Cristea XPath and Namespaces.
Bottom-up Evaluation of XPath Queries Stephanie H. Li Zhiping Zou.
XML, XML Schema, Xpath and XQuery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
SPECIAL TOPIC XML. Introducing XML XML (eXtensible Markup Language) ◦A language used to create structured documents XML vs HTML ◦XML is designed to transport.
An Introduction to XML Based on the W3C XML Recommendations.
XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer.
XQUERY. What is XQuery? XQuery is the language for querying XML data The best way to explain XQuery is to say that XQuery is to XML what SQL is to database.
1 XQuery Web and Database Management System. 2 XQuery XQuery is to XML what SQL is to database tables XQuery is designed to query XML data What is XQuery?
Friday, September 4 th, 2009 The Systems Group at ETH Zurich XML and Databases Exercise Session 6 courtesy of Ghislain Fourny/ETH © Department of Computer.
1 COS 425: Database and Information Management Systems XML and information exchange.
Cornell CS 502 More XML XML schema, XPATH, XSLT CS 502 – Carl Lagoze – Cornell University.
XML –Query Languages, Extracting from Relational Databases ADVANCED DATABASES Khawaja Mohiuddin Assistant Professor Department of Computer Sciences Bahria.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
Overview of XPath Author: Dan McCreary Date: October, 2008 Version: 0.2 with TEI Examples M D.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
ECA 228 Internet/Intranet Design I Intro to XSL. ECA 228 Internet/Intranet Design I XSL basics W3C standards for stylesheets – CSS – XSL: Extensible Markup.
CSE3201/CSE4500 XPath. 2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
1/17 ITApplications XML Module Session 7: Introduction to XPath.
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
CSE3201/CSE4500 Information Retrieval Systems
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
WORKING WITH XSLT AND XPATH
1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
Lecture 22 XML querying. 2 Example 31.5 – XQuery FLWOR Expressions ‘=’ operator is a general comparison operator. XQuery also defines value comparison.
XPath Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Processing of structured documents Spring 2003, Part 7 Helena Ahonen-Myka.
XPath. Why XPath? Common syntax, semantics for [XSLT] [XPointer][XSLT] [XPointer] Used to address parts of an XML document Provides basic facilities for.
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
1 Displaying XML Document Web and Database Management System.
August Chapter 6 - XPath & XPointer Learning XML by Erik T. Ray Slides were developed by Jack Davis College of Information Science and Technology.
Database Systems Part VII: XML Querying Software School of Hunan University
XPath Aug ’10 – Dec ‘10. XPath   XML Path Language   Technology that allows to select a part or parts of an XML document to process   XPath was.
Advanced Selenium.  XPath is used to navigate through elements and attributes in a document.  XPath includes over 100 built-in functions.  Functions.
XPath Presented by Kushan Athukorala. 2 Agenda XPath XPath Terminology Selecting Nodes Predicates.
Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa ME (Modular) CSE 2011 Batch.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
Session II Chapter 3 – Chapter 3 – XPath Patterns & Expressions Chapter 4 – XPath Functions Chapter 15 – XPath 2.0http://
IS432 Semi-Structured Data Lecture 4: XPath Dr. Gamal Al-Shorbagy.
CSE3201/CSE4500 XPath. 2 XPath A locator for items in XML document. XPath expression gives direction of navigation.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
1 XPath. 2 Agenda XPath Introduction XPath Nodes XPath Syntax XPath Operators XPath Q&A.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
CITA 330 Section 5 XPath. XSL XSL (Extensible Stylesheet Language) is the standard language for writing stylesheets to transform XML documents among different.
5 Copyright © 2004, Oracle. All rights reserved. Navigating XML Documents by Using XPath.
1 XSL Transformations (XSLT). 2 XSLT XSLT is a language for transforming XML documents into XHTML documents or to other XML documents. XSLT uses XPath.
XML. What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data.
XML: Extensible Markup Language
Unit 4 Representing Web Data: XML
Querying and Transforming XML Data
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Chapter 7 Representing Web Data: XML
More XML XML schema, XPATH, XSLT
Presentation transcript:

Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: C2

Previously we looked at: –XML –XSL –XSLT Next: –XPath –XQuery

XPath

XPath is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in XSLT XPath is a W3C recommendation, thus a Standard (16. November 1999 )

XPath Path Expressions Uses path expressions to select nodes or node-sets in an XML document. –These path expressions look very much like the expressions you see when you work with a traditional computer file system.

XPath Standard Functions over 100 built-in functions. –string values, –numeric values, –date and time comparison, –node and QName manipulation, –sequence manipulation, –Boolean values, –and more.

XPath Terminology Nodes Atomic values Items (atomic values or nodes) Relationships of nodes –Parent –Children –Siblings –Ancestors –Descendants

XPath Nodes 7 kinds of nodes: –element, –attribute, –text, –namespace, –processing-instruction, –comment, and –document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).

Nodes Examples Harry Potter J K. Rowling Document nodeElement node Attribute node

Atomic values Examples* Harry Potter J K. Rowling *nodes with no children or parent

Selecting nodes ExpressionDescription nodenameSelects all child nodes of the 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 Selects attributes

Examples of selecting nodes Path ExpressionResult bookstoreSelects all the child nodes of the bookstore element /bookstoreSelects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! bookstore/bookSelects all book elements that are children of bookstore //bookSelects all book elements no matter where they are in the document bookstore//bookSelects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element all attributes that are named lang

Predicates Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets.

Example predicates Path ExpressionResult /bookstore/book[1]Selects the first book element that is the child of the bookstore element /bookstore/book[last()]Selects the last book element that is the child of the bookstore element /bookstore/book[last()-1]Selects the last but one book element that is the child of the bookstore element /bookstore/book[position()<3]Selects the first two book elements that are children of the bookstore element

Example predicates – cont. Path ExpressionResult all the title elements that have an attribute named lang all the title elements that have an attribute named lang with a value of 'eng' /bookstore/book[price>35.00] /bookstore/book[price>35.00]/title Selects all the book elements of the bookstore element that have a price element with a value greater than Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

Selecting Unknown Nodes WildcardDescription *Matches any element any attribute node node()Matches any node of any kind

Example: selecting several paths Path ExpressionResult //book/title | //book/priceSelects all the title AND price elements of all book elements //title | //price /bookstore/book/title | //price Selects all the title AND price elements in the document Selects all the title elements of the book element of the bookstore element AND all the price elements in the document

Location Path Expression A location path can be absolute or relative. An absolute location path: /step/step/... A relative location path: step/step/... Location step: axisname::nodetest[predicate]

XPath Axes self childparent ancestordescendant ancestor-or-selfdescendant-or- self preceding-siblingfollowing-sibling precedingfollowing attribute namespace

AxisNameResult ancestorSelects all ancestors (parent, grandparent, etc.) of the current node ancestor-or-selfSelects all ancestors (parent, grandparent, etc.) of the current node and the current node itself attributeSelects all attributes of the current node childSelects all children of the current node descendantSelects all descendants (children, grandchildren, etc.) of the current node descendant-or- self Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself followingSelects everything in the document after the closing tag of the current node following-siblingSelects all siblings after the current node namespaceSelects all namespace nodes of the current node parentSelects the parent of the current node precedingSelects everything in the document that is before the start tag of the current node preceding- sibling Selects all siblings before the current node selfSelects the current node

axisname::nodetest[predicate] //DDD/parent::*

axisname::nodetest[predicate] //BBB/child::* Note: /AAA is equivalent to /child::AAA

More examples l/examples.htmlhttp:// l/examples.html –Check basics, //, *, predicates, attributes, functions (new ones: count, name, normalize-space, starts- with, contains, string-length, floor, ceiling), axes, operators (mod) –Note: The ancestor, descendant, following, preceding and self axes partition a document (ignoring attribute and namespace nodes): they do not overlap and together they contain all the nodes in the document. (see example)example

XPath Conclusion We have learned: –XPath definition –Path expressions –Standard functions –Terminology –Predicates –Location paths –Axes –Some operators

Before we go on, one more thing about XML: XML Namespaces

Naming ambiguity

The Idea to Solve it Assign a URI (~ URL) to every sub- language: –E.g., for XHTML 1.0: Qualify element names with URIs: –{ Web Naming and Addressing Overview (URIs, URLs,...)

The actual solution Namespace declarations bind URIs to prefixes: Default namespace (no prefix) declared with: xmlns=… Lexical Scope Attribute names can also be prefixed

Applying namespaces

Next we look at how to query XML This can be done, to some extent, as we have seen, within XSLT, but the main language developed for this purpose is …

XQuery

What is XQuery? XQuery is the language for querying XML data XQuery for XML is like SQL for databases XQuery is built on XPath expressions XQuery is defined by the W3C XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.) XQuery is a W3C recommendation (Jan 2007) thus a standard

XQuery and XPath XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators.

XQuery - Examples of Use Extract information to use in a Web Service Generate summary reports Transform XML data to XHTML Search Web documents for relevant information

Usage Scenario: Document-Oriented Queries could be used –To retrieve parts of documents –To provide dynamic indexes –To perform context-sensitive searching –To generate new documents as combinations of existing ones

Usage Scenario: Programming Queries could be used to automatically generate documentation

Usage Scenario: Hybrid Queries could be used to data mine hybrid data, such as patient records

XQuery compared to XPath XQuery 1.0 is a strict superset of XPath 2.0 XPath 2.0 expression is directly an XQuery 1.0 expression (a query) The extra expressive power is the ability to: –Join information from different sources and –Generate new XML fragments

Relationship to XSLT XQuery, XSLT: both domain-specific languages for combining and transforming data from multiple sources different in design - historical reasons –XQuery: designed from scratch –XSLT: intellectual descendant of CSS technically, they may emulate each other

XQuery query Prolog –Like XPath, XQuery expressions are evaluated relatively to a context –explicitly provided by a prolog (header) ~ header with definitions Body –The actual query

XQuery Prolog (i.e., header(s)) Settings define various parameters for the XQuery processor language, such as: xquery version 1.0; module " default element namespace= " declare namespace xs= " import module " functions" at "logo.xq" define function addLogo($root as node()) as node()* { } (: etc :)

XQuery capabilities Generate Join Select

Generate: constructors XQuery expressions may compute new XML nodes Expressions may denote: –element, character data, comment and processing instruction nodes node is created with a unique node identity Constructors may be either –direct or –computed

Direct constructors in XQuery my fragment Evaluates to the given XML fragment

Explicit, computed constructors

Variable bindings {$name} {$job} {$deptno} {$SGMLspecialist }

How to Select Nodes with XQuery? Functions –XQuery uses functions to extract data from XML documents. (X)Path Expressions –XQuery uses path expressions to navigate through elements in an XML document. Predicates –XQuery uses predicates to limit the extracted data from XML documents.

Functions doc() –function to open a file Example: –doc("books.xml") Note: A call to a function can appear where an expression may appear.

Path Expressions Example: select all the title elements in the "books.xml" file: doc("books.xml")/bookstore/book/title

Predicates Example: 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]

At a glance: function, path, predicate

FLWOR For, Let, Where, Order by, Return = main engine ~ SQL syntax (SFHW) ~ programs and function calls

FLWOR by comparison with Path expressions select all the title elements under the book elements that are under the bookstore element that have a price element with a value that is higher than 30. Path expression : doc("books.xml")/bookstore/book[price>30]/title FLWOR expression : for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title

Sorting in FLWOR for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title

Present the Result In an HTML List { for $x in doc("books.xml")/bookstore/book/title order by $x return {$x} }

Result HTML List Everyday Italian Harry Potter Learning XML XQuery Kick Start

Eliminate element (here: title) { for $x in doc("books.xml")/bookstore/book/title order by $x return data($x) (: also text() :) }

New result HTML List Everyday Italian Harry Potter Learning XML XQuery Kick Start

Another FLWOR Expression

The Difference between for and let

FLWOR Basic Building Blocks

General rules for and let may be used many times in any order only one where is allowed many different sorting criteria can be specified (descending, ascending, etc.)

Joining documents for $p IN doc(" for $n IN doc("neighbors.xml")//neighbor[ssn = $p/ssn] return { $p/ssn } { $n/name } { $p/income }

Conditionals FOR $b IN doc(bib.xml)/book RETURN {$b/title} {IF count($b/author) and others }

Functions DEFINE FUNCTION depth($e) RETURNS xsd:integer { IF (empty($e/*) THEN 1 ELSE max(FOR $c in $e/* RETURN depth($c)) ) +1 } FOR $b in doc(bib.xml)/book RETURN depth($b)

Existential and Universal Quantifiers FOR $b in doc(bib.xml)/book WHERE SOME $author IN $b/author SATISFIES $author/text() = Ullman RETURN $b FOR $b in doc(bib.xml)/book WHERE EVERY $author IN $b/author SATISFIES $author/text() = Ullman RETURN $b Return books where all authors are Ullman Return books where at least one author is Ullman

XQuery on Distributed Sources

XQuery Syntax Declarative, functional language ~ SQL Nested expressions Case sensitive White spaces: –Tabs, space, CR, LF –Ignored between language constructs –Significant in quoted strings No special EOL character

Keywords and names Keywords and operators –Case-sensitive, generally lower case –May have several meanings depending on the context E.g. * or in –No reserved words All names must be valid XML names –For variables, functions, elements, attributes –Can be associated with a namespace

Comments

Comparisons Value comparisons Eq, ne, lt, le, gt, ge Used to compare individual values Each operand must be a single atomic value (or a node containing a single atomic value) General comparisons =, !=,, >= Can be used with sequences of multiple items

Example

Query Prolog

XQuery gives you a choice: Path Expressions: –If you just want to copy certain elements and attributes as is FLWOR Expressions: –Allow sorting –Allow adding elements/attributes –Verbose, but can be clearer

XQuery tools XStylus Studio oad.html (free trial version) oad.html –See also short its XQuery intro at:

XML and programming XSLT, XPath and XQuery provide tools for specialized tasks. But many applications are not covered: –domain-specific tools for concrete XML languages –general tools that nobody has thought of yet

XML in general-purpose programming languages parse XML documents into XML trees navigate through XML trees construct XML trees output XML trees as XML documents DOM and SAX are corresponding APIs that are language independent and supported by numerous languages. JDOM is an API that is tailored to Java.DOMSAXJDOM

XQuery Conclusion We have learned: –XQuery definition –Usage scenarios –Comparison w. XSLT and XPath –Capabilities –Functions, path expressions and predicates –FLWOR –Extensions for generic programming with XML