XPath and XQuery - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

XPath and XQuery

Description:

Contestant Name = 'Shania Twain' Hometown = 'Timmins' Repertoire SongRef S004 /SongRef ... Composer Shania Twain /Composer /Song Song SongID = 'S004' ... – PowerPoint PPT presentation

Number of Views:381
Avg rating:3.0/5.0
Slides: 17
Provided by: syl769
Category:
Tags: shania | twain | xpath | xquery

less

Transcript and Presenter's Notes

Title: XPath and XQuery


1
XPath and XQuery
  • Sylvia Osborn

2
Well use this example
  • "Timmins"
  • S004
  • S005
  • S003
  • S005
  • S003
  • S004

3
With
  • Bah Bah Black Sheep
  • Mother Goose
  • Band on the Run
  • Paul McCartney
  • Come on Over
  • Shania Twain
  • Forever and For
    Always
  • Shania Twain

4
An Sample XML Document
  • Schema-instance" xsinoNamespaceSchemaLocation"Z
    \853\exampleschema.xsd"
  • John Doe
  • 123456
  • 414
  • Pine Street
  • London
  • Bob Day
  • 9876543212
  • Box 255
  • London

5
XPATH
  • XPath provides a path-expression type way of
    taking a document and returning a set of nodes
  • A path always returns a sequence of distinct
    nodes in document order
  • A path expression consists of a series of steps
    separated by the / character
  • can give absolute path expressions
    /childOfRoot/childOfThat/etc
  • / represents the root of the XML document
  • . represents the current node
  • .. represents the parent of the current node
  • _at_ is used to access attributes
  • /child2 gives the second child in document order

6
Xpath contd
  • //tag selects this tag no matter where it appears
    in the document
  • can do comparisons at various places
  • //studentcontains(Name, John/AddressCity
    London"
  • Or
  • //studentName"John Doe"/AddressCity"London"
  • the last thing before the last predicate is
    what is returned
  • if an attribute is optional, then _at_Hometown is
    true if the Hometown is present
  • one drawback with XPath is that it cannot do
    join-like queries
  • there are functions that can be used, e.g.
    concatenation, contains, starts-with
  • can be embedded in an xpointer to make a smart
    url http//www.uwo.ca/Document.xmlxpointer(a
    n xpath expression)

7
Some examples with our 2 students(done with
Galax)
  • students//City
  • Gives
  • London, CityLondon
  • students//Name..//City"London"
  • Gives
  • John Doe, Bob Day
  • students//student.//StName"Pine Street"
  • Gives
  • John Doe
  • 12345678
  • 414
  • Pine Street
  • London

8
More XPath examples
  • students//studentcontains(.//Name,"Bob")
  • Gives
  • Bob Day
  • 98765432
  • Box 255
  • London
  • students//student/Addresscontains(..//Name,"Bob"
    )
  • Gives
  • Box 255
  • London

9
XQuery
  • Designed to be closed they defined a query data
    model and set of operators so that closure is
    satisfied
  • Includes XPath 1.0 as a syntactic subset.
  • A value is an ordered sequence of zero or more
    items.
  • An item is a node or an atomic value.
  • There are 7 kinds of nodes document nodes,
    element nodes, attribute nodes, text nodes,
    comment nodes, processing instruction nodes, and
    namespace nodes.
  • Nodes have identity (atomic values dont)

10
More about Values
  • There is no distinction between an item and a
    value of length one.
  • There are no nested sequences.
  • There is no null value
  • A sequence can be empty.
  • All sequences are ordered
  • Sequences can contain heterogeneous values.

11
General XQuery Rules
  • XQuery is a case-sensitive language
  • Keywords are in lower case
  • Every expression has a value and no side effects
  • Expressions are fully composable
  • Comments look like this
  • ( here is a comment )
  • A variable looks like this x

12
Constructors
  • Can use XML-like syntax if you have all the
    values
  • Huckleberry Finn
  • If the content of an attribute or element must be
    computed, use an expression enclosed in , with
    variables inside
  • b/title

13
FLWOR Expressions
for var in expr
return expr
order by expr
let var expr
where expr
For and let clauses generate a list of tuples of
bound variables, preserving input document
order. Where clause applies a predicate,
eliminating some tuples. Order By clause imposes
an order on the surviving tuples. Return clause
is executed for each surviving tuple, generating
an ordered list of outputs.
14
Difference between for and let
  • Quoted from http//www.w3.org/TR/xquery/id-for-le
    t
  • The for clause iterates over the items in the
    binding sequence, binding the variable to each
    item in turn.
  • Unlike a for clause, however, a let clause binds
    each variable to the result of its associated
    expression, without iteration. If there are no
    for clauses, the let clauses generate one tuple
    containing all the variable bindings.

15
Example XQuery
  • for c in contestants//Contestant
  • let s songs//Song_at_SongID
    c//Performance/SongRef
  • return
  • c/_at_Name
  • s//Title

16
Result is
  • Come on Over
  • Forever and For Always
  • Thank You Baby
  • ,
  • Bah Bah Black Sheep
  • Band on the Run
  • Michelle
  • ,
  • I Love Paris
  • Night and Day
  • Tomorrow
  • ,
  • Band on the Run
  • Forever and For Always
  • Michelle
Write a Comment
User Comments (0)
About PowerShow.com