Presentation is loading. Please wait.

Presentation is loading. Please wait.

Querying XML: XQuery, XPath, and SQL/XML in Context

Similar presentations


Presentation on theme: "Querying XML: XQuery, XPath, and SQL/XML in Context"— Presentation transcript:

1 Querying XML: XQuery, XPath, and SQL/XML in Context
Authors: Jim Melton and Stephen Buxton Publisher: Morgan Kaufmann Publication Year: Lecturer: Kyungpook National University School of EECS Lab. of Database Systems Young Chul Park

2 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML 15.1 Introduction 15.2 SQL/XML Publishing Functions  Publishing SQL data to XML. Examples : XMLELEMENT, XMLATTRIBUTES XMLAGG XMLFOREST XMLCONCAT Summary 15.3 XML Data Types  Store and manage XML natively in a SQL database 15.4 XQuery Functions  Querying XML data in SQL to produce either SQL data, XML data, or a Boolean result. XMLQUERY  XML data XMLTABLE  SQL data XMLEXISTS  a Boolean result 15.5 Managing XML in the Database  The language for piecewise updates of XML. 15.6 Talking the Same Language – Mappings Character Sets Names Types and Values Mapping SQL Data Types to XML Schema Data Types Mapping XML Schema Atomic Values to SQL Values The Casting Functions – XMLSERIALIZE, XMLPARSE, XMLCAST Mapping SQL Tables, Schemas, Catalogs to XML 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

3 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML 15.1 Introduction The goal of the SQLX Group was to define a set of extensions to the SQL standard to integrate SQL and XML, supporting bidirectional movement of data allowing SQL programmers to manage and query XML data. Specifically they worked to: Query SQL data and publish the results as XML Store and manage XML natively in a SQL database Query XML and publish the results as either XML or as SQL data Map SQL data and identifiers to XML, and vice versa SQL/XML : the short name for part 14 of the SQL standard, that part that deals with XML. “SQL part 14, XML-Related Specifications (SQL/XML).” SQL/XML:2003, SQL/XML:2006, SQL/XML:2007 SQLX: the informal name of the group that creates and presents proposals for SQL/XML to INCITS. INCITS : the U.S. body chartered with development of standards in IT, including SQL. SQLXML: a Microsoft term for Microsoft’s proprietary SQL extensions that do roughly the same thing as the SQL/XML publishing functions. The “SQL/XML functions” (e.g., publishing functions and XQuery functions) are not actually functions at all. In SQL terms, they are pseudo-functions. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

4 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML 15.2 SQL/XML Publishing Functions Examples Figure 15-1 movie, SQL Representation (Reproduced from Chapter 1 XML) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

5 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example 15-2 Less Simple XMLELEMENT SELECT XMLELEMENT(NAME title, XMLATTRIBUTES(runningTime AS “RunningTime”, (select familyName from directors AS d where d.id = m.director) AS “Director”), ‘This movie title is ‘ || m.title) AS “Movie Titles” FROM movies AS m; Results: Movie Titles <TITLE RunningTime=“98” Director=“Landis”>This movie title is An American Werewolf in London</TITLE> <TITLE RunningTime=“109” Director=“Landis”>This movie title is Animal House</TITLE> (2 rows in the result) Example 15-1 Simple XMLELEMENT SELECT XMLELEMENT(NAME “title”, title) AS “Movie Titles” FROM movies; Results: Movie Titles <title>An American Werewolf in London</title> <title>Animal House</title> (2 rows in the result) xml_element ::=XMLELEMENT(NAME element_name, [xml_attributes,] [xml_element_list,] text_source) xml_attributes ::= XMLATTRIBUTES(text_source_attr_name_list) text_source_attr_name_list ::= {text_source AS attr_name}+ Element_name ::= identifier | “identifier” /* identifier  IDENTIFIER; “identifier”  identifier */ Note (1) XML fragment : (2) The SQL query iterates over each row in the table, returning XMLELEMENT(NAME, “title”, title) for each row. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

6 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML XMLAGG Example 15-3 Titles of Movies Using XMLELEMENT (WRONG) SELECT XMLELEMENT(NAME “all-titles”, XMLELEMENT(NAME,”title”, title)) AS “Movie Titles” FROM movies; Results: Movie Titles <all-titles> <title>An American Werewolf in London</title> </all-titles> <title>Animal House</title> (2 rows in the result) Example 15-4 Titles of Movies Using XMLAGG (RIGHT) SELECT XMLELEMENT(NAME “all-titles”, XMLAGG(XMLELEMENT(NAME,”title”, title) ORDER BY yearReleased ASC)) AS “Movie Titles” FROM movies; Results: Movie Titles <all-titles> <title>Animal House</title> <title>An American Werewolf in London</title> </all-titles> (1 row selected) XMLAGG takes an XMLELEMENT function call as its argument, and produces a single result that is the aggregate of all the elements produced by XMLELEMENT. It’s an aggregate function in the same way that MIN, MAX, and COUNT are aggregate functions in SQL. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

7 Querying XML: XQuery, XPath, and SQL/XML in Context
XMLFOREST Chapter 15 SQL/XML Example 15-5 Using XMLELEMENT to Get Values “Across the Row” SELECT XMLELEMENT(NAME “movie-details”, XMLELEMENT(NAME,”title”, title), XMLELEMENT(NAME, “yearReleased”, yearreleased), XMLELEMENT(NAME, “runningTime”, runningtime) ) AS “Movie Details” FROM movies; Results: Movie Details <movie-details> <title>An American Werewolf in London</title> <yearReleased>1981</yearReleased> <runningTime>98</runningTime> </movie-details> <title>Animal House</title> <yearReleased>1978</yearReleased> <runningTime>109</runningTime> (2 rows in the result) Example 15-5 Using XMLFOREST to Get Values “Across the Row” SELECT XMLELEMENT(NAME “movie-details”, XMLFOREST( title AS “title”, yearreleased AS “yearReleased”, runningtime AS “runningTime”) ) AS “Movie Details” FROM movies; Results: Movie Details <movie-details> <title>An American Werewolf in London</title> <yearReleased>1981</yearReleased> <runningTime>98</runningTime> </movie-details> <title>Animal House</title> <yearReleased>1978</yearReleased> <runningTime>109</runningTime> (2 rows in the result) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

8 Querying XML: XQuery, XPath, and SQL/XML in Context
XMLFOREST Chapter 15 SQL/XML Example 15-7 Producer-Details Using XMLELEMENT SELECT XMLELEMENT(NAME “producer-details”, XMLELEMENT(NAME,”givenName”, givenname), XMLELEMENT(NAME, “familyName”, familyname), XMLELEMENT(NAME, “otherNames”, othernames) ) AS “Producer Details” FROM movies; Results: Producer Details <producer-details> <givenName>George, Jr.</givenName> <familyName>Folsey</familyName> <otherNames></otherNames> </producer-details> <givenName>Peter</givenName> <familyName>Guber</familyName> <givenName>Ivan</givenName> <familyName>Reitman</familyName> (3 rows in the result) Example 15-7 Producer-Details Using XMLFOREST SELECT XMLELEMENT(NAME “producer-details”, XMLFOREST( givenname AS “givenName”, familyname AS “familyName”, othernames AS “otherNames”) ) AS “Producer Details” FROM movies; Results: Producer Details <producer-details> <givenName>George, Jr.</givenName> <familyName>Folsey</familyName> </producer-details> <givenName>Peter</givenName> <familyName>Guber</familyName> <givenName>Ivan</givenName> <familyName>Reitman</familyName> (3 rows in the result) XMLFOREST skips NULL values and does not produce empty elements for otherNames. You cannot add attributes to elements produced by XMLFOREST. If you need attributes on the elements, you have to use XMLELEMENT. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

9 Querying XML: XQuery, XPath, and SQL/XML in Context
XMLCONCAT Chapter 15 SQL/XML Example 15-9 XMLCONCAT SELECT XMLCONCAT( XMLELEMENT(NAME,”givenName”, givenname), XMLELEMENT(NAME, “familyName”, familyname), XMLELEMENT(NAME, “otherNames”, othernames) ) AS “Producer Details” FROM movies; Results: Producer Details <givenName>George, Jr.</givenName> <familyName>Folsey</familyName> <otherNames></otherNames> <givenName>Peter</givenName> <familyName>Guber</familyName> <givenName>Ivan</givenName> <familyName>Reitman</familyName> (3 rows in the result) XMLCONCAT takes a list of XML values and concatenates them into a single XML value. In Example 15-9, XMLCONCAT concatenates the first, last, and other names into a single XML value, resulting in one XML value (an XML forest) for each row. XMLCONCAT and XMLFOREST both take in a list of values, and output an XML forest. The difference is that the input to XMLCONCAT is a list of XML values, while the input to XMLFOREST is a list of SQL values. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

10 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Summary Table 15-1 SQL/XML Publishing Functions  All the publishing functions in SQL:2003. Input Output Notes XMLELEMENT an element-name, optionally a call to XMLATTRIBUTES, and a SQL value expression An XML element create an element, optionally with attributes, from a SQL value expression XMLATTRIBUTES a list of SQL value expression, attribute-name pairs attributes create attributes from SQL value expressions – only valid as the second argument to XMLELEMENT XMLAGG a list of XML values, such as the output from a call to XMLELEMENT or XMLCONCAT a forest of elements create a forest of elements from SQL value expressions “down the table” XMLFOREST a list of SQL value expression, element name pairs create a forest of elements from column values “across the row” XMLCONCAT two or more XML values concatenates two or more XML values into a single XML value (a forest) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

11 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Summary These functions can be combined with each other, and with the rest of the SQL language, into arbitrarily complex queries. In SQL/XML:2006, there are at least two additional publishing functions. XMLCOMMENT : create a comment node XMLPI : create a processing instruction node In addition, XMLELEMENT, XMLAGG, XMLFOREST, and XMLCONCAT acquire an optional RETURNING { CONTENT | SEQUENCE } clause, so each of these functions can return either an XML document or an XML sequence. This is a direct consequence of the decision in SQL/XML:2006 to upgrade the publishing functions from use of the XML Infoset to the XQuery Data Model. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

12 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML 15.3 XML Data Types SQL/XML:2003 introduced the notion of a native SQL data type for XML, called “XML.” Values of type XML are XML values. The functions in the following sections can take in XML values as arguments, and output XML values as results. SQL/XML:2006 extends the notion of an XML data type in two ways. SQL/XML:2006 adopts the XQuery Data Model rather than the XML Infoset, so that any XML value (an instance of the XML data type) is now an XQuery sequence, as defined in the XQuery Data Model. SQL/XML:2006 defines three subtypes of XML, represented as modifiers to the XML type – XML( DOCUMENT ), XML ( CONTENT ), and XML( SEQUENCE ). DOCUMENT : mans the XML value is a well-formed XML document. CONTENT : is an XML fragment (there may be multiple top-level elements or text nodes) wrapped in a document node. SEQUENCE : is any sequence of nodes and/or values. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

13 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML For each XML value, there are two properties that may or may not hold. The sequence may consist of a single Document node (Document node). If the sequence is a single Document node, it may have only the children that are valid for a Document node in a well-formed XML Document – exactly one element node plus zero or more comment nodes, Processing Instruction nodes, etc. (legal Document children). Given these properties, we can say that Every XML value is an XML( SEQUENCE ). An XML( SEQUENCE ) that is a Document node is an XML( CONTENT ). An XML( SEQUENCE ) that is a Document node (i.e., an XML( CONTENT )) that has legal Document Children is an XML( DOCUMENT ). The SQL:2005 XML type forms the structure hierarchy illustrated in Figure 15-2. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

14 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Note that, like all SQL data types, the value of an XML type may be null, and that a null XML value matches all the XML types. The XML type may have a second modifier, one of UNTYPED, XMLSCHEMA (with some associated Schema information), or ANY. “UNTYPED” means that there is no associated XML Schema (i.e., every element is of type “xdt:untypedAny” and every attribute is of type “xdt:untypedAtomic”). Note that “xdt” is the namespace prefix used for XPath (XQuery) data types. This namespace prefix is predefined in XQuery, and is used elsewhere by convention. “XMLSCHEMA” means there is an associated XML Schema. The second (optional) modifier appears in parentheses after the first modifier so that, for example, an untyped document node has the type “XML( DOCUMENT ( UNTYPED ) ).” If the second modifier is “XMLSCHEMA,” there are two ways to identify the actual Schema instance. First, you can supply a SQL identifier for some registered Schema (some Schema instance that is “known” to the SQL environment). An example is: XML( DOCUMENT (XMLSCHEMA ID “smith.movies-schema” ) ) XML( DOCUMENT (XMLSCHEMA URI “ ELEMENT “movie” ) ) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

15 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Grammar 15-1 XML type XML [ ( { DOCUMENT | CONTENT | SEQUENCE } [ ( {ANY | UNTYPED | XMLSCHEMA schema-details} ) ] )] where schema-details is: URI target-namespace-URI [ LOCATION schema-location ] [ ELEMENT element-name] | NONAMESPACE [ LOCATION schema-location ] [ ELEMENT element-name ] | ID registered-schema-name [ ELEMENT element-name ] XML == XML ( SEQUENCE ), XML(CONTENT( ANY )), or XML(CONTENT(UNTYPED)) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

16 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML 15.4 XQuery Functions In SQL/XML:2006, three additional functions are defined – XMLQUERY, XMLTABLE, and XMLEXISTS – which use XQuery. While XMLQUERY fits naturally into the SELECT clause of a SQL query, and XMLTABLE fits naturally into the FROM clause, XMLEXISTS sits comfortably in the WHERE clause. Table 15-2 MOVIES_XML ID MOVIE 42 <movie> <title>An American Werewolf in London</title> <yearReleased>1981</yearReleased> </movie> 43 <title>Animal House</title> <yearReleased>1978</yearReleased> Two columns – ID , of type INTEGER, and MOVIE, of type XML. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

17 Querying XML: XQuery, XPath, and SQL/XML in Context
XMLQUERY Chapter 15 SQL/XML Grammar 15-2 XMLQUERY Syntax Summary XMLQUERY( XQuery-expression [ PASSING { BY REF | BY VALUE } argument-list ] [RETURNING { CONTENT | SEQUENCE } [{ BY REF | BY VALUE }]] NULL ON EMPTY | EMPTY ON EMPTY ) where argument-list is a comma-separated list of: context item | value-expression AS identifier Example Simple XMLQUERY SELECT XMLQUERY(‘for $m in $col/movie return $m/title’ PASSING movie AS “col” RETURNING CONTENT NULL ON EMPTY ) AS result FROM MOVIES_XML; (1) XQuery-expression : ‘for … $m/title’ (2) PASSING: pass the data in the column movie to the variable named col. (3) RETURNING CONTENT : ensure that the result is serialized and returned BY VALUE to the SQL engine – BY REF or BY VALUE can only be specified with RETURNING SEQUENCE. RESULT <title>An American Werewolf in London</title> <title>Animal House</title> (2 rows in the result) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

18 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example Simple XMLQUERY over an XML file SELECT XMLQUERY(‘for $m in doc(“/public/movies.movies-we-own.xml”)/movies/movie return $m/title’ RETURNING CONTENT NULL ON EMPTY) AS result FROM DUAL; RESULT <title>The Fifth Element</title> <title>An American Werewolf in London</title> <title>American Graffiti</title> (1 row in the result) DUAL : is a dummy, 1-row table available in every Oracle schema, for syntactic convenience. Some SQL implementations allow you to leave out the FROM clause together. In this case, XMLQUERY merely provides a convenient harness (a context) for running an XQuery. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

19 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example XMLQUERY and AVG SELECT AVG( XMLCAST( XMLQUERY(‘for $m in $col/movie return $m/runningTime/text()’ PASSING movie AS “col” RETURNING CONTENT NULL ON EMPTY) AS decimal(8,1) ) ) AS “avgRunningTime” FROM MOVIES_XML; Results: avgRunningTime 103.5 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

20 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example XMLQUERY, Complete SELECT XMLQUERY(‘for $m in $col/movie let $producers := $m/producer where $m/yearReleased > 1950 return <output> {$m/title} {for $p in $producers <prodFullName> {concat($p/givenName, “ “, $p/familyName)} </prodFullName> } </output>’ PASSING movie AS “col” RETURNING CONTENT NULL ON EMPTY )AS “Results” FROM MOVIES_XML; Results: Results <output> <title>An American Werewolf in London</title> <prodFullName>George, Jr. Folsey</prodFullName> <prodFullName>Peter Guber</prodFullName> <prodFullName>Jon Peters</prodFullName> </output> <title>Animal House</title> <prodFullName>Matty Simons</prodFullName> <prodFullName>Ivan Reitman</prodFullName> (2 rows in the result) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

21 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML XMLTABLE XMLTABLE uses XQuery to query some XML, and returns the result in the same form as a SQL table. Grammar 15-3 XMLTABLE Syntax Summary XMLTABLE( [ namespace-declaration , ] XQuery-expression (: the row pattern :) [ PASSING argument-list ] COLUMNS XMLtbl-column-definitions (: the column definitions :) ) where argument-list is: value-expression AS identifier and XMLtbl-column-definitions is a comma-separated list of column definitions, which may contain: column-name FOR ORDINALITY and/or: column-name data type [ BY REF | BY VALUE ] // the default is BY VALUE [ default clause ] [ PATH XQuery-expression ] (: the column pattern :) XQuery-expression is a string containing an XQuery expression that expresses the contents of each row. The argument list is the same as for XMLQUERY, except that arguments in the list are always passed by reference. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

22 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example Simple XMLTABLE SELECT result.* FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m/title’ PASSING movies_xml.movie AS “col” ) AS result; Results: COLUMN_VALUE <title>An American Werewolf in London</title> <title>Animal House</title> (2 rows in the result) Note: The result of XMLTABLE() of Example is a SQL table with a single column of type XML( CONTENT ), and one row for each member of the sequence returned by the XQuery-expression. For each row in the passed-in table (MOVIES_XML), XMLTABLE evaluates the row pattern (the XQuery-expression). $col is an XML sequence, formed by casting the contents of the column movie in the table MOVIES_XML to XML( CONTENT ). This sequence is passed in by reference, so the row pattern is free to use reverse axes on the data passed in (to find its parent and ancestors). The result of evaluating an XQuery expression is always a sequence of items(, where an item is either a node or a value), in the example a sequence of two title elements. If we use “SELECT * …,” the query returns the columns resulting from the join of MOVIES_XML with the table created by XMLTABLE (i.e., id, movie, and column_value), which is not what we want. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

23 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example XMLTABLE with Column Definitions SELECT result.* FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m’ PASSING movies_xml.movie AS “col” COLUMNS “title” VARCHAR(80) PATH ‘title’, “runningTime” INTEGER PATH ‘runningTime’, “yearReleased” INTEGER PATH ‘yearReleased’ ) AS result; Results: title runningTime yearReleased An American Werewolf in London Animal House (2 rows in the result) Note <column-name, the SQL data type of the column, the XQuery expression that addresses the contents for the column (the column pattern)>, where the XQuery expression has as its context the result of the row pattern. <column-name, FOR ORDINALITY>, which captures the document order of the results within each sequence passed to the row pattern (i.e., within each row passed in). 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

24 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example XMLTABLE with Column Definitions, Alternative Syntax SELECT result.* FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m’ PASSING movies_xml.movie AS “col” COLUMNS “title” VARCHAR(80), “runningTime” INTEGER, “yearReleased” INTEGER, “producer[1]/familyName” VARCHAR(20) ) AS result (“TITLE”, “RUNNINGTIME”, “YEARRELEASED”, “PRODUCER”); Results: TITLE RUNNINGTIME YEARRELEASED PRODUCER An American Werewolf in London Folsey Animal House Simmons (2 rows in the result) Note: If the column name is exactly the same as the column pattern, you can leave out the column pattern altogether and just specify the column name and type. In SQL, if you do define a column name that is not all-uppercase alphanumerics, then you must quote the column-name every time you use it. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

25 Querying XML: XQuery, XPath, and SQL/XML in Context
Dealing with Repeating Elements Denormalize Use XML for repeating elements. Detailed Tables SQL:2003 types Chapter 15 SQL/XML Example Repeating Elements, Denormalized Result Table (Normalization  Denormalization) SELECT result.* FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m’ PASSING movies_xml.movie AS “col” COLUMNS “title” VARCHAR(80) PATH ‘title’, “producer1” VARCHAR(12) PATH ‘producer[1]/familyName’, “producer2” VARCHAR(12) PATH ‘producer[2]/familyName’ DEFAULT ‘none’, “producer3” VARCHAR(12) PATH ‘producer[3]/familyName’ DEFAULT ‘none’, ) AS result; Results: title producer producer producer3 An American Werewolf in London Folsey Guber Peters Animal House Simmons Reitman none (2 rows in the result) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

26 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Results: title producers An American Werewolf in London <producer> <familyName>Folsey</familyName> <givenName>George, Jr.</givenName> <otherNames/> </producer> <producer> <familyName>Guber</familyName> <givenName>Peter</givenName> <familyName>Peters</familyName> <givenName>Jon</givenName> Animal House <producer> <familyName>Simons</familyName> <givenName>Matty</givenName> <familyName>Reitman</familyName> <givenName>Ivan</givenName> (2 rows in the result) Example Repeating Elements, XML in the Result Table SELECT result.* FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m’ PASSING movies_xml.movie AS “col” COLUMNS “title” VARCHAR(80) PATH ‘title’, “producers” XMLTYPE PATH ‘producer’, ) AS result; 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

27 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Results: title ord familyName An American Werewolf in London Folsey An American Werewolf in London Guber An American Werewolf in London Peters Animal House Simons Animal House Reitman (5 rows in the result) Example Repeating Elements, Detail Table SELECT result.”title”, result2.* FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m’ PASSING movies_xml.movie AS “col” COLUMNS “title” VARCHAR(80) PATH ‘title’, “producers” XMLTYPE PATH ‘producer’ ) AS result, XMLTABLE(‘for $prod in $p/producer return $prod’ PASSING result.”producers” AS “p” “ord” FOR ORDINALITY, “familyName” VARCHAR(20) PATH ‘familyName’ ) AS result2; Note: One common SQL technique for storing repeating data is to move that data into a separate table, where the data can repeat as multiple rows (rather than multiple column columns, as in Example 15-17). This table is known as a detail table. A single row in the master table – in our example, MOVIES – is mapped to multiple rows in the detail table via some key that is unique in the master table. This is known as a master-detail relationship, and the key is a primary key in the master table and a foreign key in the detail table. Example produces a detail table for producers, using two calls to XMLTABLE. Just as we can pass in values from the MOVIES_XML table to the first call to XMLTABLE, so we can also pass in values from the result table of the first XMLTABLE call to a second XMLTABLE call. Each member of a sequence has an ordinality – a number that designates its order in the sequence. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

28 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Note: The SQL:2003 standard introduced a number of ways of modeling data that is not naturally “table-shaped,” notably ARRAYs, nested tables, and objects. Unfortunately, XMLTABLE does not allow an ARRAY or nested table in the result table – SQL/XML only allows casting of XML to SQL predefined types, and ARRAY and nested table are constructed types. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

29 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Dealing with Complex Elements Example Complex Elements, Denormalized Result Table SELECT result.* FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m’ PASSING movies_xml.movie AS “col” COLUMNS “title” VARCHAR(80) PATH ‘title’, “producerF” VARCHAR(12) PATH ‘producer[1]/familyName’, “producerG” VARCHAR(12) PATH ‘producer[1]/givenName’, “producerO” VARCHAR(12) PATH ‘producer[1]/otherNames’ DEFAULT ‘none’ ) AS result; Results: title ProducerF producerG producerO An American Werewolf in London Folsey Guber, Jr none Animal House Simons Matty none (2 rows in the result) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

30 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example Complex Repeating Elements, PRODUCERS Table (1) SELECT DISTINCT result.* FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m/producer’ PASSING movies_xml.movie AS “col” COLUMNS “ID” FOR ORDINALITY, “familyName” VARCHAR(12) PATH ‘familyName’, “givenName” VARCHAR(12) PATH ‘givenName’ DEFAULT ‘none’, “otherNames” VARCHAR(12) PATH ‘otherNames’ DEFAULT ‘none’ ) AS result; Results: ID familyName givenName otherNames Folsey George, Jr none Simons Matty none Guber Peter none Reitman Ivan none Peters Jon none (5 rows in the result) Results: ID familyName givenName otherNames Folsey George, Jr none Guber Peter none Peters Jon none Simons Matty none Reitman Ivan none (5 rows in the result) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

31 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example Complex Repeating Elements, PRODUCERS Table (2) SELECT (10*movies_xml.ID)+result.”ord” AS “ID”, result.”familyName”, result.”givenName”, result.”otherNames” FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m/producer’ PASSING movies_xml.movie AS “col” COLUMNS “ord” FOR ORDINALITY, “familyName” VARCHAR(12) PATH ‘familyName’, “givenName” VARCHAR(12) PATH ‘givenName’ DEFAULT ‘none’, “otherNames” VARCHAR(12) PATH ‘otherNames’ DEFAULT ‘none’ ) AS result ORDER BY “ID” ASC; Results: ID familyName givenName otherNames Folsey George, Jr none Guber Peter none Peters Jon none Simons Matty none Reitman Ivan none (5 rows in the result) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

32 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example Complex Repeating Elements, MOVIE_PRODUCERS Table SELECT movies_xml.id AS “MOVIE”, (10*movies_xml.ID)+result.”ord” AS “PRODUCER” FROM movies_xml , XMLTABLE(‘for $m in $col/movie return $m/producer’ PASSING movies_xml.movie AS “col” COLUMNS “ord” FOR ORDINALITY, ) AS result; Results: MOVIE PRODUCER (5 rows in the result) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

33 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Representing XML as SQL Data We have shown how to represent the movies XML documents as SQL data. This is tremendously useful for two reasons. (1) It frees the data administrator from the pressure of having to decide how to store data. If the data is born as XML, he now has the freedom (1-1) to leave the data as XML and process it as XML, (1-2) to convert the data to SQL and physically store it relationally (but still publish it as XML), or (1-3) to create a set of SQL views that make the XML data look like SQL. (2) you can now apply all the power of SQL to your XML data, without necessarily shredding the data and storing it in tables. The analytic functions of SQL rollup and cube. Analytic functions (also known as OLAP (online analytic processing) functions) are generally used to analyze vast amounts of data. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

34 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example Rollup Function on XML Data SELECT result.”yearReleased”, result.”director”, avg(result.”runningTime”) AS “length” FROM movies_xml_big , XMLTABLE(‘for $m in $col/movie return $m’ PASSING movies_xml_big.movie AS “col” COLUMNS “title” VARCHAR(80) PATH ‘title’, “yearReleased” NUMBER PATH ‘yearReleased’, “producers” XMLTYPE PATH ‘producer’, “runningTime” NUMBER PATH ‘runningTime’, “director” VARCHAR(12) PATH ‘director[1]/familyName’ ) AS result WHERE result.”yearReleased” IN (1997, 1998, 1999) GROUP BY rollup(result.”yearReleased”, result.”director”) ORDER BY result.”yearReleased” ASC; Results: yearReleased director length Besson Duvall Jeunet Peterson Bay Coen Mendes Myrick Peirce Wheeler 116.7 (14 rows in the result) 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

35 Querying XML: XQuery, XPath, and SQL/XML in Context
Results: yearReleased director length Besson Duvall Jeunet Peterson Bay Coen Mendes Myrick Peirce Wheeler Bay Besson Coen Duvall Jeunet Mendes Myrick Peirce Peterson Wheeler 116.7 (24 rows in the result) Chapter 15 SQL/XML Example Cube Function on XML Data SELECT result.”yearReleased”, result.”director”, avg(result.”runningTime”) AS “length” FROM movies_xml_big , XMLTABLE(‘for $m in $col/movie return $m’ PASSING movies_xml_big.movie AS “col” COLUMNS “title” VARCHAR(80) PATH ‘title’, “yearReleased” NUMBER PATH ‘yearReleased’, “producers” XMLTYPE PATH ‘producer’, “runningTime” NUMBER PATH ‘runningTime’, “director” VARCHAR(12) PATH ‘director[1]/familyName’ ) AS result WHERE result.”yearReleased” IN (1997, 1998, 1999) GROUP BY cube(result.”yearReleased”, result.”director”) ORDER BY result.”yearReleased” ASC; 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

36 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Example XMLTABLE, Not So Simple SELECT result.”title”, avg(result.”rating”) AS “average rating”, count(result.”rating”) AS “# reviews” FROM movies_xml , reviews_xml, XMLTABLE( ‘for $m in $col/movie, $r in $col2/review let $rating := $r/rating, $title := $m/title where $m/title = $r/title and $r/medium = “movie” and not(contains($r/text, “awful”)) order by $r/rating return <output> <r>{$rating}</r> <t>{$title}</t> <p>{$r/medium}</p> </output>’ PASSING movies_xml.movie AS “col”, reviews_xml.review AS “col2” COLUMNS “title” VARCHAR(80) PATH ‘t’, “rating” NUMBER PATH ‘r’, “medium” VARCHAR(10) PATH ‘p’ ) AS result GROUP BY result.”title”; Results: title average rating # reviews An American Werewolf in London Animal House (2 rows in the result) Note: The contains in the where clause is doing a strict substring search and not full text search. 1-1) The substring contains, 1-2) the full text contains. In the general case, the row pattern returns a sequence (not a document). Since the context of the column pattern is the output element, the column pattern for, e.g., title is “./t,” or simply “t” for short. If the row pattern returned a document, then the context of the column pattern would have been the document node, and the path would have been “output/t.” 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

37 Querying XML: XQuery, XPath, and SQL/XML in Context
XMLEXISTS Chapter 15 SQL/XML Example XMLEXISTS SELECT ID FROM movies_xml WHERE XMLEXISTS(‘/movie/studio’ PASSING BY VALUE movie) Results: No rows selected Example XMLEXISTS with Predicate SELECT ID FROM movies_xml WHERE XMLEXISTS(‘/movie/title[contains(.,”Werewolf”)]’ PASSING BY VALUE movie) Results: ID 42 (1 row in the result) Note: XMLEXISTS has just the same syntax as XMLQUERY. It returns false if the result of the XQuery expression is the empty sequence, otherwise it returns true. XMLEXISTS does not add new functionality – i.e., any query written with XMLEXISTS could be written with XMLQUERY and/or XMLTABLE. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

38 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML 15.5 Managing XML in the Database What we need is the ability to express a piecewise insert, update, or delete of the XML as part of the SQL statement. Efforts are under way within the W3C XML Query Working Group (which has spawned an Update Task Force) to produce the language for piecewise updates of XML. See Chapter 13, “What’s Missing?” for more details. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

39 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML 15.6 Talking the Same Language – Mapping We have assumed that the two languages – SQL and XQuery/XPath – have the same context, or at least that there is an obvious mapping from the SQL context to the XQuery/XPath context and vice versa. In fact, these mappings are neither obvious nor trivial, and a great deal of early work of the SQLX group involved defining these mappings. In this section we look at a bit more closely at the way character sets, names, data types, and values are mapped. Character Sets XML data is Unicode – the W3C XML specification says that “All XML processors MUST accept the UTF-8 and UTF-16 encodings of Unicode 3.1.” SQL/XML standard insists that any SQL/XML implementation provides a mapping from strings of each character set supported in its database to strings in Unicode, and vice versa. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

40 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Names When using SQL/XML, we need to be able to map SQL identifiers to XML Names. (Step 1) map the SQL identifier characters to Unicode. (Step 2) Then, for each character, apply the following rules:  The partially escaped mapping. If it is a valid XML Name character, leave it unchanged. If it is not a valid XML Name character, convert it to a hexadecimal number (derived from its Unicode encoding), consisting of either four or six uppercase hexadecimal digits. Add a prefix of “_x” (underscore-x) and a suffix of “_” (underscore) to this number. Map a leading colon in the SQL identifier to “_x003A_.” SQL/XML also defines A fully escaped mapping – in addition to the rules in the partially escaped mapping, apply these rules: Map all colons in the SQL identifier to “_x003A_” (not just a leading colon). If the SQL identifier begins with “XML,” in any combination of cases, then prefix the XML value Name with “_xFFFF_.” Example the SQL identifier XML Name “title” “title” “Running Time” “Running_x0020_Time” SQL Identifier  XML Name  SQL Identifier : Fully Reversible XML Name  SQL Identifier  XML Name : Not Fully Reversible 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

41 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Types and Values Mapping SQL Data Types to XML Schema Data Types SQL data type XML Schema data type Character string types xs:string, with either the facet xs:length, or the facet xs:maxLength. Binary string type (BLOB) xs:hexBinary or xs:base64Binary, with the facet xs:maxLength to indicate the maximum length in octets. Exact numeric types NUMERIC and DECIMAL INTEGER, SMALLINT, and BIGINT xs:decimal, with the facets xs:totalDigits and xs:fractionDigits. Either xs:integer, with the facets xs:maxInclusive and xs:minInclusive, or to a subtype of xs:integer, possibly with the facets xs:maxInclusive and xs:minInclusive. Approximate numeric types REAL, DOUBLE PRECISION, and FLOAT Either xs:float or xs:double. BOOLEAN xs:boolean DATE xs:date, with the xs:pattern facet. TIME types xs:dateTime and xs:time, with the xs:pattern facet. Interval types xdt:yearMonthDuration and xdt:day-TimeDuration, with the xs:pattern facet. 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

42 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Mapping XML Schema Atomic Values to SQL Values A value with XML Schema type of SQL values of xs:string a Unicode string xs:hexaBinary and xs:base64Binary a binary string xs:decimal an exact numeric value xs:float or xs:double an approximate numeric value xs:time a value of SQL type TIME xs:dateTime a value of SQL type TIMESTAMP xs:date a value of SQL type DATE xs:boolean type BOOLEAN 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

43 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML The Casting Functions – XMLSERIALIZE, XMLPARSE, XMLCAST XMLSERIALIZE : XML-to-string function XMLPARSE : string-to-XML function XMLCAST : XML-to-SQL/SQL-to-XML/XML-to-XML casting function Example XMLPARSE INSERT INTO movies_xml (ID, movie) VALUES ( 77, XMLPARSE( DOCUMENT ‘<movie> … </movie>’ ) ); Example XMLSERIALIZE SELECT XMLSERIALIZE( DOCUMENT movie AS CLOB ) AS “CLOB data” FROM movies_xml; Results: CLOB data <movie> <title>An American Werewolf in London</title> <yearReleased>1981</yearReleased> <director> <familyName>Landis</familyName> <givenName>John</givenName> SQL/XML:2003 includes two functions to cast an XML value to a SQL character string and vice versa. XMLSERIALIZE takes an XML value and serializes it to some SQL character string type, while XMLPARSE takes some SQL character string type and converts it to an XML value. At the functions of XMLSERIALIZE() and XMLPARSE(), DOCUMENT : an XML document CONTENT : an XML forest, or fragment SQL/XML:2006 introduces a more general-purpose function for casting between XML values and any SQL type (actually, any SQL predefined type). XMLCAST takes two arguments, an operand and a target type. XMLCAST( X AS Y) Either the operand’s type or the target type must be XML, so XMLCAST will convert both to and from XML. Both the operand and the target type may be XML, so XMLCAST can also be used to cast from one flavor of the XML type to another. See Example 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context

44 Querying XML: XQuery, XPath, and SQL/XML in Context
Chapter 15 SQL/XML Mapping SQL Tables, Schemas, Catalogs to XML Figure 15-4 XML Representation of MOVIES Table. <MOVIES> <row> <ID>42</ID> <title>An American Werewolf in London</title> <yearReleased>1981</yearReleased> <director>78</director> <runningTime>98</runningTime> </row> <ID>43</ID> <title>Animal House</title> <yearReleased>1978</yearReleased> <runningTime>109</runningTime> </MOVIES> In addition to defining mappings for data types and atomic values, SQL/XML defines a structure mapping, from a SQL table or schema or catalog to an XML document. Let’s take as an example the MOVIES table in Figure The SQL/XML table mapping would produce a root element whose name is the name of the table (“MOVIES”), with a child element called “row” representing each row in the table. The row element in turn has a child element representing each column in the table, as in Figure 15-4. 15.7 Chapter Summary 2007-7/KNU Querying XML: XQuery, XPath, and SQL/XML in Context


Download ppt "Querying XML: XQuery, XPath, and SQL/XML in Context"

Similar presentations


Ads by Google