XQuery An XML Query Language - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

XQuery An XML Query Language

Description:

XQuery An XML Query Language – PowerPoint PPT presentation

Number of Views:190
Avg rating:3.0/5.0
Slides: 32
Provided by: samuelr1
Category:
Tags: xml | language | query | surfing | wind | xquery

less

Transcript and Presenter's Notes

Title: XQuery An XML Query Language


1
XQueryAnXML Query Language
  • Samuel R. Collins

2
Outline
  • Introduction
  • XML Review
  • XQuery Overview
  • Derivation of XQuery
  • XQuery Expressions
  • Examples
  • Questions and Answers

3
What is XML?
  • The Extensible Markup Language (XML)
  • A subset of SGML
  • XML is a family of technologies
  • Namespaces
  • XML Schema
  • XML Query
  • XLink
  • XPointer
  • XSL
  • XPATH

4
How is XML Used?
  • XML is an extremely versatile markup language,
    capable of labeling the information content of
    diverse data sources including
  • Structured and semi-structured documents
  • Relational databases
  • Legacy databases
  • Object repositories
  • Database views
  • Electronic data interchange (EDI)
  • Facilitate B2B communication
  • Data exchange between data sources

5
Why Not SQL or OQL?
  • XML Data is different from relational and
    object-oriented data
  • In the relational and object-oriented models,
    every data instance has a schema, which is
    separate from and independent of the data.

6
Why Not SQL or OQL?
  • In XML, the schema exists with the data
  • XML data is self describing
  • XML data can be parsed without a DTD or an XML
    Schema

7
XML Query Language
  • A tool for structural and content-based query
  • Allows an application to extract information from
    one or several data sources

8
XQuery
  • XQuery is designed to be broadly applicable
    across all types of XML data sources.
  • It is designed to be a small, easily
    implementable language in which queries are
    concise and easily understood.
  • It is also flexible enough to query a broad
    spectrum of XML information sources, including
    both databases and documents.
  • The Query Working Group has identified a
    requirement for a human-readable query syntax and
    XQuery is designed to meet this requirement.

9
XQuery Derivation
  • XQuery is derived from an XML query language
    called Quilt, which in turn borrowed features
    from several other languages.
  • From XPath and XQL it took a path expression
    syntax suitable for hierarchical documents.
  • From XML-QL it took the notion of binding
    variables and then using the bound variables to
    create new structures.
  • From SQL it took the idea of a series of clauses
    based on keywords that provide a pattern for
    restructuring data (the SELECT-FROM-WHERE pattern
    in SQL).
  • From OQL it took the notion of a functional
    language composed of several different kinds of
    expressions that can be nested with full
    generality.
  • Quilt was also influenced by other XML query
    languages such as Lorel and YATL.

10
Additional Features of XQuery
  • XQuery has a type system that is based on XML
    Schema.
  • XQuery relies on path expressions for navigating
    in hierarchic documents.

11
XQuery Expressions
  • The principal forms of XQuery expressions are as
    follows
  • Path expressions
  • Element constructors
  • FLWR expressions
  • Expressions involving operators and functions
  • Conditional expressions
  • Quantified expressions
  • Expressions that test or modify datatypes

12
Path Expressions
  • One of the forms of an XQuery expression is a
    path expression, based on the syntax of XPath.
  • XPath is a notation for navigating along "paths"
    in an XML document, and is used in several
    XML-related applications including XSLT and
    XPointer.

13
Path Expression Example
  • The following example uses a path expression
    consisting of three steps.
  • The first step locates the root node of a
    document.
  • The second step locates the second chapter of the
    document
  • The third step finds figure elements occurring
    anywhere within the chapter, but retains only
    those figure elements that have a caption with
    the value "Tree Frogs."

14
Path Expression Example
  • (Q1) In the second chapter of the document named
    "zoo.xml", find the figure(s) with caption "Tree
    Frogs".
  • document("zoo.xml")//chapter2//figurecap
    tion "Tree Frogs"

15
Path Expression Example
  • XPath allows a node to be selected from a
    sequence of nodes by specifying its ordinal
    number in the sequence
  • (Q2) Find all the figures in chapters 2 through 5
    of the document named "zoo.xml."
  • document("zoo.xml")//chapter2 TO 5//figure

16
Element Constructors
  • A query often needs to generate new elements. The
    simplest way to generate a new element is to
    embed the element directly in a query using XML
    notation.
  • (Q7) Generate an element that has an
    "empid" attribute and nested and
    elements.
  • John Smith
  • Anthropologist

17
Element Constructors
  • (Q8) Generate an element that has an
    "empid" attribute. The value of the attribute and
    the content of the element are specified by
    variables that are bound in other parts of the
    query.
  • name
  • job

18
FLWR Expressions
  • A FLWR (pronounced "flower") expression is
    constructed from FOR, LET, WHERE, and RETURN
    clauses, which must appear in a specific order.
  • A FLWR expression binds values to one or more
    variables and then uses these variables to
    construct a result.
  • The overall flow of data in a FLWR expression is
    illustrated in Figure 1.

19
FLWR Expressions
  • The first part of a FLWR expression consists of
    FOR-clauses and/or LET-clauses, which serve to
    bind values to one or more variables.
  • A FOR-clause is used whenever iteration is
    needed.
  • The result of the FOR-clause is a sequence of
    tuples, each of which contains a binding for each
    of the variables in the FOR-clause.
  • A LET-clause is also used to bind one or more
    variables to one or more expressions. Unlike a
    FOR-clause, however, a LET-clause simply binds
    each variable to the value of its respective
    expression
  • A FLWR expression may contain several FOR and
    LET-clauses. Expressions used in FOR and
    LET-clauses may contain references to variables
    bound earlier in the FLWR expression. The result
    of the FOR and LET clauses is an ordered sequence
    of tuples of bound variables.

20
FLWR Expressions
  • The binding-tuples generated by the FOR and LET
    clauses are subject to further filtering by an
    optional WHERE-clause.
  • Only those tuples for which the condition in the
    WHERE-clause is true are used to invoke the
    RETURN clause.
  • The WHERE-clause may contain several predicates,
    connected by AND and OR. These predicates usually
    contain references to the bound variables.
  • The ordering of the binding-tuples generated by
    the FOR and LET clauses is preserved by the
    WHERE-clause.

21
FLWR Expressions
  • The RETURN-clause generates the output of the
    FLWR expression, which may be any sequence of
    nodes or primitive values.
  • The RETURN-clause is executed once for each tuple
    of bindings that is generated by the FOR and
    LET-clauses and satisfies the condition in the
    WHERE-clause, preserving the order of these
    tuples.
  • The results generated by the individual
    executions of the RETURN clause are concatenated
    together, preserving their order.

22
FLWR Expressions
  • (Q11) List the titles of books published by
    Morgan Kaufmann in 1998.
  • FOR b IN document("bib.xml")//book
  • WHERE b/publisher "Morgan Kaufmann
  • AND b/year "1998"
  • RETURN b/title

23
FLWR Expressions
  • (Q12) List each publisher and the average price
    of its books.
  • FOR p IN distinct(document("bib.xml")//publisher
    )
  • LET a avg(document("bib.xml")//bookpublisher
    p/price)
  • RETURN
  • p/text()
  • a

24
Sorting
  • A sequence can be ordered by means of a SORTBY
    clause that contains one or more "ordering
    expressions."
  • (Q17)List all books with price greater than 100,
    in order by first author within each group of
    books with the same first author, list the books
    in order by title.
  • document("bib.xml")//bookprice 100 SORTBY
    (author1, title)

25
Operators and Expressions
  • Arithmetic operators
  • AdditiveExpr      Expr ("" "-")
  • ExprMultiplicativeExpr      Expr ("" "div"
    "mod") Expr
  • UnaryExpr      ("-" "") Expr
  • Comparison operators
  • EqualityExpr      Expr ("" "!" ""
    "!") Expr
  • RelationalExpr      Expr (""
    "") Expr
  • Logical operators
  • OrExpr      Expr "or" Expr
  • AndExpr      Expr "and" Expr

26
Operators and Expressions
  • Sequence-related Operators
  • ParenthesizedExpr      "(" ExprSequence? ")"

  • ExprSequence      Expr ("," Expr)
  • RangeExpr      Expr "to" Expr
  • UnionExpr      Expr ("union" "") Expr
  • IntersectExceptExpr      Expr ("intersect"
    "except") Expr
  • BeforeAfterExpr      Expr ("before"
    "after") Expr

27
Operators and Expressions
  • (Q19) Prepare a "critical sequence" report
    consisting of all elements that occur between the
    first and second incision in the first procedure.
  • LET p //procedure1
  • FOR e IN // AFTER (p//incision)1
  • BEFORE (p//incision)2
  • RETURN shallow(e)
  • The shallow function makes a shallow copy of a
    node, including attributes but not including
    subelements.

28
Conditional Expressions
  • Syntax
  • IfExpr      "if" "(" Expr ")" "then" Expr
    "else" Expr
  • (Q21) Make a list of holdings, ordered by title.
    For journals, include the editor, and for all
    other holdings, include the author.
  • FOR h IN //holding
  • RETURN
  • h/title,
  • IF (h/_at_type "Journal")
  • THEN h/editor
  • ELSE h/author
  • SORTBY (title)

29
Quantified Expressions
  • The "some" expression uses an existential
    quantifier, and the "every" expression uses a
    universal quantifier.
  • SomeExpr      "some" Variable "in" Expr
    "satisfies" Expr
  • EveryExpr      "every" Variable "in" Expr
    "satisfies" Expr
  • (Q22) Find titles of books in which both sailing
    and windsurfing are mentioned in the same
    paragraph.
  • FOR b IN //book
  • WHERE SOME p IN b//para SATISFIES (contains(p,
    "sailing") AND contains(p, "windsurfing"))
  • RETURN b/title

30
Datatytpes
  • XQuery has a type system that is based on XML
    Schema. By using the datatype names defined in
    the namespace http//www.w3.org/2001/XMLSchema
    (hereafter abbreviated as xsd), all the primitive
    and derived datatypes of XML Schema can be used
    in queries.
  • Other complex types declared using XML Schema can
    also be referred to by their qualified names.
  • Literal values of XML Schema types other than
    string, integer, decimal, and float can be
    specified by means of constructor functions such
    as
  • true()
  • false()
  • date("2000-06-25")
  • or by cast expressions such as CAST AS
    xsdpositiveInteger(47).

31
Any Questions
http//www.w3.org/TR/2001/WD-xquery-20010607
Write a Comment
User Comments (0)
About PowerShow.com