Static Typing in XQuery Based on Xquery from the Experts [Katz], chap 4 Presented by Iris Rudner.

Slides:



Advertisements
Similar presentations
XML Data Management 8. XQuery Werner Nutt. Requirements for an XML Query Language David Maier, W3C XML Query Requirements: Closedness: output must be.
Advertisements

XML Examples. Bank Information Basic structure: A-101 Downtown 500 … Johnson Alma Surrey … A-101 Johnson …
XML: Extensible Markup Language
Semantics Static semantics Dynamic semantics attribute grammars
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Chapter 6 UNDERSTANDING AND DESIGNING QUERIES AND REPORTS.
1 XML DTD & XML Schema Monica Farrow G30
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
Compiler Construction
Static Typing in XQuery Mary Fernández, Jérôme Siméon, Philip Wadler Επιμέλεια Παρουσίασης: Μαγδαληνός Παναγής.
XQuery from the Experts Chapter 5 – Introduction to the formal Semantics Νίκος Λούτας.
Lecture 14 XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name.
1 COS 425: Database and Information Management Systems XML and information exchange.
XML and The Relational Data Model
Context-sensitive Analysis, II Ad-hoc syntax-directed translation, Symbol Tables, andTypes.
Sunday, June 28, 2015 Abdelali ZAHI : FALL 2003 : XML Schemas XML Schemas Presented By : Abdelali ZAHI Instructor : Dr H.Haddouti.
XML –Query Languages, Extracting from Relational Databases ADVANCED DATABASES Khawaja Mohiuddin Assistant Professor Department of Computer Sciences Bahria.
C++ for Engineers and Scientists Third Edition
XML Schema Describing the structure of XML documents. A very brief introduction 1XML Schema.
Introduction to XML This material is based heavily on the tutorial by the same name at
1 Advanced Topics XML and Databases. 2 XML u Overview u Structure of XML Data –XML Document Type Definition DTD –Namespaces –XML Schema u Query and Transformation.
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
XP New Perspectives on XML Tutorial 4 1 XML Schema Tutorial – Carey ISBN Working with Namespaces and Schemas.
Lecture 15 XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name.
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.
XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation An Introduction to XQuery.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation XML Schema 2 Lecturer.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Query Data Model Lecturer.
New Perspectives on XML, 2nd Edition
Web 310 XML Schema : What You Need to Know and Why Yasser Shohoud Program Manager XML Messaging Microsoft Corporation.
CS 363 Comparative Programming Languages Semantics.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
XML – Part III. The Element … This type of element either has the element content or the mixed content (child element and data) The attributes of the.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
XML query. introduction An XML document can represent almost anything, and users of an XML query language expect it to perform useful queries on whatever.
Sheet 1XML Technology in E-Commerce 2001Lecture 2 XML Technology in E-Commerce Lecture 2 Logical and Physical Structure, Validity, DTD, XML Schema.
XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents.
1 Tutorial 14 Validating Documents with Schemas Exploring the XML Schema Vocabulary.
Tutorial 13 Validating Documents with Schemas
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
EGEE is a project funded by the European Union under contract IST XML Schema 3 – 4 June
CS 157B: Database Management Systems II February 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
Object-Oriented Programming Chapter Chapter
QUALITY CONTROL WITH SCHEMAS CSC1310 Fall BASIS CONCEPTS SchemaSchema is a pass-or-fail test for document Schema is a minimum set of requirements.
XSD: XML Schema Language Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
XML Query: xQuery Reference: Xquery By Priscilla Walmsley, Published by O’Reilly.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name value pair;
CSE 6331 © Leonidas Fegaras XQuery 1 XQuery Leonidas Fegaras.
Lecture 23 XQuery 1.0 and XPath 2.0 Data Model. 2 Example 31.7 – User-Defined Function Function to return staff at a given branch. DEFINE FUNCTION staffAtBranch($bNo)
CITA 330 Section 4 XML Schema. XML Schema (XSD) An alternative industry standard for defining XML dialects More expressive than DTD Using XML syntax Promoting.
Session III Chapter 10 – Defining Simple Types
XML: Extensible Markup Language
Efficient Evaluation of XQuery over Streaming Data
Querying and Transforming XML Data
Introduction to Scripting
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Intro to PHP & Variables
Compiler Construction
Compiler Construction
Presentation transcript:

Static Typing in XQuery Based on Xquery from the Experts [Katz], chap 4 Presented by Iris Rudner

Intro  Xquery implementations must support dynamic typing, and may support static typing.  Objective: How to use the Xquery type system to write type-safe queries, how to understand and resolve type errors and how to work around the type system when necessary.

The Benefits of Static Typing  Can help by detecting common type errors during static analysis instead of being discovered only when the program is run.  A type error occurs when the type of an expression is not compatible with the type required by the context in which the expression is used.

 Static typing is conservative. For example: expression of type element(surgeon) | element(plumber) in a context requiring element(surgeon)  Static typing allows to detect all type errors. Cannot detect errors such as divide-by-zero or array-bounds-overflow. The Benefits of Static Typing (cont.)

An Xquery Programming Scenario XML Schema for the Relational Auction Data:

… etc.

Relational Auction (partial) Data in XML: Tom Jones B Red Bicycle

The desired output is an XHTML document that can be rendered by a browser :

XML Schema for XHTML: <xs:schema targetNamespace=" xmlns:xs=" xmlns = "

XML Schema for XHTML – cont. : …

Xquery: [1] default element namespace = " [2] declare namespace x = " [3] define variable $base := xs:anyURI(" [4] define function makeURI ($resource as xs:string) as xs:anyURI { [5] xs:anyURI(fn:concat(xs:string($base), $resource)) [6] } [7] let $auction := $input/auction [8] return [9] [10] [11] Auctions [12] [13] [14] Item Name [15] Seller [16] Last Bid [17] Closes On [18] [19] { [20] for $article in $auction/articles/article[start_date <= date()]

Xquery – cont. : [21] let $last_bid := = [22] return [23] [24] [25] { data($article/anme) } [26] [27] [28] { [29] = [30] } [31] [32] { fn:data($last_bid/amount) } [33] { fn:data($last_bid/date) } [34] [35] } [36] [37] [38]

Debugging Why is the table such a mess ???

[12] … [13] [14] Item Name ….. [18] … [22] return [23] [24] [25] {data($article/anme) } [26] [27] [28] { [29] = [30] } [31] [32] { fn:data($last_bid/amount) } [33] { fn:data($last_bid/date) } [34] [35] } …. A closer look at the query: tr and td tags are inverted

Debugging – cont. Where are the Item Names ???

[25] {data($article/anme) } should be: [25] {data($article/name) } Debugging – cont.

Are we done ???

Debugging – cont. … [28] … Xquery – link to seller’s details : Lets see which descendants of article has an idref attribute …

XML Schema: … xs:anyType possibly: Line [28]: Run-time type error. Argument to function ‘makeURI’ contains more than one value.

Debugging – cont. correction: [28] It took us 4 cycles of debugging by running the query and then changing the query…

Validation  It is important to validate the formats of the input and output documents.  To enable validation – explicitly import the schemas of the input and output, and explicitly validate the input data.  No need to explicitly validate the output data, because element constructors validate the elements they create.

Validation – cont. [1] import schema default element namespace = " at “auction.xsd” [2] import schema namespace x = " at “xhtml.xsd” [2a] default validation strict … [7] let $auction := validate { $input } The query will raise a run-time error of the input document does not conform to the auction schema or if the output document does not conform to the XHTML schema. Requires a declaration for the element

When running our query we get the error message: Validation error: Missing ‘x:head’ element in x:html element. Lets look at the XHTML schema: … … Whereas the query: … [9] [10] ….

Validation – cont. How would validation affect our earlier debugging cycles?  Detects the inverted tr and td tags.  Fails to detect the misspelled element name.  Fails to detect the error which occurs for input data in which description elements contain idref attributes.

Static Typing  Validation detects the presence of certain types of error in the format of the data. Static typing guarantees their absence.  It can spot errors during static analysis rather than at run-time.  It can detect many errors at once.  Not dependant on the input.

Static Typing Line [10]: Element ‘x:body: encountered where element ‘x:head’ expected. Line [13]: Element ‘x:td: encountered where element ‘x:tr’ expected. Line [23]: Element ‘x:td: encountered where element ‘x:tr’ expected. Line [25]: Expression ‘$article/anme’ is always empty. Line [28]: Argument to function ‘makeURI’ has actual type | xs:anySimpleType) )+ instead of expected type ‘xs:string’. The result of static typing our query:

Getting Started with Types Coming up: How the type system assigns a type to different kinds of expressions. Including: literals & operators, variables, function calls, conditionals, paths, FLWORs and element constructors.

XML Schema and Xquery types  Xquery’s type system is based on XML Schema.  But, it is simpler and more concise, for the sake of type checking. More on the relationship between XML Schema and Xquery’s type system in the following lecture (chap 5).

XML Schema and Xquery types XML Schema – global element: formal type notation: define element user of type User define element rating of type xs:string  In XML Schema, element declarations may be global, at the top level, or local, nested within a type declaration.

XML Schema and Xquery types – cont. XML Schema – local elements, unnamed types & complex types: <xs:element name="first“ type="xs:string“ minOccurs="0"/> formal type notation: define type User { attribute id of type xs:ID, element name of type AnonymousType1, element rating ? } define type AnonymousType1{ element first of type xs:string ?, element last of type xs:string }

XML Schema and Xquery types – cont. XML Schema – simple type: formal type notation: define type IntegerList {xs:integer + } In the formal type notation:  List types are formed using ?, + & *.  Union types are formed using the choice operator |.

XML Schema – the NewUser type derived by restriction of the type User : formal type notation – type declarations for NewUser : define type NewUser restricts User { attribute id of type xs:ID, element name of type AnonymousType2, element rating } define type AnonymousType2 { element first of type xs:string, element last of type xs:string } define element newuser of type NewUser

XML Schema and Xquery types – cont. define function user-name($user as element(*,user)) as xs:string {…}  Can be applied to either a user or newuser element:  All types are derived from xs:anyType.  All simple types are derived from xs:anySimpleType.

Values  Xquery expression evaluates to a value in the Xquery data model.  Every Value is a sequence of individual items.  An item is either an atomic value or a node.  Every atomic value, element node and attribute node has an associated Xquery type.  The process of XML Schema validation specifies how elements and attributes are labeled with types.

Values – cont. XML input document: Mary Doe A Xquery values representation: element user of type User { attribute id of type xs:ID { “U02” }, element name of type AnonymousType1{ element first of type xs:string {“Mary”}, element last of type xs:string {“Doe”}, }, element rating of type xs:string { “A” } } If we apply fn:data to the first element, we get the value xs:string(“Mary”).

Relating Values and Types  Types are used ay analysis time and at evaluation time.  At evaluation time, the value of a query depends upon the types that label values.  It’s possible to refer to type labels explicitly: //element(*, x:Block.type)  Both evaluation and type assignment proceed bottom-up.

Literals and Operators How does the static type system assigns types to expressions?  Literals: “hello” has type xs:string 42 has type xs:integer has type xs:decimal4.2e1 has type xs:double  arithmetic operations: has type xs: integer has type xs: decimal order of promotion: xs:integer -> xs:decimal ->xs:float -> xs:double

Variables let $z := 1+2 return $z+$z has type xs:integer let $x as xs:integer := 1+2 return $x+$x has type xs:integer  Implicit:  Explicit: let $x := xs:double(1+2) return $x+$x has type xs:double let $x as xs:decimal := 1+2 return $x+$x has type xs: decimal let $x as xs:double := 1+2 return $x+$x is a static type error xs:integer is not a subtype of xs:double

Functions  function call: fn:concat(“ “1001”) has type xs:string  function signature: fn:concat( $op1 as xs:string ?, $op2 as xs:string ? ) as xs:string fn:concat( (), “1001”) has type xs:string The argument type must derive from the required type or can be promoted to it: fn:concat(10e1, “1001”) is a static type error

unlike arithmetic operators, there’s no notion of promotion: Conditionals if ($x<$y) then 3 else 4 has type xs:integer if ($x<$y) then “three” else 4.00 has type ( xs:string | xs:decimal ) if ($x<$y) then 4.2e1 else 69 has type ( xs:double | xs:integer ) unless arithmetic is performed on an operand expression of type ‘union’: (if ($x<$y) then 4.2e1 else 69) has type ( xs:double | xs:decimal )

Path Expressions $auction/articles has type element(articles) $auction/articles/article has type element(articles)+ $auction/itesm/article is a static type error (has type empty()) assume $auction has type element(auction) : $auction/(articles | users) has type ( element(articles) | element(users) )+ $auction/(articles | users) doesn’t have type ( element(users), element(articles) )

Predicates $auction/articles/article[start_date <= date()] has type element(article)* = $id] has type element(article)* = $id]) has type element(article) = $id]) has type element(article)? = $id]) has type element(article)+

FLWOR Expressions for $article in $articles return $article/end_date - $article/start_date has type xdt:dayTimeDuration+ assume $articles has type element(article)+ : for $article in $articles[start_date <= current_date()] return $article/end_date - $article/start_date has type xdt:dayTimeDuration*

Element Construction  Objective: how to construct data.  Simplest way – include literal XML data in a query : Red Bycicle has type element(article) At evaluation time, the element is constructed and then validated.

Element Construction – cont.  Using computed data in a constructor: define function bargain ($a as element(article)) as element(article) { { $a/name, $a/seller, { fn:current-date() } { fn:current-date() + ($a/end_date - $a/start_date) } { $a/reserve_price * 0.80 } } has type element(article)

Element Construction – cont.  Using computed data in a constructor – bad example: define function bargain ($a as element(article)) as element(article){ { $a/name, $a/seller, { fn:current-date() } { $a/end_date - $a/start_date } { $a/reserve_price * 0.80 } } the type system detects 2 errors. no id attribute type expected for end_date element is xs:date. the type of the expression is xs:dayTimeDuration.

Validation Context let $first := "Mary", $last := "Doe", $rating := "A“ return { if (fn:string-length($first) > 10) then () else { $first } } { $last } { if ($rating > "C") then () else { $rating } } validated in global context validated in user context validated in user/name context  validation context specified by lexical scope of constructors:

Validation Context – cont. let $first := validate context user/name { Mary }, $last := validate context user/name { Doe }, $rating := validate context user { A } return { if (fn:string-length($first) > 10) then () else $first,$last } if ($rating > "C") then () else $rating }  validation context specified explicitly in validate expression: the first, last & rating elements are no longer lexically nested within the user and name elements => validation context must be explicitly given.

Conclusions What have we seen?  The benefits of static typing.  How Xquery infers the type for each expression during static analysis.  Common errors that can be detected by static typing, including misspelled names in path expressions and constructors.