226
NET 2 Transforming between RDF and XML with XSPARQL Net2 Tutorial Nuno Lopes December, 2010

XSPARQL Tutorial

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: XSPARQL Tutorial

NET 2

Transforming between RDF andXML with XSPARQL

Net2 Tutorial

Nuno Lopes

December, 2010

Page 2: XSPARQL Tutorial

Integration of Heterogeneous Sources

SPARQL

XSLT/XQuery

Transformations between XML and RDF are not easy, mainlydue to the heterogeneity of RDF/XML serialisations

Objective: language capable of integrating heterogeneoussources for the Semantic Web

1 / 51

Page 3: XSPARQL Tutorial

Integration of Heterogeneous Sources

SPARQL

XSLT/XQuery

Transformations between XML and RDF are not easy, mainlydue to the heterogeneity of RDF/XML serialisations

Objective: language capable of integrating heterogeneoussources for the Semantic Web

1 / 51

Page 4: XSPARQL Tutorial

Integration of Heterogeneous Sources

SPARQL

XSLT/XQuery

Transformations between XML and RDF are not easy, mainlydue to the heterogeneity of RDF/XML serialisations

Objective: language capable of integrating heterogeneoussources for the Semantic Web

1 / 51

Page 5: XSPARQL Tutorial

Integration of Heterogeneous Sources

SPARQL

XSLT/XQuery

Transformations between XML and RDF are not easy, mainlydue to the heterogeneity of RDF/XML serialisations

Objective: language capable of integrating heterogeneoussources for the Semantic Web

1 / 51

Page 6: XSPARQL Tutorial

Integration of Heterogeneous Sources

SPARQL

XSLT/XQuery

Lowering

Lifting

Transformations between XML and RDF are not easy, mainlydue to the heterogeneity of RDF/XML serialisations

Objective: language capable of integrating heterogeneoussources for the Semantic Web

1 / 51

Page 7: XSPARQL Tutorial

Integration of Heterogeneous Sources

SPARQL

XSLT/XQuery

Transformations between XML and RDF are not easy, mainlydue to the heterogeneity of RDF/XML serialisationsObjective: language capable of integrating heterogeneoussources for the Semantic Web

1 / 51

Page 8: XSPARQL Tutorial

Why are such transformations needed? (I)

Standards in Health Care and Life Sciences

Possible solution for the heterogeneous message formats

Store your data in RDF

Convert it to the required XML format when necessary

2 / 51

Page 9: XSPARQL Tutorial

Why are such transformations needed? (I)

Standards in Health Care and Life Sciences

Possible solution for the heterogeneous message formats

Store your data in RDF

Convert it to the required XML format when necessary

2 / 51

Page 10: XSPARQL Tutorial

Why are such transformations needed? (I)

Standards in Health Care and Life Sciences

Possible solution for the heterogeneous message formats

Store your data in RDF

Convert it to the required XML format when necessary

2 / 51

Page 11: XSPARQL Tutorial

Why are such transformations needed? (II)

Creating (X)HTML from an RDF backend (under development):http://musicpath.org/

3 / 51

Page 12: XSPARQL Tutorial

Why are such transformations needed? (II)

Creating (X)HTML from an RDF backend (under development):http://musicpath.org/

3 / 51

Page 13: XSPARQL Tutorial

Why are such transformations needed? (II)

Creating (X)HTML from an RDF backend (under development):http://musicpath.org/

3 / 51

Page 14: XSPARQL Tutorial

Toy example for the tutorial

knows knows

knows

4 / 51

Page 15: XSPARQL Tutorial

Toy example for the tutorial

knows knows

knows

4 / 51

Page 16: XSPARQL Tutorial

Why XQuery/XSLT is not enough:

Different syntaxes and serialisations for the same RDF graph:

@prefix alice: <alice/> .

@prefix foaf: <...foaf/0.1/> .

_:b1 rdf:type foaf:Person;

foaf:knows _:b2.

_:b2 rdf:type foaf:Person;

foaf:name "Bob".

<rdf:RDF xmlns:foaf="...foaf/0.1/">

<foaf:Person>

<foaf:knows>

<foaf:Person foaf:name="Bob"/>

</foaf:knows>

</foaf:Person>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="b1">

<rdf:type

rdf:resource=".../Person"/>

<foaf:knows rdf:nodeID="b2"/>

</rdf:Description>

<rdf:Description rdf:nodeID="b2">

<rdf:type

rdf:resource=".../Person"/>

<foaf:name>Bob</foaf:name>

</rdf:Description>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="x">

<foaf:knows rdf:nodeID="y"/>

</rdf:Description>

<rdf:Description rdf:nodeID="x">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<foaf:name>Bob</foaf:name>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

</rdf:RDF>

5 / 51

Page 17: XSPARQL Tutorial

Why XQuery/XSLT is not enough:

Different syntaxes and serialisations for the same RDF graph:

@prefix alice: <alice/> .

@prefix foaf: <...foaf/0.1/> .

_:b1 rdf:type foaf:Person;

foaf:knows _:b2.

_:b2 rdf:type foaf:Person;

foaf:name "Bob".

<rdf:RDF xmlns:foaf="...foaf/0.1/">

<foaf:Person>

<foaf:knows>

<foaf:Person foaf:name="Bob"/>

</foaf:knows>

</foaf:Person>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="b1">

<rdf:type

rdf:resource=".../Person"/>

<foaf:knows rdf:nodeID="b2"/>

</rdf:Description>

<rdf:Description rdf:nodeID="b2">

<rdf:type

rdf:resource=".../Person"/>

<foaf:name>Bob</foaf:name>

</rdf:Description>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="x">

<foaf:knows rdf:nodeID="y"/>

</rdf:Description>

<rdf:Description rdf:nodeID="x">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<foaf:name>Bob</foaf:name>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

</rdf:RDF>

Turtle

5 / 51

Page 18: XSPARQL Tutorial

Why XQuery/XSLT is not enough:

Different syntaxes and serialisations for the same RDF graph:

@prefix alice: <alice/> .

@prefix foaf: <...foaf/0.1/> .

_:b1 rdf:type foaf:Person;

foaf:knows _:b2.

_:b2 rdf:type foaf:Person;

foaf:name "Bob".

<rdf:RDF xmlns:foaf="...foaf/0.1/">

<foaf:Person>

<foaf:knows>

<foaf:Person foaf:name="Bob"/>

</foaf:knows>

</foaf:Person>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="b1">

<rdf:type

rdf:resource=".../Person"/>

<foaf:knows rdf:nodeID="b2"/>

</rdf:Description>

<rdf:Description rdf:nodeID="b2">

<rdf:type

rdf:resource=".../Person"/>

<foaf:name>Bob</foaf:name>

</rdf:Description>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="x">

<foaf:knows rdf:nodeID="y"/>

</rdf:Description>

<rdf:Description rdf:nodeID="x">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<foaf:name>Bob</foaf:name>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

</rdf:RDF>

TurtleTriplestore

5 / 51

Page 19: XSPARQL Tutorial

Why XQuery/XSLT is not enough:

Different syntaxes and serialisations for the same RDF graph:

@prefix alice: <alice/> .

@prefix foaf: <...foaf/0.1/> .

_:b1 rdf:type foaf:Person;

foaf:knows _:b2.

_:b2 rdf:type foaf:Person;

foaf:name "Bob".

<rdf:RDF xmlns:foaf="...foaf/0.1/">

<foaf:Person>

<foaf:knows>

<foaf:Person foaf:name="Bob"/>

</foaf:knows>

</foaf:Person>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="b1">

<rdf:type

rdf:resource=".../Person"/>

<foaf:knows rdf:nodeID="b2"/>

</rdf:Description>

<rdf:Description rdf:nodeID="b2">

<rdf:type

rdf:resource=".../Person"/>

<foaf:name>Bob</foaf:name>

</rdf:Description>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="x">

<foaf:knows rdf:nodeID="y"/>

</rdf:Description>

<rdf:Description rdf:nodeID="x">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<foaf:name>Bob</foaf:name>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

</rdf:RDF>

TurtleTriplestore

RDF/XML

RDF/XMLRDF/X

ML

5 / 51

Page 20: XSPARQL Tutorial

Why XQuery/XSLT is not enough:

Different syntaxes and serialisations for the same RDF graph:

@prefix alice: <alice/> .

@prefix foaf: <...foaf/0.1/> .

_:b1 rdf:type foaf:Person;

foaf:knows _:b2.

_:b2 rdf:type foaf:Person;

foaf:name "Bob".

<rdf:RDF xmlns:foaf="...foaf/0.1/">

<foaf:Person>

<foaf:knows>

<foaf:Person foaf:name="Bob"/>

</foaf:knows>

</foaf:Person>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="b1">

<rdf:type

rdf:resource=".../Person"/>

<foaf:knows rdf:nodeID="b2"/>

</rdf:Description>

<rdf:Description rdf:nodeID="b2">

<rdf:type

rdf:resource=".../Person"/>

<foaf:name>Bob</foaf:name>

</rdf:Description>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="x">

<foaf:knows rdf:nodeID="y"/>

</rdf:Description>

<rdf:Description rdf:nodeID="x">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<foaf:name>Bob</foaf:name>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

</rdf:RDF>

TurtleTriplestore

RDF/XML

RDF/XMLRDF/X

ML

Any transformation needs to takeinto account the different RDF/XMLrepresentations

5 / 51

Page 21: XSPARQL Tutorial

Why XQuery/XSLT is not enough:

Different syntaxes and serialisations for the same RDF graph:

@prefix alice: <alice/> .

@prefix foaf: <...foaf/0.1/> .

_:b1 rdf:type foaf:Person;

foaf:knows _:b2.

_:b2 rdf:type foaf:Person;

foaf:name "Bob".

<rdf:RDF xmlns:foaf="...foaf/0.1/">

<foaf:Person>

<foaf:knows>

<foaf:Person foaf:name="Bob"/>

</foaf:knows>

</foaf:Person>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="b1">

<rdf:type

rdf:resource=".../Person"/>

<foaf:knows rdf:nodeID="b2"/>

</rdf:Description>

<rdf:Description rdf:nodeID="b2">

<rdf:type

rdf:resource=".../Person"/>

<foaf:name>Bob</foaf:name>

</rdf:Description>

</rdf:RDF>

<rdf:RDF xmlns:foaf="...foaf/0.1/"

xmlns:rdf="...rdf-syntax-ns#">

<rdf:Description rdf:nodeID="x">

<foaf:knows rdf:nodeID="y"/>

</rdf:Description>

<rdf:Description rdf:nodeID="x">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<foaf:name>Bob</foaf:name>

</rdf:Description>

<rdf:Description rdf:nodeID="y">

<rdf:type rdf:resource=".../Person"/>

</rdf:Description>

</rdf:RDF>

TurtleTriplestore

RDF/XML

RDF/XMLRDF/X

ML

Or: end up with different transfor-mations for the same RDF data

5 / 51

Page 22: XSPARQL Tutorial

Why SPARQL is not enough:

Great for querying RDF! Easy to output Turtle orSPARQL XML results format ...

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.rdf>

where { $X vc:FN $FN .}

... but

How to produce arbitrary XML???

6 / 51

Page 23: XSPARQL Tutorial

Why SPARQL is not enough:

Great for querying RDF! Easy to output Turtle orSPARQL XML results format ...

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.rdf>

where { $X vc:FN $FN .}

... but

How to produce arbitrary XML???

6 / 51

Page 24: XSPARQL Tutorial

XSPARQL

Transformation language

XML and RDF formats (based onXQuery and SPARQL)

Lifting and Lowering in a singlelanguage

7 / 51

Page 25: XSPARQL Tutorial

XSPARQL

Transformation language

XML and RDF formats (based onXQuery and SPARQL)

Lifting and Lowering in a singlelanguage

7 / 51

Page 26: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 27: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 28: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 29: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 30: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 31: XSPARQL Tutorial

XPath

XPath is used to locate nodes in XML treesAn XPath expression is a sequence of steps separated by /.Each step evaluates to a sequence of nodes.

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

Full spec at http://www.w3.org/TR/xpath20/.Tutorial at http://www.w3schools.com/xpath/default.asp.

8 / 51

Page 32: XSPARQL Tutorial

XPath

XPath is used to locate nodes in XML treesAn XPath expression is a sequence of steps separated by /.Each step evaluates to a sequence of nodes.

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

relations nodeis the root ele-ment

Full spec at http://www.w3.org/TR/xpath20/.Tutorial at http://www.w3schools.com/xpath/default.asp.

8 / 51

Page 33: XSPARQL Tutorial

XPath

XPath is used to locate nodes in XML treesAn XPath expression is a sequence of steps separated by /.Each step evaluates to a sequence of nodes.

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

3 child elementsperson with at-tribute name

Full spec at http://www.w3.org/TR/xpath20/.Tutorial at http://www.w3schools.com/xpath/default.asp.

8 / 51

Page 34: XSPARQL Tutorial

XPath

XPath is used to locate nodes in XML treesAn XPath expression is a sequence of steps separated by /.Each step evaluates to a sequence of nodes.

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

Each person el-ement can haveknows childs

Full spec at http://www.w3.org/TR/xpath20/.Tutorial at http://www.w3schools.com/xpath/default.asp.

8 / 51

Page 35: XSPARQL Tutorial

XPath Steps

Step examples

/relations Selects the root element relations

/relations/person Selects all person elements that are children of relations

//knows Selects all knows elements (in all the document)

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

9 / 51

Page 36: XSPARQL Tutorial

XPath Steps

Step examples

/relations Selects the root element relations

/relations/person Selects all person elements that are children of relations

//knows Selects all knows elements (in all the document)

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

9 / 51

Page 37: XSPARQL Tutorial

XPath Steps

Step examples

/relations Selects the root element relations

/relations/person Selects all person elements that are children of relations

//knows Selects all knows elements (in all the document)

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

9 / 51

Page 38: XSPARQL Tutorial

XPath Predicates

Predicate examples

/relations/person[3] Selects the third person child of relations

/relations/person[position()<3] Selects the first two person children of relations//person[@name=’Alice’] Selects all person elements which the value of the

name attribute is ’Alice’

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

10 / 51

Page 39: XSPARQL Tutorial

XPath Predicates

Predicate examples

/relations/person[3] Selects the third person child of relations

/relations/person[position()<3] Selects the first two person children of relations

//person[@name=’Alice’] Selects all person elements which the value of thename attribute is ’Alice’

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

10 / 51

Page 40: XSPARQL Tutorial

XPath Predicates

Predicate examples

/relations/person[3] Selects the third person child of relations

/relations/person[position()<3] Selects the first two person children of relations//person[@name=’Alice’] Selects all person elements which the value of the

name attribute is ’Alice’

relations.xml<relations>

<person name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

10 / 51

Page 41: XSPARQL Tutorial

XQuery

XQuery

Query language for XML (different requirements than XSLT)

functional languagetyped language

Superset of XPath

Overview of the formal semantics (Normalisation rules, StaticTyping and Dynamic Evaluation)

XQuery spec: http://www.w3.org/TR/xquery/.XQuery and XPath Formal Semantics: http://www.w3.org/TR/xquery-semantics/.

11 / 51

Page 42: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query Example: Convert our relations data into RDF/XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name

where $nameA = "Alice"

return <foaf:Person>{$nameA,

for $nameB in $person/knows

let $friend := <foaf:Person name="{$nameB}"/>

order by $nameB

return <foaf:knows>{$friend}</foaf:knows>

}</foaf:Person>

12 / 51

Page 43: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query Example: Convert our relations data into RDF/XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name

where $nameA = "Alice"

return <foaf:Person>{$nameA,

for $nameB in $person/knows

let $friend := <foaf:Person name="{$nameB}"/>

order by $nameB

return <foaf:knows>{$friend}</foaf:knows>

}</foaf:Person>

12 / 51

Page 44: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query Example: Convert our relations data into RDF/XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name

where $nameA = "Alice"

return <foaf:Person>{$nameA,

for $nameB in $person/knows

let $friend := <foaf:Person name="{$nameB}"/>

order by $nameB

return <foaf:knows>{$friend}</foaf:knows>

}</foaf:Person>

12 / 51

Page 45: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query Example: Convert our relations data into RDF/XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name

where $nameA = "Alice"

return <foaf:Person>{$nameA,

for $nameB in $person/knows

let $friend := <foaf:Person name="{$nameB}"/>

order by $nameB

return <foaf:knows>{$friend}</foaf:knows>

}</foaf:Person>

12 / 51

Page 46: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query Example: Convert our relations data into RDF/XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name

where $nameA = "Alice"

return <foaf:Person>{$nameA,

for $nameB in $person/knows

let $friend := <foaf:Person name="{$nameB}"/>

order by $nameB

return <foaf:knows>{$friend}</foaf:knows>

}</foaf:Person>

12 / 51

Page 47: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query Example: Convert our relations data into RDF/XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name

where $nameA = "Alice"

return <foaf:Person>{$nameA,

for $nameB in $person/knows

let $friend := <foaf:Person name="{$nameB}"/>

order by $nameB

return <foaf:knows>{$friend}</foaf:knows>

}</foaf:Person>

12 / 51

Page 48: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query Example: Convert our relations data into RDF/XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name

where $nameA = "Alice"

return <foaf:Person>{$nameA,

for $nameB in $person/knows

let $friend := <foaf:Person name="{$nameB}"/>

order by $nameB

return <foaf:knows>{$friend}</foaf:knows>

}</foaf:Person>

12 / 51

Page 49: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query Example: Convert our relations data into RDF/XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name

where $nameA = "Alice"

return <foaf:Person>{$nameA,

for $nameB in $person/knows

let $friend := <foaf:Person name="{$nameB}"/>

order by $nameB

return <foaf:knows>{$friend}</foaf:knows>

}</foaf:Person>

12 / 51

Page 50: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query Example: Convert our relations data into RDF/XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name

where $nameA = "Alice"

return <foaf:Person>{$nameA,

for $nameB in $person/knows

let $friend := <foaf:Person name="{$nameB}"/>

order by $nameB

return <foaf:knows>{$friend}</foaf:knows>

}</foaf:Person>

12 / 51

Page 51: XSPARQL Tutorial

Schematic view on XQuery

Prolog: P declare namespace prefix="namespace-URI"

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by XPath-expression

Head: R return XML + nested XQuery

Query result<foaf:Person xmlns:foaf="http://xmlns.com/foaf/0.1/" name="Alice">

<foaf:knows>

<foaf:Person name="Bob"/>

</foaf:knows>

<foaf:knows>

<foaf:Person name="Charles"/>

</foaf:knows>

</foaf:Person>

12 / 51

Page 52: XSPARQL Tutorial

Positional variable at

Query example: add an id attribute to the relations datafor $person at $pos in doc("relations.xml")//person

return <person id="{$pos}">

{$person/@*, $person/*}

</person>

$pos refers to the position of $person in the for expression

Query result<person id="1" name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person id="2" name="Bob">

<knows>Charles</knows>

</person>

<person id="3" name="Charles"/>

13 / 51

Page 53: XSPARQL Tutorial

Positional variable at

Query example: add an id attribute to the relations datafor $person at $pos in doc("relations.xml")//person

return <person id="{$pos}">

{$person/@*, $person/*}

</person>

$pos refers to the position of $person in the for expression

Query result<person id="1" name="Alice">

<knows>Bob</knows>

<knows>Charles</knows>

</person>

<person id="2" name="Bob">

<knows>Charles</knows>

</person>

<person id="3" name="Charles"/>

13 / 51

Page 54: XSPARQL Tutorial

XQuery Formal semantics

Normalisation rules

Normalisation rules are rewriting rules that translate XQuery into asimplified version (XQuery Core).

Rule application

1 Static Analysis: Apply normalisation rules and static typeanalysis

2 Dynamic Evaluation Rules: evaluate expressions

Environments

statEnv contains information needed for performing statictype analysis. E.g. varType, funcType, . . .

dynEnv contains information needed for the evaluation ofexpressions. E.g. varValue, . . .

14 / 51

Page 55: XSPARQL Tutorial

XQuery Formal semantics

Normalisation rules

Normalisation rules are rewriting rules that translate XQuery into asimplified version (XQuery Core).

Rule application

1 Static Analysis: Apply normalisation rules and static typeanalysis

2 Dynamic Evaluation Rules: evaluate expressions

Environments

statEnv contains information needed for performing statictype analysis. E.g. varType, funcType, . . .

dynEnv contains information needed for the evaluation ofexpressions. E.g. varValue, . . .

14 / 51

Page 56: XSPARQL Tutorial

XQuery Formal semantics

Normalisation rules

Normalisation rules are rewriting rules that translate XQuery into asimplified version (XQuery Core).

Rule application

1 Static Analysis: Apply normalisation rules and static typeanalysis

2 Dynamic Evaluation Rules: evaluate expressions

Environments

statEnv contains information needed for performing statictype analysis. E.g. varType, funcType, . . .

dynEnv contains information needed for the evaluation ofexpressions. E.g. varValue, . . .

14 / 51

Page 57: XSPARQL Tutorial

XQuery Formal semantics

Normalisation rules

Normalisation rules are rewriting rules that translate XQuery into asimplified version (XQuery Core).

Rule application

1 Static Analysis: Apply normalisation rules and static typeanalysis

2 Dynamic Evaluation Rules: evaluate expressions

Environments

statEnv contains information needed for performing statictype analysis. E.g. varType, funcType, . . .

dynEnv contains information needed for the evaluation ofexpressions. E.g. varValue, . . .

14 / 51

Page 58: XSPARQL Tutorial

Semantics - Normalisation rules example

for Examplefor $i in (1, 2),

$j in (3, 4)

return

<pair>{ ($i,$j) }</pair>

for Normalised examplefor $i in (1, 2) return

for $j in (3, 4) return

<pair>{ ($i,$j) }</pair>

15 / 51

Page 59: XSPARQL Tutorial

Semantics - Normalisation rules example

for Examplefor $i in (1, 2),

$j in (3, 4)

return

<pair>{ ($i,$j) }</pair>

for Normalised examplefor $i in (1, 2) return

for $j in (3, 4) return

<pair>{ ($i,$j) }</pair>

15 / 51

Page 60: XSPARQL Tutorial

Semantics - Normalisation rules example

For Normalisation for $VarName1 in Expr1, · · · ,$VarNamen in Exprn

ReturnClause

Expr

==

for $VarName1 in [[Expr1]]Expr return

· · ·for $VarNamen in [[Exprn]]Expr [[ReturnClause]]Expr

16 / 51

Page 61: XSPARQL Tutorial

Semantics - Static typing example

For Static Type Analysis

statEnv ` Expr 1 : Type1

statEnv + varType(Variable ⇒ Type1) ` Expr 2 : Type2

statEnv ` for $Variable in Expr 1

return Expr 2 : Type2 · quantifier(Type1)

: means Expr 1 isof type Type1

+ means extendthe environment

quantifier estimates thenumber of solutions: *, +or ?

17 / 51

Page 62: XSPARQL Tutorial

Semantics - Static typing example

For Static Type Analysis

statEnv ` Expr 1 : Type1

statEnv + varType(Variable ⇒ Type1) ` Expr 2 : Type2

statEnv ` for $Variable in Expr 1

return Expr 2 : Type2 · quantifier(Type1)

: means Expr 1 isof type Type1

+ means extendthe environment

quantifier estimates thenumber of solutions: *, +or ?

17 / 51

Page 63: XSPARQL Tutorial

Semantics - Static typing example

For Static Type Analysis

statEnv ` Expr 1 : Type1

statEnv + varType(Variable ⇒ Type1) ` Expr 2 : Type2

statEnv ` for $Variable in Expr 1

return Expr 2 : Type2 · quantifier(Type1)

: means Expr 1 isof type Type1

+ means extendthe environment

quantifier estimates thenumber of solutions: *, +or ?

17 / 51

Page 64: XSPARQL Tutorial

Semantics - Static typing example

For Static Type Analysis

statEnv ` Expr 1 : Type1

statEnv + varType(Variable ⇒ Type1) ` Expr 2 : Type2

statEnv ` for $Variable in Expr 1

return Expr 2 : Type2 · quantifier(Type1)

: means Expr 1 isof type Type1

+ means extendthe environment

quantifier estimates thenumber of solutions: *, +or ?

17 / 51

Page 65: XSPARQL Tutorial

Semantics - Dynamic evaluation rules

Simple for example

for $i in (1, 2) return $i+1

18 / 51

Page 66: XSPARQL Tutorial

Semantics - Dynamic evaluation rules

Simple for example

for $i in (1, 2) return $i+1

For each result inthe expression

18 / 51

Page 67: XSPARQL Tutorial

Semantics - Dynamic evaluation rules

Simple for example

for $i in (1, 2) return $i+1

For each result inthe expression

Variable $i is as-signed the corre-sponding value

18 / 51

Page 68: XSPARQL Tutorial

Semantics - Dynamic evaluation rules

Simple for example

for $i in (1, 2) return $i+1

For each result inthe expression

Variable $i is as-signed the corre-sponding value

Return expresionis evaluated

18 / 51

Page 69: XSPARQL Tutorial

Semantics - Dynamic evaluation rules

Simple for example

for $i in (1, 2) return $i+1

For Dynamic Evaluation (Simplified)

dynEnv ` Expr 1 ⇒ Item1, . . . , Itemn

dynEnv + varValue(Variable ⇒ Item1) ` Expr 2 ⇒ Value1

· · ·dynEnv + varValue(Variable ⇒ Itemn) ` Expr 2 ⇒ Valuen

dynEnv ` for $Variable in Expr 1

return Expr 2 ⇒ Value1, · · · ,Valuen

18 / 51

Page 70: XSPARQL Tutorial

Semantics - Dynamic evaluation rules

Simple for example

for $i in (1, 2) return $i+1

For Dynamic Evaluation (Simplified)

dynEnv ` Expr 1 ⇒ Item1, . . . , Itemn

dynEnv + varValue(Variable ⇒ Item1) ` Expr 2 ⇒ Value1

· · ·dynEnv + varValue(Variable ⇒ Itemn) ` Expr 2 ⇒ Valuen

dynEnv ` for $Variable in Expr 1

return Expr 2 ⇒ Value1, · · · ,Valuen

⇒ means Expr 1 eval-uates to the sequenceItem1, . . . , Itemn

18 / 51

Page 71: XSPARQL Tutorial

Semantics - Dynamic evaluation rules

Simple for example

for $i in (1, 2) return $i+1

For Dynamic Evaluation (Simplified)

dynEnv ` Expr 1 ⇒ Item1, . . . , Itemn

dynEnv + varValue(Variable ⇒ Item1) ` Expr 2 ⇒ Value1

· · ·dynEnv + varValue(Variable ⇒ Itemn) ` Expr 2 ⇒ Valuen

dynEnv ` for $Variable in Expr 1

return Expr 2 ⇒ Value1, · · · ,Valuen

⇒ means Expr 1 eval-uates to the sequenceItem1, . . . , Itemn

For each Itemi , add Variable⇒ Itemi to dynEnv andevaluate Expr 2

18 / 51

Page 72: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 73: XSPARQL Tutorial

SPARQL (in 3 slides)

SPARQL

Query language for RDF

RDF represents data as triples: Subject, Predicate, Object.An RDF Graph is a set of triples.

SPARQL queries RDF data by pattern matching: given a setof triple patterns, finds the corresponding triples in the inputgraph

Actually, matched against the scoping graph: a graphequivalent to the input graph but does not share any blanknodes with it or the query.

SPARQL spec: http://www.w3.org/TR/rdf-sparql-query/.SPARQL 1.1 Tutorial:

http://polleres.net/presentations/20101019SPARQL1.1Tutorial.pdf.

19 / 51

Page 74: XSPARQL Tutorial

SPARQL (in 3 slides)

SPARQL

Query language for RDF

RDF represents data as triples: Subject, Predicate, Object.An RDF Graph is a set of triples.

SPARQL queries RDF data by pattern matching: given a setof triple patterns, finds the corresponding triples in the inputgraph

Actually, matched against the scoping graph: a graphequivalent to the input graph but does not share any blanknodes with it or the query.

SPARQL spec: http://www.w3.org/TR/rdf-sparql-query/.SPARQL 1.1 Tutorial:

http://polleres.net/presentations/20101019SPARQL1.1Tutorial.pdf.

19 / 51

Page 75: XSPARQL Tutorial

SPARQL (in 3 slides)

SPARQL

Query language for RDF

RDF represents data as triples: Subject, Predicate, Object.An RDF Graph is a set of triples.

SPARQL queries RDF data by pattern matching: given a setof triple patterns, finds the corresponding triples in the inputgraph

Actually, matched against the scoping graph: a graphequivalent to the input graph but does not share any blanknodes with it or the query.

SPARQL spec: http://www.w3.org/TR/rdf-sparql-query/.SPARQL 1.1 Tutorial:

http://polleres.net/presentations/20101019SPARQL1.1Tutorial.pdf.

19 / 51

Page 76: XSPARQL Tutorial

SPARQL (in 3 slides)

SPARQL

Query language for RDF

RDF represents data as triples: Subject, Predicate, Object.An RDF Graph is a set of triples.

SPARQL queries RDF data by pattern matching: given a setof triple patterns, finds the corresponding triples in the inputgraph

Actually, matched against the scoping graph: a graphequivalent to the input graph but does not share any blanknodes with it or the query.

SPARQL spec: http://www.w3.org/TR/rdf-sparql-query/.SPARQL 1.1 Tutorial:

http://polleres.net/presentations/20101019SPARQL1.1Tutorial.pdf.

19 / 51

Page 77: XSPARQL Tutorial

Schematic view on SPARQL

Prolog: P prefix prefix: <namespace-URI>

Head: C construct { template }select variableList

Body: D from / from named <dataset-URI>W where { pattern }M order by expression

limit integer > 0offset integer > 0

Query Example: Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

20 / 51

Page 78: XSPARQL Tutorial

Schematic view on SPARQL

Prolog: P prefix prefix: <namespace-URI>

Head: C construct { template }select variableList

Body: D from / from named <dataset-URI>W where { pattern }M order by expression

limit integer > 0offset integer > 0

Query Example: Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

20 / 51

Page 79: XSPARQL Tutorial

Schematic view on SPARQL

Prolog: P prefix prefix: <namespace-URI>

Head: C construct { template }select variableList

Body: D from / from named <dataset-URI>W where { pattern }M order by expression

limit integer > 0offset integer > 0

Query Example: Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

20 / 51

Page 80: XSPARQL Tutorial

Schematic view on SPARQL

Prolog: P prefix prefix: <namespace-URI>

Head: C construct { template }select variableList

Body: D from / from named <dataset-URI>W where { pattern }M order by expression

limit integer > 0offset integer > 0

Query Example: Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

20 / 51

Page 81: XSPARQL Tutorial

Schematic view on SPARQL

Prolog: P prefix prefix: <namespace-URI>

Head: C construct { template }select variableList

Body: D from / from named <dataset-URI>W where { pattern }M order by expression

limit integer > 0offset integer > 0

Query Example: Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

20 / 51

Page 82: XSPARQL Tutorial

Schematic view on SPARQL

Prolog: P prefix prefix: <namespace-URI>

Head: C construct { template }select variableList

Body: D from / from named <dataset-URI>W where { pattern }M order by expression

limit integer > 0offset integer > 0

Query Example: Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

20 / 51

Page 83: XSPARQL Tutorial

Schematic view on SPARQL

Prolog: P prefix prefix: <namespace-URI>

Head: C construct { template }select variableList

Body: D from / from named <dataset-URI>W where { pattern }M order by expression

limit integer > 0offset integer > 0

Query Example: Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

20 / 51

Page 84: XSPARQL Tutorial

Schematic view on SPARQL

Prolog: P prefix prefix: <namespace-URI>

Head: C construct { template }select variableList

Body: D from / from named <dataset-URI>W where { pattern }M order by expression

limit integer > 0offset integer > 0

Query Example: Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

20 / 51

Page 85: XSPARQL Tutorial

Schematic view on SPARQL

Prolog: P prefix prefix: <namespace-URI>

Head: C construct { template }select variableList

Body: D from / from named <dataset-URI>W where { pattern }M order by expression

limit integer > 0offset integer > 0

Query Example: Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

select $X $FN

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

20 / 51

Page 86: XSPARQL Tutorial

SPARQL select solutions

Solutions for SPARQL select queries are substitutions forthe variables present in the head (variableList)

Can be represented as XML

SPARQL XML Results Format (previous query)<sparql xmlns="http://www.w3.org/2005/sparql-results#">

<head>

<variable name="X"/>

<variable name="FN"/>

</head>

<results>

<result>

<binding name="X"><bnode>b0</bnode></binding>

<binding name="FN"><literal>Nuno Lopes</literal></binding>

</result>

</results>

</sparql>

21 / 51

Page 87: XSPARQL Tutorial

SPARQL select solutions

Solutions for SPARQL select queries are substitutions forthe variables present in the head (variableList)

Can be represented as XML

SPARQL XML Results Format (previous query)<sparql xmlns="http://www.w3.org/2005/sparql-results#">

<head>

<variable name="X"/>

<variable name="FN"/>

</head>

<results>

<result>

<binding name="X"><bnode>b0</bnode></binding>

<binding name="FN"><literal>Nuno Lopes</literal></binding>

</result>

</results>

</sparql>

21 / 51

Page 88: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 89: XSPARQL Tutorial

XSPARQL

Transformation language

Consume and generate XML and RDF

Syntactic extension of XQuery

With a formally defined semantics(based on the XQuery semantics)

22 / 51

Page 90: XSPARQL Tutorial

XSPARQL

Transformation language

Consume and generate XML and RDF

Syntactic extension of XQuery

With a formally defined semantics(based on the XQuery semantics)

22 / 51

Page 91: XSPARQL Tutorial

XSPARQL

Transformation language

Consume and generate XML and RDF

Syntactic extension of XQuery

With a formally defined semantics(based on the XQuery semantics)

22 / 51

Page 92: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 93: XSPARQL Tutorial

XSPARQL: Combining XQuery with SPARQL

prefix

declarations

Data input(XML or RDF)

Data output(XML or RDF)

Prolog: P declare namespace prefix="namespace-URI"or prefix prefix: <namespace-URI>

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by expression orF’ for varlistD from / from named <dataset-URI>W where {pattern }M order by expression

limit integer > 0offset integer > 0

Head: C construct

{ template (with nested XSPARQL) } orR return XML + nested XSPARQL

XQuery orSPARQLprefix

declarations

Any XQueryquery

SPARQLForClause

represents aSPARQL query

construct

creates RDFoutput

23 / 51

Page 94: XSPARQL Tutorial

XSPARQL: Combining XQuery with SPARQL

prefix

declarations

Data input(XML or RDF)

Data output(XML or RDF)

Prolog: P declare namespace prefix="namespace-URI"or prefix prefix: <namespace-URI>

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by expression orF’ for varlistD from / from named <dataset-URI>W where {pattern }M order by expression

limit integer > 0offset integer > 0

Head: C construct

{ template (with nested XSPARQL) } orR return XML + nested XSPARQL

XQuery orSPARQLprefix

declarations

Any XQueryquery

SPARQLForClause

represents aSPARQL query

construct

creates RDFoutput

23 / 51

Page 95: XSPARQL Tutorial

XSPARQL: Combining XQuery with SPARQL

prefix

declarations

Data input(XML or RDF)

Data output(XML or RDF)

Prolog: P declare namespace prefix="namespace-URI"or prefix prefix: <namespace-URI>

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by expression orF’ for varlistD from / from named <dataset-URI>W where {pattern }M order by expression

limit integer > 0offset integer > 0

Head: C construct

{ template (with nested XSPARQL) } orR return XML + nested XSPARQL

XQuery orSPARQLprefix

declarations

Any XQueryquery

SPARQLForClause

represents aSPARQL query

construct

creates RDFoutput

23 / 51

Page 96: XSPARQL Tutorial

XSPARQL: Combining XQuery with SPARQL

prefix

declarations

Data input(XML or RDF)

Data output(XML or RDF)

Prolog: P declare namespace prefix="namespace-URI"or prefix prefix: <namespace-URI>

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by expression orF’ for varlistD from / from named <dataset-URI>W where {pattern }M order by expression

limit integer > 0offset integer > 0

Head: C construct

{ template (with nested XSPARQL) } orR return XML + nested XSPARQL

XQuery orSPARQLprefix

declarations

Any XQueryquery

SPARQLForClause

represents aSPARQL query

construct

creates RDFoutput

23 / 51

Page 97: XSPARQL Tutorial

XSPARQL: Combining XQuery with SPARQL

prefix

declarations

Data input(XML or RDF)

Data output(XML or RDF)

Prolog: P declare namespace prefix="namespace-URI"or prefix prefix: <namespace-URI>

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by expression orF’ for varlistD from / from named <dataset-URI>W where {pattern }M order by expression

limit integer > 0offset integer > 0

Head: C construct

{ template (with nested XSPARQL) } orR return XML + nested XSPARQL

XQuery orSPARQLprefix

declarations

Any XQueryquery

SPARQLForClause

represents aSPARQL query

construct

creates RDFoutput

23 / 51

Page 98: XSPARQL Tutorial

XSPARQL: Combining XQuery with SPARQL

prefix

declarations

Data input(XML or RDF)

Data output(XML or RDF)

Prolog: P declare namespace prefix="namespace-URI"or prefix prefix: <namespace-URI>

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by expression orF’ for varlistD from / from named <dataset-URI>W where {pattern }M order by expression

limit integer > 0offset integer > 0

Head: C construct

{ template (with nested XSPARQL) } orR return XML + nested XSPARQL

XQuery orSPARQLprefix

declarations

Any XQueryquery

SPARQLForClause

represents aSPARQL query

construct

creates RDFoutput

23 / 51

Page 99: XSPARQL Tutorial

XSPARQL: Combining XQuery with SPARQL

prefix

declarations

Data input(XML or RDF)

Data output(XML or RDF)

Prolog: P declare namespace prefix="namespace-URI"or prefix prefix: <namespace-URI>

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by expression orF’ for varlistD from / from named <dataset-URI>W where {pattern }M order by expression

limit integer > 0offset integer > 0

Head: C construct

{ template (with nested XSPARQL) } orR return XML + nested XSPARQL

XQuery orSPARQLprefix

declarations

Any XQueryquery

SPARQLForClause

represents aSPARQL query

construct

creates RDFoutput

23 / 51

Page 100: XSPARQL Tutorial

XSPARQL: Combining XQuery with SPARQL

prefix

declarations

Data input(XML or RDF)

Data output(XML or RDF)

Prolog: P declare namespace prefix="namespace-URI"or prefix prefix: <namespace-URI>

Body: F for var in XPath-expressionL let var := XPath-expressionW where XPath-expressionO order by expression orF’ for varlistD from / from named <dataset-URI>W where {pattern }M order by expression

limit integer > 0offset integer > 0

Head: C construct

{ template (with nested XSPARQL) } orR return XML + nested XSPARQL

XQuery orSPARQLprefix

declarations

Any XQueryquery

SPARQLForClause

represents aSPARQL query

construct

creates RDFoutput

23 / 51

Page 101: XSPARQL Tutorial

Query Example - Lifting

Convert our relations data into RDF

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

Query result@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Bob"; a foaf:Person ] ].

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

[ foaf:name "Bob"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

24 / 51

Page 102: XSPARQL Tutorial

Query Example - Lifting

Convert our relations data into RDF

XQuery fordata selec-tion

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

Query result@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Bob"; a foaf:Person ] ].

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

[ foaf:name "Bob"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

24 / 51

Page 103: XSPARQL Tutorial

Query Example - Lifting

Convert our relations data into RDF

construct

clause gen-erates RDF

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

Query result@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Bob"; a foaf:Person ] ].

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

[ foaf:name "Bob"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

24 / 51

Page 104: XSPARQL Tutorial

Query Example - Lifting

Convert our relations data into RDF

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

Query result@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Bob"; a foaf:Person ] ].

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

[ foaf:name "Bob"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

24 / 51

Page 105: XSPARQL Tutorial

Query Example - Lifting

Convert our relations data into RDF

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

Nesting producesan RDF literal

Query result@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Bob"; a foaf:Person ] ].

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

[ foaf:name "Bob"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

24 / 51

Page 106: XSPARQL Tutorial

Query Example - Lifting

Convert our relations data into RDF

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

Query result@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Bob"; a foaf:Person ] ].

[ foaf:name "Alice"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

[ foaf:name "Bob"; a foaf:Person; foaf:knows

[ foaf:name "Charles"; a foaf:Person ] ].

24 / 51

Page 107: XSPARQL Tutorial

Nesting in construct clauses

Nesting operators

{Expr} The result of evaluating Expr will be an RDF literal

:{Expr} Same but for RDF blank nodes

<{Expr}> and IRIs

25 / 51

Page 108: XSPARQL Tutorial

Nesting in construct clauses

Nesting operators

{Expr} The result of evaluating Expr will be an RDF literal

:{Expr} Same but for RDF blank nodes

<{Expr}> and IRIs

25 / 51

Page 109: XSPARQL Tutorial

Query Example - Lifting (II)

Convert our relations data into RDF

declare namespace foaf="http://xmlns.com/foaf/0.1/";

let $persons := doc("relations.xml")//person

let $ids := data($persons/@name)

for $p in $persons

let $id := fn:index-of($ids, $p/@name)

construct { _:b{$id} a foaf:Person; foaf:name {data($p/@name)}.

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

Query result (reformatted output)@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:b1 a foaf:Person; foaf:name "Alice"; foaf:knows _:b2, _:b3 .

_:b2 a foaf:Person; foaf:name "Bob"; foaf:knows _:b3 .

_:b3 a foaf:Person; foaf:name "Charles" .

26 / 51

Page 110: XSPARQL Tutorial

Query Example - Lifting (II)

Convert our relations data into RDF

Keep person iden-tifiers

declare namespace foaf="http://xmlns.com/foaf/0.1/";

let $persons := doc("relations.xml")//person

let $ids := data($persons/@name)

for $p in $persons

let $id := fn:index-of($ids, $p/@name)

construct { _:b{$id} a foaf:Person; foaf:name {data($p/@name)}.

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

Query result (reformatted output)@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:b1 a foaf:Person; foaf:name "Alice"; foaf:knows _:b2, _:b3 .

_:b2 a foaf:Person; foaf:name "Bob"; foaf:knows _:b3 .

_:b3 a foaf:Person; foaf:name "Charles" .

26 / 51

Page 111: XSPARQL Tutorial

Query Example - Lifting (II)

Convert our relations data into RDF

For each personlookup their id

declare namespace foaf="http://xmlns.com/foaf/0.1/";

let $persons := doc("relations.xml")//person

let $ids := data($persons/@name)

for $p in $persons

let $id := fn:index-of($ids, $p/@name)

construct { _:b{$id} a foaf:Person; foaf:name {data($p/@name)}.

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

Query result (reformatted output)@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:b1 a foaf:Person; foaf:name "Alice"; foaf:knows _:b2, _:b3 .

_:b2 a foaf:Person; foaf:name "Bob"; foaf:knows _:b3 .

_:b3 a foaf:Person; foaf:name "Charles" .

26 / 51

Page 112: XSPARQL Tutorial

Query Example - Lifting (II)

Convert our relations data into RDF

The same for eachperson then know

declare namespace foaf="http://xmlns.com/foaf/0.1/";

let $persons := doc("relations.xml")//person

let $ids := data($persons/@name)

for $p in $persons

let $id := fn:index-of($ids, $p/@name)

construct { _:b{$id} a foaf:Person; foaf:name {data($p/@name)}.

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

Query result (reformatted output)@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:b1 a foaf:Person; foaf:name "Alice"; foaf:knows _:b2, _:b3 .

_:b2 a foaf:Person; foaf:name "Bob"; foaf:knows _:b3 .

_:b3 a foaf:Person; foaf:name "Charles" .

26 / 51

Page 113: XSPARQL Tutorial

Query Example - Lifting (II)

Convert our relations data into RDF

declare namespace foaf="http://xmlns.com/foaf/0.1/";

let $persons := doc("relations.xml")//person

let $ids := data($persons/@name)

for $p in $persons

let $id := fn:index-of($ids, $p/@name)

construct { _:b{$id} a foaf:Person; foaf:name {data($p/@name)}.

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

Query result (reformatted output)@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:b1 a foaf:Person; foaf:name "Alice"; foaf:knows _:b2, _:b3 .

_:b2 a foaf:Person; foaf:name "Bob"; foaf:knows _:b3 .

_:b3 a foaf:Person; foaf:name "Charles" .

26 / 51

Page 114: XSPARQL Tutorial

Query Example - Lowering

Convert FOAF RDF data into XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

27 / 51

Page 115: XSPARQL Tutorial

Query Example - Lowering

Convert FOAF RDF data into XML

XML con-struction

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

27 / 51

Page 116: XSPARQL Tutorial

Query Example - Lowering

Convert FOAF RDF data into XML

SPARQL for

query: “Give me

persons and their

names”

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

27 / 51

Page 117: XSPARQL Tutorial

Query Example - Lowering

Convert FOAF RDF data into XML

SPARQL for

query: “Give me

persons and their

names”

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

SPARQL variablesare $-prefixed

27 / 51

Page 118: XSPARQL Tutorial

Query Example - Lowering

Convert FOAF RDF data into XML

XML con-struction

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

27 / 51

Page 119: XSPARQL Tutorial

Query Example - Lowering

Convert FOAF RDF data into XML

SPARQL for

query: “Give me

the persons each

one knows”

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

27 / 51

Page 120: XSPARQL Tutorial

Query Example - Lowering

Convert FOAF RDF data into XML

SPARQL for

query: “Give me

the persons each

one knows”

$Person and$Name instanti-ated by the outerloop

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

27 / 51

Page 121: XSPARQL Tutorial

Query Example - Lowering

Convert FOAF RDF data into XML

XML con-struction

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

27 / 51

Page 122: XSPARQL Tutorial

Query Example - Lowering result

Query result<relations>

<person name="Alice">

<knows>Charles</knows>

<knows>Bob</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

28 / 51

Page 123: XSPARQL Tutorial

Online Demo

Online Demo at: http://xsparql.deri.org/demo/

Try it for yourself!

29 / 51

Page 124: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 125: XSPARQL Tutorial

XSPARQL Formal Semantics

Extend the XQuery semantics

Adding the normalisation, static type and dynamic evaluationrules for the new expressions:

SPARQL for clauseconstruct clause

30 / 51

Page 126: XSPARQL Tutorial

Formal semantics types

Newly defined types

RDFTerm is the type of SPARQL variables, with the subtypes:

uri

bnode

literal

RDFGraph will be the type of construct expressions

PatternSolution is a pair (variableName,RDFTerm)representing SPARQL variable bindings

31 / 51

Page 127: XSPARQL Tutorial

Formal semantics types

Newly defined types

RDFTerm is the type of SPARQL variables, with the subtypes:

uri

bnode

literal

RDFGraph will be the type of construct expressions

PatternSolution is a pair (variableName,RDFTerm)representing SPARQL variable bindings

31 / 51

Page 128: XSPARQL Tutorial

Formal semantics types

Newly defined types

RDFTerm is the type of SPARQL variables, with the subtypes:

uri

bnode

literal

RDFGraph will be the type of construct expressions

PatternSolution is a pair (variableName,RDFTerm)representing SPARQL variable bindings

31 / 51

Page 129: XSPARQL Tutorial

SPARQL for - Static Type Analysis

statEnv + varType(Variable1 ⇒ RDFTerm;· · · ;Variablen ⇒ RDFTerm

) ` ReturnExpr : Type2

statEnv `for $Variable1 · · · $Variablen DatasetClausewhere GroupGraphPattern SolutionModifierreturn ReturnExpr : Type2∗

32 / 51

Page 130: XSPARQL Tutorial

SPARQL for - Static Type Analysis

statEnv + varType(Variable1 ⇒ RDFTerm;· · · ;Variablen ⇒ RDFTerm

) ` ReturnExpr : Type2

statEnv `for $Variable1 · · · $Variablen DatasetClausewhere GroupGraphPattern SolutionModifierreturn ReturnExpr : Type2∗

$Variable1 · · · $Variablen

are of type RDFTerm

32 / 51

Page 131: XSPARQL Tutorial

SPARQL for - Static Type Analysis

statEnv + varType(Variable1 ⇒ RDFTerm;· · · ;Variablen ⇒ RDFTerm

) ` ReturnExpr : Type2

statEnv `for $Variable1 · · · $Variablen DatasetClausewhere GroupGraphPattern SolutionModifierreturn ReturnExpr : Type2∗

∗ comes fromthe sequence ofSPARQL results

32 / 51

Page 132: XSPARQL Tutorial

Example: Simple SPARQL for

Simple SPARQL for example

for $s $p $o

from <foaf.rdf> where { $s $p $o }

return ($s, $p, $o)

33 / 51

Page 133: XSPARQL Tutorial

Example: Simple SPARQL for

Simple SPARQL for example

for $s $p $o

from <foaf.rdf> where { $s $p $o }

return ($s, $p, $o)

For each SPARQLresult

33 / 51

Page 134: XSPARQL Tutorial

Example: Simple SPARQL for

Simple SPARQL for example

for $s $p $o

from <foaf.rdf> where { $s $p $o }

return ($s, $p, $o)

For each SPARQLresult

Variables are as-signed values

33 / 51

Page 135: XSPARQL Tutorial

Example: Simple SPARQL for

Simple SPARQL for example

for $s $p $o

from <foaf.rdf> where { $s $p $o }

return ($s, $p, $o)

For each SPARQLresult

Variables are as-signed values

Return expresionis evaluated

33 / 51

Page 136: XSPARQL Tutorial

SPARQL for - Dynamic Evaluation

dynEnv ` fs:sparql(DatasetClause,GroupGraphPattern,SolutionModifier)⇒ PS1, . . . ,PSm

dynEnv + varValue(Variable1 ⇒ fs:value(PS1,Variable1);. . . ;Variablen ⇒ fs:value(PS1,Variablen)

) ` ReturnExpr ⇒ Value1

...dynEnv + varValue(Variable1 ⇒ fs:value(PSm,Variable1);

. . . ;Variablen ⇒ fs:value(PSm,Variablen)

) ` ReturnExpr ⇒ Valuem

dynEnv `for $Variable1 · · · $Variablen

where GroupGraphPattern SolutionModifierreturn ReturnExpr ⇒ Value1, . . . ,Valuem

34 / 51

Page 137: XSPARQL Tutorial

SPARQL for - Dynamic Evaluation

dynEnv ` fs:sparql(DatasetClause,GroupGraphPattern,SolutionModifier)⇒ PS1, . . . ,PSm

dynEnv + varValue(Variable1 ⇒ fs:value(PS1,Variable1);. . . ;Variablen ⇒ fs:value(PS1,Variablen)

) ` ReturnExpr ⇒ Value1

...dynEnv + varValue(Variable1 ⇒ fs:value(PSm,Variable1);

. . . ;Variablen ⇒ fs:value(PSm,Variablen)

) ` ReturnExpr ⇒ Valuem

dynEnv `for $Variable1 · · · $Variablen

where GroupGraphPattern SolutionModifierreturn ReturnExpr ⇒ Value1, . . . ,Valuem

The results ofthe SPARQLquery

34 / 51

Page 138: XSPARQL Tutorial

SPARQL for - Dynamic Evaluation

dynEnv ` fs:sparql(DatasetClause,GroupGraphPattern,SolutionModifier)⇒ PS1, . . . ,PSm

dynEnv + varValue(Variable1 ⇒ fs:value(PS1,Variable1);. . . ;Variablen ⇒ fs:value(PS1,Variablen)

) ` ReturnExpr ⇒ Value1

...dynEnv + varValue(Variable1 ⇒ fs:value(PSm,Variable1);

. . . ;Variablen ⇒ fs:value(PSm,Variablen)

) ` ReturnExpr ⇒ Valuem

dynEnv `for $Variable1 · · · $Variablen

where GroupGraphPattern SolutionModifierreturn ReturnExpr ⇒ Value1, . . . ,Valuem

The results ofthe SPARQLquery

fs:sparql evaluatesa SPARQL query

34 / 51

Page 139: XSPARQL Tutorial

SPARQL for - Dynamic Evaluation

dynEnv ` fs:sparql(DatasetClause,GroupGraphPattern,SolutionModifier)⇒ PS1, . . . ,PSm

dynEnv + varValue(Variable1 ⇒ fs:value(PS1,Variable1);. . . ;Variablen ⇒ fs:value(PS1,Variablen)

) ` ReturnExpr ⇒ Value1

...dynEnv + varValue(Variable1 ⇒ fs:value(PSm,Variable1);

. . . ;Variablen ⇒ fs:value(PSm,Variablen)

) ` ReturnExpr ⇒ Valuem

dynEnv `for $Variable1 · · · $Variablen

where GroupGraphPattern SolutionModifierreturn ReturnExpr ⇒ Value1, . . . ,Valuem

For each PS

add the valuesof the variablesto dynEnv

34 / 51

Page 140: XSPARQL Tutorial

SPARQL for - Dynamic Evaluation

dynEnv ` fs:sparql(DatasetClause,GroupGraphPattern,SolutionModifier)⇒ PS1, . . . ,PSm

dynEnv + varValue(Variable1 ⇒ fs:value(PS1,Variable1);. . . ;Variablen ⇒ fs:value(PS1,Variablen)

) ` ReturnExpr ⇒ Value1

...dynEnv + varValue(Variable1 ⇒ fs:value(PSm,Variable1);

. . . ;Variablen ⇒ fs:value(PSm,Variablen)

) ` ReturnExpr ⇒ Valuem

dynEnv `for $Variable1 · · · $Variablen

where GroupGraphPattern SolutionModifierreturn ReturnExpr ⇒ Value1, . . . ,Valuem

For each PS

add the valuesof the variablesto dynEnv

fs:value selectsthe value ofVariable i fromthe PS

34 / 51

Page 141: XSPARQL Tutorial

SPARQL for - Dynamic Evaluation

dynEnv ` fs:sparql(DatasetClause,GroupGraphPattern,SolutionModifier)⇒ PS1, . . . ,PSm

dynEnv + varValue(Variable1 ⇒ fs:value(PS1,Variable1);. . . ;Variablen ⇒ fs:value(PS1,Variablen)

) ` ReturnExpr ⇒ Value1

...dynEnv + varValue(Variable1 ⇒ fs:value(PSm,Variable1);

. . . ;Variablen ⇒ fs:value(PSm,Variablen)

) ` ReturnExpr ⇒ Valuem

dynEnv `for $Variable1 · · · $Variablen

where GroupGraphPattern SolutionModifierreturn ReturnExpr ⇒ Value1, . . . ,Valuem

For each PS

add the valuesof the variablesto dynEnv

34 / 51

Page 142: XSPARQL Tutorial

SPARQL for - Dynamic Evaluation

dynEnv ` fs:sparql(DatasetClause,GroupGraphPattern,SolutionModifier)⇒ PS1, . . . ,PSm

dynEnv + varValue(Variable1 ⇒ fs:value(PS1,Variable1);. . . ;Variablen ⇒ fs:value(PS1,Variablen)

) ` ReturnExpr ⇒ Value1

...dynEnv + varValue(Variable1 ⇒ fs:value(PSm,Variable1);

. . . ;Variablen ⇒ fs:value(PSm,Variablen)

) ` ReturnExpr ⇒ Valuem

dynEnv `for $Variable1 · · · $Variablen

where GroupGraphPattern SolutionModifierreturn ReturnExpr ⇒ Value1, . . . ,Valuem

The result ofthe expressionis the sequenceof computedValues

34 / 51

Page 143: XSPARQL Tutorial

construct expressions

[[construct ConstructTemplate′ ]]

Expr

==

return fs:evalTemplate(

[[ConstructTemplate′]]normaliseTemplate

)

35 / 51

Page 144: XSPARQL Tutorial

construct expressions

[[construct ConstructTemplate′ ]]

Expr

==

return fs:evalTemplate(

[[ConstructTemplate′]]normaliseTemplate

)

[[·]]normaliseTemplate ex-pands any Turtle short-cuts in its argument

35 / 51

Page 145: XSPARQL Tutorial

construct expressions

[[construct ConstructTemplate′ ]]

Expr

==

return fs:evalTemplate(

[[ConstructTemplate′]]normaliseTemplate

)fs:evalTemplate vali-dates the created triples

35 / 51

Page 146: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 147: XSPARQL Tutorial

Implementation

Each XSPARQL query is rewritten into an XQuery

SPARQLForClauses are translated into SPARQL SELECTqueries and executed using a SPARQL engine

36 / 51

Page 148: XSPARQL Tutorial

Implementation

Each XSPARQL query is rewritten into an XQuery

SPARQLForClauses are translated into SPARQL SELECTqueries and executed using a SPARQL engine

36 / 51

Page 149: XSPARQL Tutorial

Implementation

Each XSPARQL query is rewritten into an XQuery

SPARQLForClauses are translated into SPARQL SELECTqueries and executed using a SPARQL engine

36 / 51

Page 150: XSPARQL Tutorial

Lifting: Rewriting to XQuery

Convert our relations data into RDF

declare namespace foaf="http://xmlns.com/foaf/0.1/";

let $persons := doc("relations.xml")//person

let $ids := data($persons/@name)

for $p in $persons

let $id := fn:index-of($ids, $p/@name)

construct { _:b{$id} a foaf:Person; foaf:name {data($p/@name)}.

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

37 / 51

Page 151: XSPARQL Tutorial

Lifting: Rewriting to XQuery

Convert our relations data into RDF

declare namespace foaf="http://xmlns.com/foaf/0.1/";

let $persons := doc("relations.xml")//person

let $ids := data($persons/@name)

for $p in $persons

let $id := fn:index-of($ids, $p/@name)

construct { _:b{$id} a foaf:Person; foaf:name {data($p/@name)}.

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

37 / 51

Page 152: XSPARQL Tutorial

Lifting: Rewriting to XQuery

Convert our relations data into RDF (partial)

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

Rewritten XQuery (and re-rewritten for presentation purposes)

for $k at $k_pos in $p/knows

let $kid := fn:index-of( $ids, $k )

return

let $_rdf8 := _xsparql:_binding_term( "_:b", $id, "", "" )

let $_rdf9 := _xsparql:_binding_term( "_:b", $kid, "", "" )

return _xsparql:_serialize((

if (_xsparql:_validSubject($_rdf8) and _xsparql:_validObject($_rdf9))

then

($_rdf8, " foaf:knows ", _xsparql:_rdf_term( $_rdf9 ), " .&#xA;")

else "")

) ) )

37 / 51

Page 153: XSPARQL Tutorial

Lifting: Rewriting to XQuery

Convert our relations data into RDF (partial)

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

Rewritten XQuery (and re-rewritten for presentation purposes)

for $k at $k_pos in $p/knows

let $kid := fn:index-of( $ids, $k )

return

let $_rdf8 := _xsparql:_binding_term( "_:b", $id, "", "" )

let $_rdf9 := _xsparql:_binding_term( "_:b", $kid, "", "" )

return _xsparql:_serialize((

if (_xsparql:_validSubject($_rdf8) and _xsparql:_validObject($_rdf9))

then

($_rdf8, " foaf:knows ", _xsparql:_rdf_term( $_rdf9 ), " .&#xA;")

else "")

) ) )

Create variables oftype RDFTerm foreach constructedterm

37 / 51

Page 154: XSPARQL Tutorial

Lifting: Rewriting to XQuery

Convert our relations data into RDF (partial)

{ for $k in $p/knows

let $kid := fn:index-of($ids, $k)

construct { _:b{$id} foaf:knows _:b{$kid} } } }

Rewritten XQuery (and re-rewritten for presentation purposes)

for $k at $k_pos in $p/knows

let $kid := fn:index-of( $ids, $k )

return

let $_rdf8 := _xsparql:_binding_term( "_:b", $id, "", "" )

let $_rdf9 := _xsparql:_binding_term( "_:b", $kid, "", "" )

return _xsparql:_serialize((

if (_xsparql:_validSubject($_rdf8) and _xsparql:_validObject($_rdf9))

then

($_rdf8, " foaf:knows ", _xsparql:_rdf_term( $_rdf9 ), " .&#xA;")

else "")

) ) )

If all the variablesrepresent valid RDFterms for the givenposition we outputthe triple

37 / 51

Page 155: XSPARQL Tutorial

Handling blank nodes in CONSTRUCT

Construct foaf:Person

prefix foaf: <http://xmlns.com/foaf/0.1/>

for $id in ("a","b","c","d")

construct { _:Person a foaf:Person }

XQuery rewriting

for $id at $id_pos in ("a", "b", "c", "d")

let $_rdf0 := _xsparql:_binding_term(fn:concat("_:Person","_",$id_pos))

return

_xsparql:_serialize( (

if (_xsparql:_validSubject( $_rdf0 )) then

($_rdf0, " ", "a", " ", "foaf:Person", " .&#xA;")

else "") )

Query result

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:Person_1 a foaf:Person .

_:Person_2 a foaf:Person .

_:Person_3 a foaf:Person .

_:Person_4 a foaf:Person .

38 / 51

Page 156: XSPARQL Tutorial

Handling blank nodes in CONSTRUCT

Construct foaf:Person

prefix foaf: <http://xmlns.com/foaf/0.1/>

for $id in ("a","b","c","d")

construct { _:Person a foaf:Person }

Blank nodesshould be differentfor each solution

XQuery rewriting

for $id at $id_pos in ("a", "b", "c", "d")

let $_rdf0 := _xsparql:_binding_term(fn:concat("_:Person","_",$id_pos))

return

_xsparql:_serialize( (

if (_xsparql:_validSubject( $_rdf0 )) then

($_rdf0, " ", "a", " ", "foaf:Person", " .&#xA;")

else "") )

Query result

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:Person_1 a foaf:Person .

_:Person_2 a foaf:Person .

_:Person_3 a foaf:Person .

_:Person_4 a foaf:Person .

38 / 51

Page 157: XSPARQL Tutorial

Handling blank nodes in CONSTRUCT

Construct foaf:Person

prefix foaf: <http://xmlns.com/foaf/0.1/>

for $id in ("a","b","c","d")

construct { _:Person a foaf:Person }

XQuery rewriting

for $id at $id_pos in ("a", "b", "c", "d")

let $_rdf0 := _xsparql:_binding_term(fn:concat("_:Person","_",$id_pos))

return

_xsparql:_serialize( (

if (_xsparql:_validSubject( $_rdf0 )) then

($_rdf0, " ", "a", " ", "foaf:Person", " .&#xA;")

else "") )

Append positionvariable to theblank node

Query result

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:Person_1 a foaf:Person .

_:Person_2 a foaf:Person .

_:Person_3 a foaf:Person .

_:Person_4 a foaf:Person .

38 / 51

Page 158: XSPARQL Tutorial

Handling blank nodes in CONSTRUCT

Construct foaf:Person

prefix foaf: <http://xmlns.com/foaf/0.1/>

for $id in ("a","b","c","d")

construct { _:Person a foaf:Person }

XQuery rewriting

for $id at $id_pos in ("a", "b", "c", "d")

let $_rdf0 := _xsparql:_binding_term(fn:concat("_:Person","_",$id_pos))

return

_xsparql:_serialize( (

if (_xsparql:_validSubject( $_rdf0 )) then

($_rdf0, " ", "a", " ", "foaf:Person", " .&#xA;")

else "") )

Query result

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:Person_1 a foaf:Person .

_:Person_2 a foaf:Person .

_:Person_3 a foaf:Person .

_:Person_4 a foaf:Person .

38 / 51

Page 159: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

39 / 51

Page 160: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

39 / 51

Page 161: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML (partial)

for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

Rewriting to XQuery

let $_aux_results3 := _xsparql:_sparqlQuery(

"PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT $Person $Name from <relations.rdf>

where { $Person foaf:name $Name . } order by $Name " )

for $_aux_result3 at $_aux_result3_pos in $_aux_results3

let $Person := _xsparql:_resultNode( $_aux_result3, "Person" )

let $Name := _xsparql:_resultNode( $_aux_result3, "Name" )

40 / 51

Page 162: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML (partial)

for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

Rewriting to XQuery

let $_aux_results3 := _xsparql:_sparqlQuery(

"PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT $Person $Name from <relations.rdf>

where { $Person foaf:name $Name . } order by $Name " )

for $_aux_result3 at $_aux_result3_pos in $_aux_results3

let $Person := _xsparql:_resultNode( $_aux_result3, "Person" )

let $Name := _xsparql:_resultNode( $_aux_result3, "Name" )

40 / 51

Page 163: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML (partial)

for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

Rewriting to XQuery

let $_aux_results3 := _xsparql:_sparqlQuery(

"PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT $Person $Name from <relations.rdf>

where { $Person foaf:name $Name . } order by $Name " )

for $_aux_result3 at $_aux_result3_pos in $_aux_results3

let $Person := _xsparql:_resultNode( $_aux_result3, "Person" )

let $Name := _xsparql:_resultNode( $_aux_result3, "Name" )

40 / 51

Page 164: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML (partial)

for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

Rewriting to XQuery

let $_aux_results3 := _xsparql:_sparqlQuery(

"PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT $Person $Name from <relations.rdf>

where { $Person foaf:name $Name . } order by $Name " )

for $_aux_result3 at $_aux_result3_pos in $_aux_results3

let $Person := _xsparql:_resultNode( $_aux_result3, "Person" )

let $Name := _xsparql:_resultNode( $_aux_result3, "Name" )

40 / 51

Page 165: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML (partial)

for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

Rewriting to XQuery

let $_aux_results3 := _xsparql:_sparqlQuery(

"PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT $Person $Name from <relations.rdf>

where { $Person foaf:name $Name . } order by $Name " )

for $_aux_result3 at $_aux_result3_pos in $_aux_results3

let $Person := _xsparql:_resultNode( $_aux_result3, "Person" )

let $Name := _xsparql:_resultNode( $_aux_result3, "Name" )

sparqlQuery

Implementation:HTTP call, Javaextension

40 / 51

Page 166: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

41 / 51

Page 167: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML (partial)

for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

Rewriting to XQuery

let $_aux_results5 := _xsparql:_sparqlQuery( fn:concat(

"PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT $FName from <relations.rdf>

where { ", $Person, " foaf:knows $Friend . ",

$Person, " foaf:name ", $Name, " .

$Friend foaf:name $FName . } " ) )

for $_aux_result5 at $_aux_result5_pos in $_aux_results5

let $FName := _xsparql:_resultNode( $_aux_result5, "FName" )

41 / 51

Page 168: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML (partial)

for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

Rewriting to XQuery

let $_aux_results5 := _xsparql:_sparqlQuery( fn:concat(

"PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT $FName from <relations.rdf>

where { ", $Person, " foaf:knows $Friend . ",

$Person, " foaf:name ", $Name, " .

$Friend foaf:name $FName . } " ) )

for $_aux_result5 at $_aux_result5_pos in $_aux_results5

let $FName := _xsparql:_resultNode( $_aux_result5, "FName" )

$Person and$Name instanti-ated by the outerloop

41 / 51

Page 169: XSPARQL Tutorial

Lowering: Rewriting to XQuery

Convert FOAF RDF data into XML (partial)

for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

Rewriting to XQuery

let $_aux_results5 := _xsparql:_sparqlQuery( fn:concat(

"PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT $FName from <relations.rdf>

where { ", $Person, " foaf:knows $Friend . ",

$Person, " foaf:name ", $Name, " .

$Friend foaf:name $FName . } " ) )

for $_aux_result5 at $_aux_result5_pos in $_aux_results5

let $FName := _xsparql:_resultNode( $_aux_result5, "FName" )

Replaced by theirvalue in the querystring

41 / 51

Page 170: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 171: XSPARQL Tutorial

Lowering Example

Convert FOAF RDF data into XML

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

42 / 51

Page 172: XSPARQL Tutorial

Lowering Example

Convert FOAF RDF data into XML

$Person and$Name instanti-ated by the outerloop

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

42 / 51

Page 173: XSPARQL Tutorial

Lowering Example

Convert FOAF RDF data into XML

If $Person is ablank node, queryjoining is done bythe $Name variable

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName from <relations.rdf>

where { $Person foaf:knows $Friend .

$Person foaf:name $Name.

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

42 / 51

Page 174: XSPARQL Tutorial

Problem with the Lowering example

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:b1 a foaf:Person; foaf:name "Alice"; foaf:knows _:b2.

_:b4 a foaf:Person; foaf:name "Alice"; foaf:knows _:b3.

_:b2 a foaf:Person; foaf:name "Bob"; foaf:knows _:b3.

_:b3 a foaf:Person; foaf:name "Charles".

<relations>

<person name="Alice">

<knows>Charles</knows>

<knows>Bob</knows>

</person>

<person name="Alice">

<knows>Charles</knows>

<knows>Bob</knows>

</person>

<person name="Bob"><knows>Charles</knows></person>

<person name="Charles"/>

</relations>

43 / 51

Page 175: XSPARQL Tutorial

Problem with the Lowering example

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:b1 a foaf:Person; foaf:name "Alice"; foaf:knows _:b2.

_:b4 a foaf:Person; foaf:name "Alice"; foaf:knows _:b3.

_:b2 a foaf:Person; foaf:name "Bob"; foaf:knows _:b3.

_:b3 a foaf:Person; foaf:name "Charles".

<relations>

<person name="Alice">

<knows>Charles</knows>

<knows>Bob</knows>

</person>

<person name="Alice">

<knows>Charles</knows>

<knows>Bob</knows>

</person>

<person name="Bob"><knows>Charles</knows></person>

<person name="Charles"/>

</relations>

43 / 51

Page 176: XSPARQL Tutorial

Problem with the Lowering example

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:b1 a foaf:Person; foaf:name "Alice"; foaf:knows _:b2.

_:b4 a foaf:Person; foaf:name "Alice"; foaf:knows _:b3.

_:b2 a foaf:Person; foaf:name "Bob"; foaf:knows _:b3.

_:b3 a foaf:Person; foaf:name "Charles".

<relations>

<person name="Alice">

<knows>Charles</knows>

<knows>Bob</knows>

</person>

<person name="Alice">

<knows>Charles</knows>

<knows>Bob</knows>

</person>

<person name="Bob"><knows>Charles</knows></person>

<person name="Charles"/>

</relations>

43 / 51

Page 177: XSPARQL Tutorial

Problem with the Lowering example

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:b1 a foaf:Person; foaf:name "Alice"; foaf:knows _:b2.

_:b4 a foaf:Person; foaf:name "Alice"; foaf:knows _:b3.

_:b2 a foaf:Person; foaf:name "Bob"; foaf:knows _:b3.

_:b3 a foaf:Person; foaf:name "Charles".

<relations>

<person name="Alice">

<knows>Charles</knows>

<knows>Bob</knows>

</person>

<person name="Alice">

<knows>Charles</knows>

<knows>Bob</knows>

</person>

<person name="Bob"><knows>Charles</knows></person>

<person name="Charles"/>

</relations>

43 / 51

Page 178: XSPARQL Tutorial

Scoped Dataset

Scoping Graph

SPARQL query solutions are taken from the scoping graph: agraph that is equivalent to the active graph but does not share anyblank nodes with it or any graph pattern within the query.

Scoped Dataset

The XSPARQL scoped dataset allows to make SPARQL queriesover the previous scoping graph, keeping the same blank nodeassignments

44 / 51

Page 179: XSPARQL Tutorial

Scoped Dataset

Scoping Graph

SPARQL query solutions are taken from the scoping graph: agraph that is equivalent to the active graph but does not share anyblank nodes with it or any graph pattern within the query.

Scoped Dataset

The XSPARQL scoped dataset allows to make SPARQL queriesover the previous scoping graph, keeping the same blank nodeassignments

44 / 51

Page 180: XSPARQL Tutorial

Scoped Dataset Example

Convert FOAF RDF data into XML (Scoped Dataset)

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName

where { $Person foaf:knows $Friend .

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

45 / 51

Page 181: XSPARQL Tutorial

Scoped Dataset Example

Convert FOAF RDF data into XML (Scoped Dataset)

Keep the sameactive datasetthat was last used

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName

where { $Person foaf:knows $Friend .

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

45 / 51

Page 182: XSPARQL Tutorial

Scoped Dataset Example

Convert FOAF RDF data into XML (Scoped Dataset)

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName

where { $Person foaf:knows $Friend .

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

45 / 51

Page 183: XSPARQL Tutorial

Scoped Dataset Example

Convert FOAF RDF data into XML (Scoped Dataset)

Can no longerbe implementedas rewriting to aSPARQL query

declare namespace foaf = "http://xmlns.com/foaf/0.1/";

<relations>

{ for $Person $Name from <relations.rdf>

where { $Person foaf:name $Name }

order by $Name

return <person name="{$Name}">

{ for $FName

where { $Person foaf:knows $Friend .

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}

</relations>

45 / 51

Page 184: XSPARQL Tutorial

Scoped Dataset Example

Query output

<relations>

<person name="Alice">

<knows>Charles</knows>

</person>

<person name="Alice">

<knows>Bob</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

45 / 51

Page 185: XSPARQL Tutorial

Scoped Dataset Example

Query output

<relations>

<person name="Alice">

<knows>Charles</knows>

</person>

<person name="Alice">

<knows>Bob</knows>

</person>

<person name="Bob">

<knows>Charles</knows>

</person>

<person name="Charles"/>

</relations>

45 / 51

Page 186: XSPARQL Tutorial

Constructed Dataset

Assign the result of a construct query to a variable

The variable can then be used as the dataset of a SPARQLquery

Lifting and Lowering query :)

let $ds := for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

return <relations>{ for $Person $Name from $ds

where { $Person foaf:name $Name } order by $Name

return <person name="{$Name}">{ for $FName

where { $Person foaf:knows $Friend .

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}</relations>

46 / 51

Page 187: XSPARQL Tutorial

Constructed Dataset

Assign the result of a construct query to a variable

The variable can then be used as the dataset of a SPARQLquery

Lifting and Lowering query :)

let $ds := for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

return <relations>{ for $Person $Name from $ds

where { $Person foaf:name $Name } order by $Name

return <person name="{$Name}">{ for $FName

where { $Person foaf:knows $Friend .

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}</relations>

46 / 51

Page 188: XSPARQL Tutorial

Constructed Dataset

Assign the result of a construct query to a variable

The variable can then be used as the dataset of a SPARQLquery

Lifting and Lowering query :)

let $ds := for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

return <relations>{ for $Person $Name from $ds

where { $Person foaf:name $Name } order by $Name

return <person name="{$Name}">{ for $FName

where { $Person foaf:knows $Friend .

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}</relations>

46 / 51

Page 189: XSPARQL Tutorial

Constructed Dataset

Assign the result of a construct query to a variable

The variable can then be used as the dataset of a SPARQLquery

Lifting and Lowering query :)

let $ds := for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

return <relations>{ for $Person $Name from $ds

where { $Person foaf:name $Name } order by $Name

return <person name="{$Name}">{ for $FName

where { $Person foaf:knows $Friend .

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}</relations>

$ds contains theRDF Dataset

46 / 51

Page 190: XSPARQL Tutorial

Constructed Dataset

Assign the result of a construct query to a variable

The variable can then be used as the dataset of a SPARQLquery

Lifting and Lowering query :)

let $ds := for $person in doc("relations.xml")//person,

$nameA in $person/@name,

$nameB in $person/knows

construct { [ foaf:name {data($nameA)}; a foaf:Person ] foaf:knows

[ foaf:name {data($nameB)}; a foaf:Person ]. }

return <relations>{ for $Person $Name from $ds

where { $Person foaf:name $Name } order by $Name

return <person name="{$Name}">{ for $FName

where { $Person foaf:knows $Friend .

$Friend foaf:name $FName. }

return <knows> { $FName }</knows>}

</person>}</relations>

Later used in afrom clause

46 / 51

Page 191: XSPARQL Tutorial

Outline

Overview

XPath & XQueryXPathXQueryXQuery Semantics

SPARQL

XSPARQLSyntaxSemanticsImplementationXSPARQL FeaturesQuery examples

Page 192: XSPARQL Tutorial

More expressive SPARQL

Convert between different RDF vocabularies (SPARQL)

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { $X foaf:name $FN.}

from <vCard.ttl>

where { $X vc:FN $FN .}

order by $FN

limit 1 offset 1

47 / 51

Page 193: XSPARQL Tutorial

More expressive SPARQL

Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { _:b foaf:name {fn:concat($N," ", $F)}.}

from <vCard.rdf>

where { $P vc:Given $N. $P vc:Family $F. }

47 / 51

Page 194: XSPARQL Tutorial

More expressive SPARQL

Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { _:b foaf:name {fn:concat($N," ", $F)}.}

from <vCard.rdf>

where { $P vc:Given $N. $P vc:Family $F. }

Constructionof new valuesnot available inSPARQL 1.0

47 / 51

Page 195: XSPARQL Tutorial

More expressive SPARQL

Convert between different RDF vocabularies

prefix vc: <http://www.w3.org/2001/vcard-rdf/3.0#>

prefix foaf: <http://xmlns.com/foaf/0.1/>

construct { _:b foaf:name {fn:concat($N," ", $F)}.}

from <vCard.rdf>

where { $P vc:Given $N. $P vc:Family $F. }

XSPARQL pro-vides you withall the XQueryfunctions

47 / 51

Page 196: XSPARQL Tutorial

Query Example - KML Lowering

Create a KML file from RDF Geolocation data

prefix foaf: <http://xmlns.com/foaf/0.1/>

prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

<kml xmlns="http://www.opengis.net/kml/2.2">{

for $person $name $long $lat

from <http://nunolopes.org/foaf.rdf>

where { $person a foaf:Person; foaf:name $name;

foaf:based_near [ a geo:Point; geo:long $long;

geo:lat $lat ] }

return <Placemark>

<name>{fn:concat("Location of ", $name)}</name>

<Point>

<coordinates>{fn:concat($long, ",", $lat, ",0")}

</coordinates>

</Point>

</Placemark>

}</kml>

48 / 51

Page 197: XSPARQL Tutorial

Query Example - KML Lowering

Create a KML file from RDF Geolocation data

prefix foaf: <http://xmlns.com/foaf/0.1/>

prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

<kml xmlns="http://www.opengis.net/kml/2.2">{

for $person $name $long $lat

from <http://nunolopes.org/foaf.rdf>

where { $person a foaf:Person; foaf:name $name;

foaf:based_near [ a geo:Point; geo:long $long;

geo:lat $lat ] }

return <Placemark>

<name>{fn:concat("Location of ", $name)}</name>

<Point>

<coordinates>{fn:concat($long, ",", $lat, ",0")}

</coordinates>

</Point>

</Placemark>

}</kml>

SPARQL: Per-sons and theirgeographic lo-cations

48 / 51

Page 198: XSPARQL Tutorial

Query Example - KML Lowering

Create a KML file from RDF Geolocation data

prefix foaf: <http://xmlns.com/foaf/0.1/>

prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

<kml xmlns="http://www.opengis.net/kml/2.2">{

for $person $name $long $lat

from <http://nunolopes.org/foaf.rdf>

where { $person a foaf:Person; foaf:name $name;

foaf:based_near [ a geo:Point; geo:long $long;

geo:lat $lat ] }

return <Placemark>

<name>{fn:concat("Location of ", $name)}</name>

<Point>

<coordinates>{fn:concat($long, ",", $lat, ",0")}

</coordinates>

</Point>

</Placemark>

}</kml>

XML repre-senting thespecific KMLfile structure

48 / 51

Page 199: XSPARQL Tutorial

Query Example - KML Lowering

Create a KML file from RDF Geolocation data

prefix foaf: <http://xmlns.com/foaf/0.1/>

prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

<kml xmlns="http://www.opengis.net/kml/2.2">{

for $person $name $long $lat

from <http://nunolopes.org/foaf.rdf>

where { $person a foaf:Person; foaf:name $name;

foaf:based_near [ a geo:Point; geo:long $long;

geo:lat $lat ] }

return <Placemark>

<name>{fn:concat("Location of ", $name)}</name>

<Point>

<coordinates>{fn:concat($long, ",", $lat, ",0")}

</coordinates>

</Point>

</Placemark>

}</kml>

48 / 51

Page 200: XSPARQL Tutorial

Query Example - KML Lifting

Create RDF Geolocation data from a KML file

prefix foaf: <http://xmlns.com/foaf/0.1/>

prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

prefix kml: <http://earth.google.com/kml/2.0>

let $loc := "Departamento de Ingeneria Matematica, U. de Chile"

for $place in doc(fn:concat("http://maps.google.com/?q=",

fn:encode-for-uri($loc),"&amp;num=1&amp;output=kml"))

let $geo := fn:tokenize($place//kml:coordinates, ",")

construct { [] foaf:based_near [ a geo:Point; geo:long {$geo[1]};

geo:lat {$geo[2]} ] }

49 / 51

Page 201: XSPARQL Tutorial

Query Example - KML Lifting

Create RDF Geolocation data from a KML file

prefix foaf: <http://xmlns.com/foaf/0.1/>

prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

prefix kml: <http://earth.google.com/kml/2.0>

let $loc := "Departamento de Ingeneria Matematica, U. de Chile"

for $place in doc(fn:concat("http://maps.google.com/?q=",

fn:encode-for-uri($loc),"&amp;num=1&amp;output=kml"))

let $geo := fn:tokenize($place//kml:coordinates, ",")

construct { [] foaf:based_near [ a geo:Point; geo:long {$geo[1]};

geo:lat {$geo[2]} ] }

Google doesn’t know Departa-mento de Ciencias de la Com-putacion??

49 / 51

Page 202: XSPARQL Tutorial

Query Example - KML Lifting

Create RDF Geolocation data from a KML file

prefix foaf: <http://xmlns.com/foaf/0.1/>

prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

prefix kml: <http://earth.google.com/kml/2.0>

let $loc := "Departamento de Ingeneria Matematica, U. de Chile"

for $place in doc(fn:concat("http://maps.google.com/?q=",

fn:encode-for-uri($loc),"&amp;num=1&amp;output=kml"))

let $geo := fn:tokenize($place//kml:coordinates, ",")

construct { [] foaf:based_near [ a geo:Point; geo:long {$geo[1]};

geo:lat {$geo[2]} ] }

Ask GoogleMaps for theKML descrip-tion

49 / 51

Page 203: XSPARQL Tutorial

Query Example - KML Lifting

Create RDF Geolocation data from a KML file

prefix foaf: <http://xmlns.com/foaf/0.1/>

prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

prefix kml: <http://earth.google.com/kml/2.0>

let $loc := "Departamento de Ingeneria Matematica, U. de Chile"

for $place in doc(fn:concat("http://maps.google.com/?q=",

fn:encode-for-uri($loc),"&amp;num=1&amp;output=kml"))

let $geo := fn:tokenize($place//kml:coordinates, ",")

construct { [] foaf:based_near [ a geo:Point; geo:long {$geo[1]};

geo:lat {$geo[2]} ] }

Construct theupdated RDFgraph

49 / 51

Page 204: XSPARQL Tutorial

Query Example - KML Lifting

Create RDF Geolocation data from a KML file

prefix foaf: <http://xmlns.com/foaf/0.1/>

prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

prefix kml: <http://earth.google.com/kml/2.0>

let $loc := "Departamento de Ingeneria Matematica, U. de Chile"

for $place in doc(fn:concat("http://maps.google.com/?q=",

fn:encode-for-uri($loc),"&amp;num=1&amp;output=kml"))

let $geo := fn:tokenize($place//kml:coordinates, ",")

construct { [] foaf:based_near [ a geo:Point; geo:long {$geo[1]};

geo:lat {$geo[2]} ] }

Output RDF data@prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> .

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:based_near [ a geo:Point ; geo:long "-70.664448" ;

geo:lat "-33.457286" ] ] .

49 / 51

Page 205: XSPARQL Tutorial

Queries over Linked Data

Give me all pairs of co-authors and their joint publications.

let $ds :=

for * from <http://dblp.l3s.de/d2r/resource/authors/Axel_Polleres>

where { $pub dc:creator [] }

construct { { for * from $pub where { $p dc:creator $o . }

construct { $p dc:creator $o } } }

let $allauthors := distinct-values(

for $o from $ds where {$p dc:creator $o}

order by $o return $o)

for $auth at $auth_pos in $allauthors

for $coauth in $allauthors[position() > $auth_pos]

let $commonPubs := count(

{ for $pub from $ds

where { $pub dc:creator $auth, $coauth }

return $pub } )

where ($commonPubs > 0)

construct { [ :author $auth; :author $coauth; :commonPubs $commonPubs ] }

50 / 51

Page 206: XSPARQL Tutorial

Queries over Linked Data

Give me all pairs of co-authors and their joint publications.

let $ds :=

for * from <http://dblp.l3s.de/d2r/resource/authors/Axel_Polleres>

where { $pub dc:creator [] }

construct { { for * from $pub where { $p dc:creator $o . }

construct { $p dc:creator $o } } }

let $allauthors := distinct-values(

for $o from $ds where {$p dc:creator $o}

order by $o return $o)

for $auth at $auth_pos in $allauthors

for $coauth in $allauthors[position() > $auth_pos]

let $commonPubs := count(

{ for $pub from $ds

where { $pub dc:creator $auth, $coauth }

return $pub } )

where ($commonPubs > 0)

construct { [ :author $auth; :author $coauth; :commonPubs $commonPubs ] }

Find all the co-authors of AxelPolleres

50 / 51

Page 207: XSPARQL Tutorial

Queries over Linked Data

Give me all pairs of co-authors and their joint publications.

let $ds :=

for * from <http://dblp.l3s.de/d2r/resource/authors/Axel_Polleres>

where { $pub dc:creator [] }

construct { { for * from $pub where { $p dc:creator $o . }

construct { $p dc:creator $o } } }

let $allauthors := distinct-values(

for $o from $ds where {$p dc:creator $o}

order by $o return $o)

for $auth at $auth_pos in $allauthors

for $coauth in $allauthors[position() > $auth_pos]

let $commonPubs := count(

{ for $pub from $ds

where { $pub dc:creator $auth, $coauth }

return $pub } )

where ($commonPubs > 0)

construct { [ :author $auth; :author $coauth; :commonPubs $commonPubs ] }

And the dis-tinct names

50 / 51

Page 208: XSPARQL Tutorial

Queries over Linked Data

Give me all pairs of co-authors and their joint publications.

let $ds :=

for * from <http://dblp.l3s.de/d2r/resource/authors/Axel_Polleres>

where { $pub dc:creator [] }

construct { { for * from $pub where { $p dc:creator $o . }

construct { $p dc:creator $o } } }

let $allauthors := distinct-values(

for $o from $ds where {$p dc:creator $o}

order by $o return $o)

for $auth at $auth_pos in $allauthors

for $coauth in $allauthors[position() > $auth_pos]

let $commonPubs := count(

{ for $pub from $ds

where { $pub dc:creator $auth, $coauth }

return $pub } )

where ($commonPubs > 0)

construct { [ :author $auth; :author $coauth; :commonPubs $commonPubs ] }

For each dis-tinct pair ofauthors

50 / 51

Page 209: XSPARQL Tutorial

Queries over Linked Data

Give me all pairs of co-authors and their joint publications.

let $ds :=

for * from <http://dblp.l3s.de/d2r/resource/authors/Axel_Polleres>

where { $pub dc:creator [] }

construct { { for * from $pub where { $p dc:creator $o . }

construct { $p dc:creator $o } } }

let $allauthors := distinct-values(

for $o from $ds where {$p dc:creator $o}

order by $o return $o)

for $auth at $auth_pos in $allauthors

for $coauth in $allauthors[position() > $auth_pos]

let $commonPubs := count(

{ for $pub from $ds

where { $pub dc:creator $auth, $coauth }

return $pub } )

where ($commonPubs > 0)

construct { [ :author $auth; :author $coauth; :commonPubs $commonPubs ] }

Count thenumber oftheir sharedpublications

50 / 51

Page 210: XSPARQL Tutorial

Queries over Linked Data

Give me all pairs of co-authors and their joint publications.

let $ds :=

for * from <http://dblp.l3s.de/d2r/resource/authors/Axel_Polleres>

where { $pub dc:creator [] }

construct { { for * from $pub where { $p dc:creator $o . }

construct { $p dc:creator $o } } }

let $allauthors := distinct-values(

for $o from $ds where {$p dc:creator $o}

order by $o return $o)

for $auth at $auth_pos in $allauthors

for $coauth in $allauthors[position() > $auth_pos]

let $commonPubs := count(

{ for $pub from $ds

where { $pub dc:creator $auth, $coauth }

return $pub } )

where ($commonPubs > 0)

construct { [ :author $auth; :author $coauth; :commonPubs $commonPubs ] }

Create theRDF output

50 / 51

Page 211: XSPARQL Tutorial

The end...

http://xsparql.deri.org/Downloads: http://sourceforge.net/projects/xsparql/

new version coming very soon!

Mailing List: [email protected]

This tutorial: http://nunolopes.org/presentations/2010.12.17-XSPARQLTutorial.pdf

51 / 51

Page 212: XSPARQL Tutorial

Outline

Optimisations

Page 213: XSPARQL Tutorial

Optimisations & Benchmarks

Data

Benchmark suite for XML

http://www.xml-benchmark.org/Provides data generator and 20 benchmark queriesData simulates an auction website, containing people, itemsand auctions

Converted XML data to RDF

Queries written using XSPARQL

Query example

List the names of persons and the number of items they bought

52 / 51

Page 214: XSPARQL Tutorial

Optimisations & Benchmarks

Data

Benchmark suite for XML

http://www.xml-benchmark.org/Provides data generator and 20 benchmark queriesData simulates an auction website, containing people, itemsand auctions

Converted XML data to RDF

Queries written using XSPARQL

Query example

List the names of persons and the number of items they bought

52 / 51

Page 215: XSPARQL Tutorial

Optimisations & Benchmarks

Data

Benchmark suite for XML

http://www.xml-benchmark.org/Provides data generator and 20 benchmark queriesData simulates an auction website, containing people, itemsand auctions

Converted XML data to RDF

Queries written using XSPARQL

Query example

List the names of persons and the number of items they bought

52 / 51

Page 216: XSPARQL Tutorial

Optimisations & Benchmarks

Data

Benchmark suite for XML

http://www.xml-benchmark.org/Provides data generator and 20 benchmark queriesData simulates an auction website, containing people, itemsand auctions

Converted XML data to RDF

Queries written using XSPARQL

Query example

List the names of persons and the number of items they bought

52 / 51

Page 217: XSPARQL Tutorial

Benchmark Query example

XQuerylet $auction := doc("input.xml") return

for $p in $auction/site/people/person

let $a := for $t in $auction/site/closed_auctions/closed_auction

where $t/buyer/@person = $p/@id

return $t

return <item person="{$p/name/text()}">{count($a)}</item>

Translation to XSPARQLfor $id $name from <input.rdf>

where { $person a foaf:Person ; :id $id ; foaf:name $name . }

return <item person="{$name}">{

let $x := for * from $rdf

where { $ca a :ClosedAuction ; :buyer [ :id $id ] . }

return $ca

return count($x)

}</item>

53 / 51

Page 218: XSPARQL Tutorial

Benchmark Query example

XQuerylet $auction := doc("input.xml") return

for $p in $auction/site/people/person

let $a := for $t in $auction/site/closed_auctions/closed_auction

where $t/buyer/@person = $p/@id

return $t

return <item person="{$p/name/text()}">{count($a)}</item>

Translation to XSPARQLfor $id $name from <input.rdf>

where { $person a foaf:Person ; :id $id ; foaf:name $name . }

return <item person="{$name}">{

let $x := for * from $rdf

where { $ca a :ClosedAuction ; :buyer [ :id $id ] . }

return $ca

return count($x)

}</item>

53 / 51

Page 219: XSPARQL Tutorial

Rewriting to XQuery

Unoptimised Version

let $_aux_results0 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $id $name from <input.rdf>

WHERE { $person a foaf:Person ; :id $id ; foaf:name $name .}"))

for $_aux_result0 at $_aux_result0_pos in $_aux_results0

let $id := _xsparql:_resultNode( $_aux_result0, "id" )

let $name := _xsparql:_resultNode( $_aux_result0, "name" )

return <item person="{$name}">{

let $x := let $_aux_results2 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $ca FROM <input.rdf>

WHERE { $ca a :ClosedAuction ; :buyer [:id ", $id, "] .}"))

for $_aux_result2 at $_aux_result2_pos in $_aux_results2

let $ca := _xsparql:_resultNode( $_aux_result2, "ca" )

return $ca

return count( $x )

}</item>

54 / 51

Page 220: XSPARQL Tutorial

Rewriting to XQuery

Unoptimised Version

let $_aux_results0 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $id $name from <input.rdf>

WHERE { $person a foaf:Person ; :id $id ; foaf:name $name .}"))

for $_aux_result0 at $_aux_result0_pos in $_aux_results0

let $id := _xsparql:_resultNode( $_aux_result0, "id" )

let $name := _xsparql:_resultNode( $_aux_result0, "name" )

return <item person="{$name}">{

let $x := let $_aux_results2 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $ca FROM <input.rdf>

WHERE { $ca a :ClosedAuction ; :buyer [:id ", $id, "] .}"))

for $_aux_result2 at $_aux_result2_pos in $_aux_results2

let $ca := _xsparql:_resultNode( $_aux_result2, "ca" )

return $ca

return count( $x )

}</item>

Outer SPARQL query

54 / 51

Page 221: XSPARQL Tutorial

Rewriting to XQuery

Unoptimised Version

let $_aux_results0 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $id $name from <input.rdf>

WHERE { $person a foaf:Person ; :id $id ; foaf:name $name .}"))

for $_aux_result0 at $_aux_result0_pos in $_aux_results0

let $id := _xsparql:_resultNode( $_aux_result0, "id" )

let $name := _xsparql:_resultNode( $_aux_result0, "name" )

return <item person="{$name}">{

let $x := let $_aux_results2 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $ca FROM <input.rdf>

WHERE { $ca a :ClosedAuction ; :buyer [:id ", $id, "] .}"))

for $_aux_result2 at $_aux_result2_pos in $_aux_results2

let $ca := _xsparql:_resultNode( $_aux_result2, "ca" )

return $ca

return count( $x )

}</item>

Inner SPARQL query

54 / 51

Page 222: XSPARQL Tutorial

Rewriting to XQuery

Optimised Version (nested loop join)

let $_aux_results4 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $ca $id from <input.rdf>

WHERE { $ca a :ClosedAuction; :buyer [:id $id ] .}") )

let $_aux_results0 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $id $name from <input.rdf>

WHERE { $person a foaf:Person; :id $id; foaf:name $name .}"))

for $_aux_result0 at $_aux_result0_pos in $_aux_results0

let $id := _xsparql:_resultNode( $_aux_result0, "id" )

let $name := _xsparql:_resultNode( $_aux_result0, "name" )

return <item person="{$name}">{

let $x :=

for $_aux_result4 at $_aux_result4_pos in $_aux_results4

where $id = _xsparql:_resultNode( $_aux_result4, "id" )

return _xsparql:_resultNode( $_aux_result4, "ca" )

return count( $x )

}</item>

54 / 51

Page 223: XSPARQL Tutorial

Rewriting to XQuery

Optimised Version (nested loop join)

let $_aux_results4 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $ca $id from <input.rdf>

WHERE { $ca a :ClosedAuction; :buyer [:id $id ] .}") )

let $_aux_results0 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $id $name from <input.rdf>

WHERE { $person a foaf:Person; :id $id; foaf:name $name .}"))

for $_aux_result0 at $_aux_result0_pos in $_aux_results0

let $id := _xsparql:_resultNode( $_aux_result0, "id" )

let $name := _xsparql:_resultNode( $_aux_result0, "name" )

return <item person="{$name}">{

let $x :=

for $_aux_result4 at $_aux_result4_pos in $_aux_results4

where $id = _xsparql:_resultNode( $_aux_result4, "id" )

return _xsparql:_resultNode( $_aux_result4, "ca" )

return count( $x )

}</item>

Inner SPARQL query

54 / 51

Page 224: XSPARQL Tutorial

Rewriting to XQuery

Optimised Version (nested loop join)

let $_aux_results4 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $ca $id from <input.rdf>

WHERE { $ca a :ClosedAuction; :buyer [:id $id ] .}") )

let $_aux_results0 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $id $name from <input.rdf>

WHERE { $person a foaf:Person; :id $id; foaf:name $name .}"))

for $_aux_result0 at $_aux_result0_pos in $_aux_results0

let $id := _xsparql:_resultNode( $_aux_result0, "id" )

let $name := _xsparql:_resultNode( $_aux_result0, "name" )

return <item person="{$name}">{

let $x :=

for $_aux_result4 at $_aux_result4_pos in $_aux_results4

where $id = _xsparql:_resultNode( $_aux_result4, "id" )

return _xsparql:_resultNode( $_aux_result4, "ca" )

return count( $x )

}</item>

Outer SPARQL query

54 / 51

Page 225: XSPARQL Tutorial

Rewriting to XQuery

Optimised Version (nested loop join)

let $_aux_results4 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $ca $id from <input.rdf>

WHERE { $ca a :ClosedAuction; :buyer [:id $id ] .}") )

let $_aux_results0 := _xsparql:_sparqlQuery( fn:concat(

"SELECT $id $name from <input.rdf>

WHERE { $person a foaf:Person; :id $id; foaf:name $name .}"))

for $_aux_result0 at $_aux_result0_pos in $_aux_results0

let $id := _xsparql:_resultNode( $_aux_result0, "id" )

let $name := _xsparql:_resultNode( $_aux_result0, "name" )

return <item person="{$name}">{

let $x :=

for $_aux_result4 at $_aux_result4_pos in $_aux_results4

where $id = _xsparql:_resultNode( $_aux_result4, "id" )

return _xsparql:_resultNode( $_aux_result4, "ca" )

return count( $x )

}</item>

54 / 51

Page 226: XSPARQL Tutorial

Preliminary results look promising...

0

5000

10000

15000

20000

25000

30000

5 10 20 50 100

Tim

e(s

ec)

Dataset size (MB)

XSPARQLoptimised version

Figure: optimisation query 08

55 / 51