Introduction to XQuery and XML Databases

Similar documents
XML: Extensible Markup Language

Pre-Discussion. XQuery: An XML Query Language. Outline. 1. The story, in brief is. Other query languages. XML vs. Relational Data

XML databases. Jan Chomicki. University at Buffalo. Jan Chomicki (University at Buffalo) XML databases 1 / 9

Introduction to Programming Using Java (98-388)

Part VII. Querying XML The XQuery Data Model. Marc H. Scholl (DBIS, Uni KN) XML and Databases Winter 2005/06 153

The XQuery Data Model

Oracle Database 11g: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Advanced Database Technologies XQuery

Table of Contents Chapter 1 - Introduction Chapter 2 - Designing XML Data and Applications Chapter 3 - Designing and Managing XML Storage Objects

Progress Report on XQuery

Big Data 12. Querying

Big Data 10. Querying

Chapter 13 XML: Extensible Markup Language

BEAWebLogic. Integration. Transforming Data Using XQuery Mapper

exist: An Open Source Native XML database

DBS2: Exkursus XQuery and XML-Databases. Jan Sievers Jens Hündling Lars Trieloff

TDDD43. Theme 1.2: XML query languages. Fang Wei- Kleiner h?p:// TDDD43

Semi-structured Data. 8 - XPath

Working with Mediator Framework

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Index-Driven XQuery Processing in the exist XML Database

XML Technologies. Doc. RNDr. Irena Holubova, Ph.D. Web pages:

XQuery. Leonidas Fegaras University of Texas at Arlington. Web Databases and XML L7: XQuery 1

Course: The XPath Language

XPath. by Klaus Lüthje Lauri Pitkänen

Outline of this part (I) Part IV. Querying XML Documents. Querying XML Documents. Outline of this part (II)

XPath. Asst. Prof. Dr. Kanda Runapongsa Saikaew Dept. of Computer Engineering Khon Kaen University

H2 Spring B. We can abstract out the interactions and policy points from DoDAF operational views

XPath Expression Syntax

CSC Web Programming. Introduction to SQL

Variable Scope The Main() Function Struct Functions Overloading Functions Using Delegates Chapter 7: Debugging and Error Handling Debugging in Visual

- if you look too hard it isn't there

Oracle Syllabus Course code-r10605 SQL

Navigation. 3.1 Introduction. 3.2 Paths

1) Introduction to SQL

XML Query Languages. Yanlei Diao UMass Amherst April 22, Slide content courtesy of Ramakrishnan & Gehrke, Donald Kossmann, and Gerome Miklau

COPYRIGHTED MATERIAL. Contents. Part I: Introduction 1. Chapter 1: What Is XML? 3. Chapter 2: Well-Formed XML 23. Acknowledgments

Introduction to Computer Science and Business

Introduction to XPath

XML. Semi-structured data (SSD) SSD Graphs. SSD Examples. Schemas for SSD. More flexible data model than the relational model.

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 7 XML

Evaluating XPath Queries

Informatics 1: Data & Analysis

Course: The XPath Language

Extending SystemVerilog Data Types to Nets

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 27-1

Querying purexml Part 1 The Basics

Burrows & Langford Appendix D page 1 Learning Programming Using VISUAL BASIC.NET

Introduction p. 1 An XML Primer p. 5 History of XML p. 6 Benefits of XML p. 11 Components of XML p. 12 BNF Grammar p. 14 Prolog p. 15 Elements p.

Govt. of Karnataka, Department of Technical Education Diploma in Computer Science & Engineering. Fifth Semester. Subject: Web Programming

XML and information exchange. XML extensible Markup Language XML

Seleniet XPATH Locator QuickRef

Introduction to XQuery. Overview. Basic Principles. .. Fall 2007 CSC 560: Management of XML Data Alexander Dekhtyar..

XML Data Management. 6. XPath 1.0 Principles. Werner Nutt

7.1 Introduction. extensible Markup Language Developed from SGML A meta-markup language Deficiencies of HTML and SGML

Outline. Approximation: Theory and Algorithms. Ordered Labeled Trees in a Relational Database (II/II) Nikolaus Augsten. Unit 5 March 30, 2009

Navigating Input Documents Using Paths4

516. XSLT. Prerequisites. Version 1.2

2006 Martin v. Löwis. Data-centric XML. XPath

Database Management Systems,

XPath and XQuery. Introduction to Databases CompSci 316 Fall 2018

M359 Block5 - Lecture12 Eng/ Waleed Omar

Extending CMIS Standard for XML Databases

SPARQL QUERY LANGUAGE WEB:

Microsoft MB Microsoft CRM Extending MS CRM 1.2 with.net.

Foreword... v Introduction... vi. 1 Scope Normative references Terms and definitions Extensible Datatypes schema overview...

Querying Data with Transact SQL

Querying Microsoft SQL Server (461)

XPath. Mario Alviano A.Y. 2017/2018. University of Calabria, Italy 1 / 21

CSI33 Data Structures

CSE 431S Type Checking. Washington University Spring 2013

Delivery Options: Attend face-to-face in the classroom or via remote-live attendance.

Event Stores (I) [Source: DB-Engines.com, accessed on August 28, 2016]

Teiid Designer User Guide 7.5.0

Simplified RDF Syntax for Power System Model Exchange

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

CHAPTER 1: INTRODUCING C# 3

Deccansoft softwareservices-microsoft Silver Learing Partner. SQL Server Syllabus

Teiid Designer User Guide 7.7.0

MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

XQ: An XML Query Language Language Reference Manual

Table of Contents. Preface... xvii

XML & Databases. Tutorial. 3. XPath Queries. Universität Konstanz. Database & Information Systems Group Prof. Marc H. Scholl

Delivery Options: Attend face-to-face in the classroom or remote-live attendance.

One of the main selling points of a database engine is the ability to make declarative queries---like SQL---that specify what should be done while

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

EMERGING TECHNOLOGIES

FOUNDATIONS OF DATABASES AND QUERY LANGUAGES

5. Single-row function

Navigating an XML Document

Sql Server Syllabus. Overview

Big Data Exercises. Fall 2016 Week 9 ETH Zurich

Pro LINQ. Language Integrated Query in C# Apress. Adam Freeman and Joseph C. Rattz, Jr.

Comp 336/436 - Markup Languages. Fall Semester Week 9. Dr Nick Hayward

Data Exchange. Hyper-Text Markup Language. Contents: HTML Sample. HTML Motivation. Cascading Style Sheets (CSS) Problems w/html

Oracle Database: SQL and PL/SQL Fundamentals

Transcription:

Introduction to XQuery and XML Databases TEI@Oxford July 2009

XQuery While XSLT is good for transforming XML to other formats (XML, HTML, PDF, Text, etc.) sometimes you may wish to query a large database of XML documents and extract only matching records. In cases such as this, XQuery might be more appropriate.

XML in Relational Databases vs Native XML Databases XML-enabled Relational Databases: All XML structures are mapped onto to a traditional database form of fields in tables, but it is able to accept XML as input and render XML as output. The database itself does the conversion rather than relying on some form of middleware. Native XML Databases: XML documents are used as the fundamental unit or storage and the internal model uses this XML. This does not mean that the documents are stored as text files, but may be stored as an indexed relationships of nodes.

Reasons for XML Databases Leverage advantages of XML Easy interoperability with other systems Maintain arbitrarily deeply-nested hierarchical documents as data source Extensibility and customisability of XML Integrating XML into existing relational database systems is difficult: Tables and fields are basic storage element instead of arbitrary hierarchy XML structures need to be mapped in and out of tables for every query High number of joins to recreate XML structure

Querying XML Documents Queries on XML documents are usually about the hierarchical structure (relationships between nodes) Types of relationships: parent-child ancestor-descendant sibling (previous and following) Processing such queries without native XML databases can be difficult and slow

Storage in XML Databases: Collections of Trees Trees XML documents are hierarchical trees, so storing them as trees benefits applications which need to navigate around that tree Collections Collection-based implementations are useful for applications that query a collection of XML documents Access XPath and XQuery are the main ways to access collections of XML trees

What is XQuery? It is a domain-specific method for accessing and manipulating XML It is meant for querying XML It is built upon XPath It is like SQL but for XML A W3C recommendation

XQuery: Expressions path expressions return a nodeset element constructors return a new element FLWOR expressions analogous to SQL Select statement list expressions operations on lists or sets of values conditional expressions traditional if then else construction qualified expressions boolean operations over lists or sets of values datatype expressions test datatypes of values

XQuery: Path Expression The simplest kind of XQuery that you've already seen: document("test.xml")//p //p/foreign[@xml:lang='lat'] //foreign[@xml:lang='lat']/text() //tei:tei//tei:person[@age >= 25]

XQuery: Element constructor You may construct elements and embed XQueries or create well-formed results in the output you return. These may contain literal text or variables: <latin>o tempora o mores</latin> <latin>{$s}</latin> <li>name: {$surname}, {$forename}</li> <li>birth Country: {data($person/tei:birth/tei:placename/tei:country)} </li>

XQuery: FLWOR expressions For - Let - Where - Order - Return declare namespace tei="http://www.tei-c.org/ns/1.0"; for $t in document("book.xml")//tei:text let $latinphrases := $t//tei:foreign[@xml:lang='lat'] where count($latinphrases) > 1 order by count($latinphrases) return <list><item>id: {data($t/@xml:id)}</item> for defines a cursor over an xpath let defines a name for the contents of an xpath where selects from the nodes order sorts the results return specifies the XML fragments to construct Curly braces are used for grouping, and defining scope

XQuery: List Expressions XQuery expressions manipulate lists of values, for which many operators are supported: constant lists: (7, 9, <thirteen/>) integer ranges: i to j XPath expressions concatenation set operators: (or union), intersect, except functions: remove, index-of, count, avg, max, min, sum, distinct-values...

XQuery: List Expressions (cont.) When lists are viewed as sets: XML nodes are compared on their node identity Any duplicates which exist are removed Unless re-ordered the database order is preserved

XQuery: Conditional Expressions <div> { if document("xqt")//tei:title/text() ="Introduction to XQuery" then <p>this is true.</p> else <p>this is false.</p> } </div> More often used in user-defined functions

XQuery: Datatype Expressions XQuery supports all datatypes from XML Schema, both primitive and complex types Constant values can be written: as literals (like string, integer, float) as constructor functions (true(), date("2001-06-07")) as explicit casts (cast as xsd:positiveinteger(47)) Arbitrary XML Schema documents can be imported into an XQuery An instance of operator allows runtime validation of any value relative to a datatype or a schema. A typeswitch operator allows branching based on types.

XQuery and Namespaces As with XPath queries, if your documents are in a particular namespace then this namespace must be declared in your query Namespace prefixes must be used to access any element in a namespace Multiple namespaces can be declared and used Output can be in a different namespace to the input

XQuery Example: Multiple Variables One of the real benefits that XQuery gives you over XPath queries is that you can define multiple variables: (: All person elements and place elements :) declare namespace tei="http://www.tei-c.org/ns/1.0"; for $issues in collection('/db/punch')//tei:tei let $people := $issues//tei:person let $places := $issues//tei:places return <div>{$people} {$places}</div>

XQuery Example: Element Constructors You can construct the results into whatever elements you want: ( : Women's Birth and Death Countries in placename elements :) declare namespace tei="http://www.tei-c.org/ns/1.0"; let $issues := collection('/db/pc')//tei:tei for $person in $issues//tei:person[@sex = '2'] let $birthcountry := $person/tei:birth//tei:country let $deathcountry := $person/tei:death//tei:country return <div><p>this woman was born in <placename>{$birthcountry}</placename> and died in <placename>{$deathcountry}</placename>.</p> </div>

Result: Element Constructors A series of <div> elements like: <div> <p>this woman was born in <placename> England </placename> and died in <placename> Ethiopia</placeName>. </p> </div> <!-- and more divs after this -->

XQuery Example: Traversing the Tree You are not limited to one section of the document: (: Getting the issue title for each person :) declare namespace tei="http://www.tei-c.org/ns/1.0"; let $issues := collection('/db/pc')//tei:tei for $person in $issues//tei:person[@sex ='2'] let $birthcountry := $person/tei:birth//tei:country let $deathcountry := $person/tei:death//tei:country let $name := $person/tei:persname let $title := $person/ancestor::tei:tei/tei:teiheader//tei:title[1] return <div> <head>{$name}</head> <p>this woman mentioned in {$title} was born in <placename>{$birthcountry}</placename> and died in <placename>{$deathcountry}</placename>.</p> </div>

Result: Traversing the Tree A series of <div> elements like: <div> <head><persname>sylvia Pankhurst</persName></head> <p> This woman mentioned in <title>punch, or the London Charivari, Vol. 147, July 1, 1914</title> was born in <placename> England</placeName> and died in<placename> Ethiopa </placename>. </p> </div>

XQuery Example: Using Functions (: Getting birth date attribute and parts thereof :) declare namespace tei="http://www.tei-c.org/ns/1.0"; let $issues := collection('/db/punch')//tei:tei for $person in $issues//tei:person[@sex ='2'] let $birthcountry := $person/tei:birth//tei:country let $birthdate := $person/tei:birth/@when let $name := $person/tei:persname let $title := $person/ancestor::tei:tei/tei:teiheader//tei:title[1] return <div> <head>{$name}</head> <p>this woman mentioned in {$title} was born in <placename>{$birthcountry}</placename>. The date of their birth was in the year <date>{$birthdate} {data(substring-before($birthdate, '-'))}</date>. </p> </div>

Result: Using Functions A series of <div> elements like: <div> <head><persname>sylvia Pankhurst</persName></head> <p> This woman mentioned in <title>punch, or the London Charivari, Vol. 147, July 1, 1914</title> was born in <placename> England</placeName>. The date of their birth was in the year <date when="1882-05-05"> 1882 </date>.</p> </div>

XQuery Example: Nesting For Loops (: Number of people in an issue, women's forenames :) declare namespace tei="http://www.tei-c.org/ns/1.0"; for $issues in collection('/db/punch')//tei:tei let $title := $issues//tei:teiheader//tei:title[1] let $number := count($issues//tei:person) return <div> <head>{$title}</head> <p>this issue has {$number} people.</p> { for $person in $issues//tei:person[@sex ='2'] let $forename := $person//tei:forename return <p>this issue has a woman whose first name is {$forename}.</p> }</div>

Result: Nesting For Loops A series of <div> elements like: <div> <head>punch, or the London Charivari, Vol. 147, July 1, 1914</head> <p> This issue has 30 people.</p> <p> This issue has a woman whose first name is Sylvia. </p> <p> This issue has a woman whose first name is Hilda. </p>

XQuery Example: Embedding in HTML (: XQuery nested inside HTML :) declare namespace tei="http://www.tei-c.org/ns/1.0"; <html> <head><title>issues with numbers</title></head> <body> { for $issues in collection('/db/pc')//tei:tei let $title := $issues//tei:teiheader//tei:title[1]/text() let $number := count($issues//tei:person) return <div> <h1>{$title}</h1> <p>this issue has {$number} people.</p> </div> } </body> </html>

Result: Embedding in HTML An HTML document like: <html> <head> <title> Issues with numbers </title> </head> <body> <div> <h1>punch, or the London Charivari, Vol. 147, July 1, 1914</h1> <p> This issue has 20 people. </p> </div> <div> <h1>>punch, or the London Charivari, Vol. 147, July 15, 1914</h1> <p> This issue has 30 people. </p> </div> <!-- Many more issues--> </body> </html>

XQuery in Practice You can handle requests passed from HTML forms inside your XQueries That XQuery is being used is invisible to the user exist is often run embedded into Apache's Cocoon web publishing framework You can send queries to exist in all sorts of ways including from within Java programs and via HTTP/REST, XML-RPC, SOAP, WebDAV. You can use XUpdate to add/change/delete nodes in the XML database