25
1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

1

Dickson K.W. ChiuPhD, SMIEEE

Thanks to Prof. Francis Lau (HKU)

CSIT600b: XML ProgrammingXlink, XPointer, XQuery

Page 2: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-2

Problem of HTML Linking

Growth of the “Web” attributed to the ability of an HTML page to (hyper)link to another page <a href=http://www.ust.hk/> …

Limitations Involves only two resources One-way (the “back” button is a browser’s thing) The source must be HTML The whole document is loaded even if your link

points to a section <img src=“http://www.dot.com#jpg5”> … The target page must contain explicit section markups

Page 3: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-3

XML Linking XLink to link resources, XPointer to point to

fragments of XML documents Tutorial: http://

zvon.org/xxl/xlink/OutputExamples/xlinksimple_intro.html

W3C Recommendation 1.0, Jun 2001 Example

One-way and two-way One link, involving 4 resources Information about this link

can be in a completely separated file

A.html

C.htmlD.xml

B.xml

Page 4: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-4

XLink

Example <name xmlns=“http://dot.com/name”

xmlns:xlink=“http://www.w3.org/1999/xlink”><first>Tommy</first><last xlink:type=“simple” xlink:href=“http://dot.com/~tlee”>Lee</last></name>

XLink’s global attributes type, href, label, role, arcrole, title, show, actuate,

from, to Can be added to elements that are in any

arbitrary namespace

Page 5: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-5

The “type” Attribute

Mandatory, to define a link Six types (the first 2 are link types, the

others are for providing more info about a link) Simple Extended Title – to provide human-readable titles Locator – to indicate remote resources Resource – to define local resources

involved Arc – to indicate the rules for traversing

Page 6: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-6

“ title”, “label”, and “role”

title and label are used to label the functions of various resources in the link label is machine-readable, title human-readable <last xlink:label=“lastName”

xlink:type=“simple” xlink:href=“people.xml” xlink:title=“Last name” xlink:role=“http://dot.com/names”>Last name</last>

role is a URI that references a resource that describes the meaning of the link

Page 7: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-7

“ actuate” and “show”

The actuate attribute specifies when the resource should be retrieved

onLoad (default) onRequest

The show attribute specifies how to display the resource when it is loaded

new – new window replace – current window embed (default) – insert here like <html:img src=...

> The application can provide other values,

using “QNames”, but the application should know how to handle them

xlink:actuate=“op:EveryFiveMinutes”

Page 8: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-8

“ from” and “to”

A link can involve multiple resources, and be traversed in multiple directions <element

xlink:from=“myapp:first” xlink:to=“myapp:last” xlink:show=“replace” xlink:actuate=“onRequest”> Click for last</element>

When clicked, the first link resource will be replaced by the last link resource

Page 9: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-9

Simple Links

Like HTML links: 2 resources <element xlink:type=“simple”

xlink:href=… xlink:show=“new”>Click me!</element>

Simple links are “inline” links because the link’s own content (i.e., “Click me”) is the resource

Page 10: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-10

Extended Links An element to describe links among sets of document(s),

classified by labels Extended links can be “out of line”

You could create a link from A to B even though you have no access rights for A and B

Locator-type to indicate remote resources, and arc-type to specify rules for traversals

Attribute arcrole is used to describe the relationship between the resources

Page 11: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-11

Extended Links Example<extendedlink xlink:type="extended"> <loc xlink:type="locator" xlink:href="..." xlink:label="parent" xlink:title="p1" /> <loc xlink:type="locator" xlink:href="..." xlink:label="parent" xlink:title="p2" /> <loc xlink:type="locator" xlink:href="..." xlink:label="child" xlink:title="c1" /> <loc xlink:type="locator" xlink:href="..." xlink:label="child" xlink:title="c2" /> <loc xlink:type="locator" xlink:href="..." xlink:label="child" xlink:title="c3" /> ... <!-- arc-type elements would go here --></extendedlink>

<go xlink:type=“pararc" xlink:from="parent" xlink:to="child" />

links from parents to children (6 links total) <go xlink:type=“childarc" xlink:to="child" />

links from parents to children and children to children (15 links total)

Upon clicking on a item, say “p1”, the rendering software (need not be a browser) displays a list of linked items for the user to choose.

Page 12: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-12

XPointer

Provides a method for pointing to pieces of an XML document

W3C Recommendation 1.0, Mar 2003 Built on XPath, with the following extensions

Locate information by string matching Can be appended to URI’s Address not only entire nodes, but pieces of nodes in

an XML document Finer than nodes

A location is any node type allowed in Xpath A point is any spot in the XML document A range is defined by two points

Page 13: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-13

XInclude and XBase

XML 1.0 / 1.1 does not have “include’s” XInclude uses XPointer to include

portions of another document The parse attribute has value xml

(parsed) or text (unparsed) XBase is for providing base URI’s for

relative links in a document

Page 14: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-14

XQuery XQuery is a language for querying XML data XQuery is built on XPath expressions XQuery for XML is like SQL for databases http://www.w3c.org/XML/Query Updated release of the XQuery 1.0, XPath 2.0:

W3C Oct 2004 XQuery is not an XML language - a version in

XML syntax is called XQueryX. Tutorial booklet download from DataDirect:

http://www.datadirect.com/news/whatsnew/xquerybook/index.ssp

Brief tutorials: http://www.brics.dk/~amoeller/XML/index.html http://www.w3schools.com/xquery/default.asp

Page 15: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-15

XQuery W3C Requirements at least one XML syntax (at least one human-readable

syntax) must be declarative must be protocol independent must respect XML data model must be namespace aware must coordinate with XML Schema must work even if schemas are unavailable must support simple and complex datatypes must support universal and existential quantifiers must support operations on hierarchy and sequence of

document structures must combine information from multiple documents must support aggregation must be able to transform and to create XML structures must be able to traverse ID references

Page 16: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-16

XQuery Basic Concepts A query in XQuery is an

expression that: reads a sequence of XML

fragments or atomic values returns a sequence of XML

fragments or atomic values The principal forms of XQuery

expressions are: path expressions (same as

XPath) element constructors FLWOR ("flower") expressions list expressions conditional expressions quantified expressions datatype expressions

Expressions are evaluated relative to a context:

namespaces variables functions date and time context item (current

node or atomic value) context position (in the

sequence being processed)

context size (of the sequence being processed)

Page 17: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-17

XQuery Path Expressions

The simplest kind of query is just an XPath 2.0 expression.

Example: document("recipes.xml")//

recipe[title="Ricotta Pie"]//ingredient[@amount]

Some XQuery specific extension of XPath: location steps may follow a new IDREF axis an arbitrary XQuery expression may be

used as a location step

Page 18: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-18

XPath 2.0 Major Changes from 1.0

now using XML Schema primitive types instead of just node-set, boolean, number, string

new type operators: cast, treat, assert, instance of

now using sequences instead of node-sets also allow non-node types new operators: for, if, some, every, intersect, except

many new functions regular expression match/replace/tokenize date formats ...

Page 19: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-19

XQuery FLWOR expressions The main engine of XQuery is FLWOR expression

For-Let-Where-Order-Return pronounced "flower" generalizes SELECT-FROM-HAVING-WHERE from SQL

Examplefor $d in document("depts.xml")//deptno let $e := document("emps.xml")//employee[deptno = $d] where count($e) >= 10 order by avg($e/salary) descending return <big-dept> { $d, <headcount>{count($e)}</headcount>,

<avgsal>{avg($e/salary)}</avgsal> }</big-dept>

Only 1 where

For – loopLet – binding once

{ … } – list expression

Page 20: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-20

XQuery FLWOR Join Example Sufficient to compute joins of documents –

example:

for $p IN document("www.irs.gov/taxpayers.xml")//personfor $n IN document("neighbors.xml")//neighbor[ssn =

$p/ssn]return <person> <ssn> { $p/ssn } </ssn> { $n/name } <income> { $p/income } </income> </person>

Page 21: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-21

XQuery List Expression XQuery expressions manipulate lists of values,

for which many operators are supported: constant lists: (7, 9, <thirteen/>) integer ranges: i to j XPath expressions, like all named children of the context

node: name concatenation: , set operators: | (or union), intersect, except functions: remove, index-of, count, avg, max, min,

sum, distinct-values ... When lists are viewed as sets:

XML nodes are compared on node identity duplicates are removed the order is preserved

Page 22: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-22

XQuery Qualified Expression some-in-satisfies

for $b in document("bib.xml")//bookwhere some $p in $b//paragraph satisfies (contains($p,"sailing") AND contains($p,"windsurfing"))return $b/title

every-in-satisfies for $b in document("bib.xml")//bookwhere every $p in $b//paragraph satisfies contains($p,"sailing")return $b/title

Page 23: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-23

XQuery Datatype Expression 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 a query.

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.

Page 24: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-24

XQuery - Other Features Hundreds of built-in operators and functions -

contains anything you might think of Computed element and attribute names - allow

more flexible queries User-defined functions - allow general-purpose

computations Views and updates are still under development

Page 25: 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming Xlink, XPointer, XQuery

Dickson Chiu-2004 CSIT600b 3-25

Status (updated) Software? Starting to come up http://www.w3.org/XML/Linking

XLink 1.0 and Xbase 1.0 - June 27, 2001 XPointer 1.0 - 25 March 2003

XInclude 1.0 – W3C Working Draft 10 Nov 2003

Updated release of the XQuery 1.0, XPath 2.0, Oct 2004

Working draft Last call working draft Candidate recommendation

Proposed recommendation Recommendation