Querying XML: XQuery and XSLT Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 21, 2004 Some slide content courtesy.

Slides:



Advertisements
Similar presentations
Inside an XSLT Processor Michael Kay, ICL 19 May 2000.
Advertisements

Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
XML Data Management 8. XQuery Werner Nutt. Requirements for an XML Query Language David Maier, W3C XML Query Requirements: Closedness: output must be.
Querying on the Web: XQuery, RDQL, SparQL Semantic Web - Spring 2006 Computer Engineering Department Sharif University of Technology.
XML May 3 rd, XQuery Based on Quilt (which is based on XML-QL) Check out the W3C web site for the latest. XML Query data model –Ordered !
From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 22, 2005.
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.
XSL XSLT and XPath 11-Apr-17.
Query Execution, Concluded Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 18, 2003 Some slide content may.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Paper by: A. Balmin, T. Eliaz, J. Hornibrook, L. Lim, G. M. Lohman, D. Simmen, M. Wang, C. Zhang Slides and Presentation By: Justin Weaver.
Querying XML Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 6, 2003 Some slide content courtesy of Susan Davidson.
Querying XML (cont.). Comments on XPath? What’s good about it? What can’t it do that you want it to do? How does it compare, say, to SQL?
Introduction to Structured Query Language (SQL)
XML, XPath, and XQuery Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 18, 2005 Some slide content courtesy.
Query Languages Aswin Yedlapalli. XML Query data model Document is viewed as a labeled tree with nodes Successors of node may be : - an ordered sequence.
XSL Unit 6 November 2. XSL –eXtensible Stylesheet Language –Basically a stylesheet for XML documents XSL has three parts: –XSLT –XPath –XSL-FO.
1 COS 425: Database and Information Management Systems XML and information exchange.
XML Querying and Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 1, 2005 Some slide content courtesy.
Views: Alternate Data Representations Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 2, 2004 Some slide content.
Introduction to Structured Query Language (SQL)
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
XML Querying and Views Helena Galhardas DEI IST (slides baseados na disciplina CIS 550 – Database & Information Systems, Univ. Pennsylvania, Zachary Ives)CIS.
Querying XML: XPath, XQuery, and XSLT Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 27, 2005 Some slide content.
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
MC 365 – Software Engineering Presented by: John Ristuccia Shawn Posts Ndi Sampson XSLT Introduction BCi.
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.
XQuery and Hierarchical Naming Zachary G. Ives University of Pennsylvania CIS 455 / 555 – Internet and Web Systems February 7, 2008.
Lecture 21 XML querying. 2 XSL (eXtensible Stylesheet Language) In HTML, default styling is built into browsers as tag set for HTML is predefined and.
IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
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 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
XSLT + Xpath CS High level view We are doing tree editing Changes to node properties Structural rearrangement Several mdels for this kind of task.
Company LOGO OODB and XML Database Management Systems – Fall 2012 Matthew Moccaro.
Implementing Forms and Form Renderers in the Open Source Portfolio David McPherson, Chris Maurer Will Trillich, Janice Smith Materials by Sean Keesler.
XML A web enabled data description language 4/22/2001 By Mark Lawson & Edward Ryan L’Herault.
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
CIS 375—Web App Dev II XSL. 2 XSL Introduction XSL stands for _____________________________. XSL is the language used for manipulating and displaying.
Waqas Anwar Next SlidePrevious Slide. Waqas Anwar Next SlidePrevious Slide XML XML stands for EXtensible Markup Language.
Querying XML – Concluded Introduction to Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 9, 2003 Some.
Database Systems Part VII: XML Querying Software School of Hunan University
XML Schemas, XPath, and XQuery Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 19, 2004 Some slide content.
XML and Database.
XSLT. XSLT stands for Extensible Stylesheet Language Transformations XSLT is used to transform XML documents into other kinds of documents. XSLT can produce.
XML Query: xQuery Reference: Xquery By Priscilla Walmsley, Published by O’Reilly.
Views and XML/Relational Mappings Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 21, 2003.
Session 1 Module 1: Introduction to Data Integrity
XP 1 XSLT II Robin Burke ECT 360. XP 2 Homework #4 Forgot we hadn't covered "choose" becomes extra credit My books.xml didn't include descriptions.
Querying XML, Part II Zachary G. Ives University of Pennsylvania CIS 455 / 555 – Internet and Web Systems February 5, 2008.
 XML derives its strength from a variety of supporting technologies.  Structure and data types: When using XML to exchange data among clients, partners,
XML Schema – XSLT Week 8 Web site:
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.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
XML: Extensible Markup Language
Displaying XML Data with XSLT
Querying and Transforming XML Data
Database Processing with XML
Prepared for Md. Zakir Hossain Lecturer, CSE, DUET Prepared by Miton Chandra Datta
XML, XPath, and XQuery Zachary G. Ives October 22, 2007
Views and XML Views of Relations
Querying XML: XPath, XQuery, and XSLT
XML Querying and Views Zachary G. Ives November 1, 2007
Querying XML: XQuery and XSLT
Presentation transcript:

Querying XML: XQuery and XSLT Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 21, 2004 Some slide content courtesy of Susan Davidson, Dan Suciu, & Raghu Ramakrishnan

2 Reminder  Homework 4 due Monday, 1:30PM  Bring any questions to…  Optional review session 10/26, 1:30PM  (Can post to newsgroup or send instead)  Midterm Thursday 10/28, 1:30PM

3 XQuery A strongly-typed, Turing-complete XML manipulation language  Attempts to do static typechecking against XML Schema  Based on an object model derived from Schema Unlike SQL, fully compositional, highly orthogonal:  Inputs & outputs collections (sequences or bags) of XML nodes  Anywhere a particular type of object may be used, may use the results of a query of the same type  Designed mostly by DB and functional language people Attempts to satisfy the needs of data management and document management  The database-style core is mostly complete (even has support for NULLs in XML!!)  The document keyword querying features are still in the works – shows in the order-preserving default model

4 XQuery’s Basic Form  Has an analogous form to SQL’s SELECT..FROM..WHERE..GROUP BY..ORDER BY  The model: bind nodes (or node sets) to variables; operate over each legal combination of bindings; produce a set of nodes  “FLWOR” statement: for {iterators that bind variables} let {collections} where {conditions} order by {order-conditions}(the handout uses old “SORTBY”) return {output constructor}

5 “Iterations” in XQuery A series of (possibly nested) FOR statements assigning the results of XPaths to variables for $root in document(“ for $sub in $root/rootElement, $sub2 in $sub/subElement, …  Something like a template that pattern-matches, produces a “binding tuple”  For each of these, we evaluate the WHERE and possibly output the RETURN template  document() or doc() function specifies an input file as a URI  Old version was “document”; now “doc” but it depends on your XQuery implementation

6 Two XQuery Examples { for $p in document(“dblp.xml”)/dblp/proceedings, $yr in $p/yr where $yr = “1999” return {$p} } for $i in document(“dblp.xml”)/dblp/inproceedings[author/text() = “John Smith”] return { $i/title/text() } { } { $i/crossref }

7 Nesting in XQuery Nesting XML trees is perhaps the most common operation In XQuery, it’s easy – put a subquery in the return clause where you want things to repeat! for $u in document(“dblp.xml”)/universities where $u/country = “USA” return { $u/title } { for $mt in $u/../mastersthesis where $mt/year/text() = “1999” and ____________ return $mt/title }

8 Collections & Aggregation in XQuery In XQuery, many operations return collections  XPaths, sub-XQueries, functions over these, …  The let clause assigns the results to a variable Aggregation simply applies a function over a collection, where the function returns a value (very elegant!) let $allpapers := document(“dblp.xml”)/dblp/article return { fn:count(fn:distinct-values($allpapers/authors)) } {for $paper in doc(“dblp.xml”)/dblp/article let $pauth := $paper/author return {$paper/title} { fn:count($pauth) } }

9 Collections, Ctd. Unlike in SQL, we can compose aggregations and create new collections from old: { let $avgItemsSold := fn:avg( for $order in document(“my.xml”)/orders/order let $totalSold = fn:sum($order/item/quantity) return $totalSold) return $avgItemsSold }

10 Distinct-ness In XQuery, DISTINCT-ness happens as a function over a collection  But since we have nodes, we can do duplicate removal according to value or node  Can do fn:distinct-values(collection) to remove duplicate values, or fn:distinct-nodes(collection) to remove duplicate nodes for $years in fn:distinct-values(doc(“dblp.xml”)//year/text() return $years

11 Sorting in XQuery  SQL actually allows you to sort its output, with a special ORDER BY clause (which we haven’t discussed, but which specifies a sort key list)  XQuery borrows this idea  In XQuery, what we order is the sequence of “result tuples” output by the return clause: for $x in document(“dblp.xml”)/proceedings order by $x/title/text() return $x

12 What If Order Doesn’t Matter? By default:  SQL is unordered  XQuery is ordered everywhere!  But unordered queries are much faster to answer XQuery has a way of telling the query engine to avoid preserving order:  unordered { for $x in (mypath) … }

13 Querying & Defining Metadata – Can’t Do This in SQL Can get a node’s name by querying node-name(): for $x in document(“dblp.xml”)/dblp/* return node-name($x) Can construct elements and attributes using computed names: for $x in document(“dblp.xml”)/dblp/*, $year in $x/year, $title in $x/title/text(), element node-name($x) { attribute {“year-” + $year} { $title } }

14 XQuery Summary Very flexible and powerful language for XML  Clean and orthogonal: can always replace a collection with an expression that creates collections  DB and document-oriented (we hope)  The core is relatively clean and easy to understand Turing Complete – we’ll talk more about XQuery functions soon

15 XSL(T): The Bridge Back to HTML  XSL (XML Stylesheet Language) is actually divided into two parts:  XSL:FO: formatting for XML  XSLT: a special transformation language  We’ll leave XSL:FO for you to read off if you’re interestedwww.w3.org  XSLT is actually able to convert from XML  HTML, which is how many people do their formatting today  Products like Apache Cocoon generally translate XML  HTML on the server side

16 A Different Style of Language  XSLT is based on a series of templates that match different parts of an XML document  There’s a policy for what rule or template is applied if more than one matches (it’s not what you’d think!)  XSLT templates can invoke other templates  XSLT templates can be nonterminating (beware!)  XSLT templates are based on XPath “match”es, and we can also apply other templates (potentially to “select”ed XPaths)  Within each template, we describe what should be output  (Matches to text default to outputting it)

17 An XSLT Stylesheet This is DBLP …

18 Results of XSLT Stylesheet Paper1 Smith Chakrabarti Gray Paper2 This Is DBLP Paper1 Smith Paper2 Chakrabarti Gray

19 What XSLT Can and Can’t Do  XSLT is great at converting XML to other formats  XML  diagrams in SVG; HTML; LaTeX  …  XSLT doesn’t do joins (well), it only works on one XML file at a time, and it’s limited in certain respects  It’s not a query language, really  … But it’s a very good formatting language  Most web browsers (post Netscape 4.7x) support XSLT and XSL formatting objects  But most real implementations use XSLT with something like Apache Cocoon  You may want to use XSL/XSLT for your projects – see for the spec

20 Querying XML We’ve seen three XML manipulation formalisms today:  XPath: the basic language for “projecting and selecting” (evaluating path expressions and predicates) over XML  XQuery: a statically typed, Turing-complete XML processing language  XSLT: a template-based language for transforming XML documents  Each is extremely useful for certain applications!

21 Views in SQL and XQuery  A view is a named query  We use the name of the view to invoke the query (treating it as if it were the relation it returns) SQL: CREATE VIEW V(A,B,C) AS SELECT A,B,C FROM R WHERE R.A = “123” XQuery: declare function V() as element(content)* { for $r in doc(“R”)/root/tree, $a in $r/a, $b in $r/b, $c in $r/c where $a = “123” return {$a, $b, $c} } SELECT * FROM V, R WHERE V.B = 5 AND V.C = R.C for $v in V()/content, $r in doc(“r”)/root/tree where $v/b = $r/b return $v Using the views:

22 What’s Useful about Views Providing security/access control  We can assign users permissions on different views  Can select or project so we only reveal what we want! Can be used as relations in other queries  Allows the user to query things that make more sense Describe transformations from one schema (the base relations) to another (the output of the view)  The basis of converting from XML to relations or vice versa  This will be incredibly useful in data integration, discussed soon… Allow us to define recursive queries

23 Materialized vs. Virtual Views  A virtual view is a named query that is actually re-computed every time – it is merged with the referencing query CREATE VIEW V(A,B,C) AS SELECT A,B,C FROM R WHERE R.A = “123”  A materialized view is one that is computed once and its results are stored as a table  Think of this as a cached answer  These are incredibly useful!  Techniques exist for using materialized views to answer other queries  Materialized views are the basis of relating tables in different schemas SELECT * FROM V, R WHERE V.B = 5 AND V.C = R.C

24 Views Should Stay Fresh  Views (sometimes called intensional relations) behave, from the perspective of a query language, exactly like base relations (extensional relations)  But there’s an association that should be maintained:  If tuples change in the base relation, they should change in the view (whether it’s materialized or not)  If tuples change in the view, that should reflect in the base relation(s)

25 View Maintenance and the View Update Problem  There exist algorithms to incrementally recompute a materialized view when the base relations change  We can try to propagate view changes to the base relations  However, there are lots of views that aren’t easily updatable:  We can ensure views are updatable by enforcing certain constraints (e.g., no aggregation), but this limits the kinds of views we can have! AB BC R S ABC R⋈SR⋈S delete?

26 Next Time (Post-Midterm)  Can we have views in XML over tables in relations?  … Or vice versa?  What other things can we use views for