RDF Twig Accessing RDF Graphs in XSLT Norman Walsh - - PowerPoint PPT Presentation

rdf twig accessing rdf graphs in xslt
SMART_READER_LITE
LIVE PREVIEW

RDF Twig Accessing RDF Graphs in XSLT Norman Walsh - - PowerPoint PPT Presentation

RDF Twig Accessing RDF Graphs in XSLT Norman Walsh http://www.sun.com/ Extreme Markup Languages 04 - 08 August, 2003 Version 1.0 Introduction There's nothing as practical as a good theory http://www.sun.com/ 2 / 29 Introduction


slide-1
SLIDE 1

RDF Twig Accessing RDF Graphs in XSLT

Version 1.0

http://www.sun.com/

Norman Walsh

Extreme Markup Languages 04 - 08 August, 2003

slide-2
SLIDE 2

“There's nothing as practical as a good theory”

2 / 29 http://www.sun.com/

Introduction

slide-3
SLIDE 3

“There's nothing as practical as a good theory.” “There's nothing as theoretical as good practice.”

3 / 29 http://www.sun.com/

Introduction

slide-4
SLIDE 4

“There's nothing as practical as a good theory.” “There's nothing as theoretical as good practice.” “Sometimes a practical solution is good enough.”

4 / 29 http://www.sun.com/

Introduction

slide-5
SLIDE 5
  • RDF is a useful way to store and process information that

fits into the RDF paradigm.

  • Lots of information does fit into that paradigm.
  • RDF can be serialized in XML.
  • XSLT is a useful way to process XML.

But...

  • Processing RDF with XSLT is difficult and tedious.

5 / 29 http://www.sun.com/

Observations

slide-6
SLIDE 6
  • XSLT (and XPath) are designed to operate on XML documents.

XML documents are trees.

  • A collection of RDF statements is a directed graph, but it is

not generally a tree.

  • Templates designed to transform RDF often stumble over

this missmatch at the data model level.

  • But RDF has an XML serialization, doesn't it?

6 / 29 http://www.sun.com/

The Problem

slide-7
SLIDE 7
  • Nodes in a tree have only one parent.
  • Nodes in a graph may have several “parents”.
  • If node identity is to be preserved:
  • Nodes must be treated in two different ways.
  • It boils down to: instantiate once.
  • Reference elsewhere.

7 / 29 http://www.sun.com/

What about RDF Serialization?

slide-8
SLIDE 8

Consider this small graph: And how it might be serialized: <A> <B> <C> ??? What do you do about A?

8 / 29 http://www.sun.com/

Serialization Example

slide-9
SLIDE 9

<A node="n1"> <B node="n2"> <C node="n3"> <A node="n1"/> <E node="n4"> ...

  • Graph: B/C/A/* = B
  • Tree: B/C/A/* = empty node set

9 / 29 http://www.sun.com/

Serialization Example (Continued)

slide-10
SLIDE 10
  • Know your serialization tool. There are several flavors and

recent RDF changes introduce at least one more.

  • Use keys and conditional logic in your templates to identify

and correctly process nodes that are inline and nodes that are referenced.

  • In the general case, you need a choose statement for each

node, one to test for @rdf:resource and one to test for @rdf:about. Difficult and tedious.

10 / 29 http://www.sun.com/

Working With Serialized RDF

slide-11
SLIDE 11

There's no single, right way to do the serialization.

  • Any node could be the “root” of the tree.
  • Nodes must be instantiated exactly once.
  • Which nodes are “new” and which are “duplicates” depends
  • n where you start and how you build the tree.

11 / 29 http://www.sun.com/

There's More Than One Way To Do It

slide-12
SLIDE 12
  • Let's you start at any node in the RDF graph.
  • Builds a serialized representation of that part of the graph

(with a few user-tuneable parameters).

  • Returns the tree as a document so that you can apply XSLT

to it. In short, RDF Twig lets you serialize interesting parts of the graph on the fly.

12 / 29 http://www.sun.com/

RDF Twig

slide-13
SLIDE 13
  • RDF Twig is implemented as a set of (Java) XSLT extension

functions and elements.

  • The current implementation is built on top of the Jena RDF

toolkit.

13 / 29 http://www.sun.com/

RDF Twig Implementation

slide-14
SLIDE 14

Consider this graph: How can this be serialized (starting at A)?

14 / 29 http://www.sun.com/

How to Serialize

slide-15
SLIDE 15

15 / 29 http://www.sun.com/

Serialize Breadth First

slide-16
SLIDE 16

16 / 29 http://www.sun.com/

Serialize Depth First

slide-17
SLIDE 17

17 / 29 http://www.sun.com/

Serialize Breadth First Deep

slide-18
SLIDE 18

18 / 29 http://www.sun.com/

Serialize a Leaf

slide-19
SLIDE 19

twig A shallow breadth-first tree. dftwig A shallow depth-first tree. branch A deep tree. leaf A “tree” with no instantiated children.

19 / 29 http://www.sun.com/

RDF Twig Terminology

slide-20
SLIDE 20

Load the model: <xsl:variable name="model" select="rt:load('diagrams/bgraph.rdf')"/> Grab a node: <xsl:variable name="A" select="rt:resource('http://uri/for/A')"/> Turn the results into a tree: <xsl:variable name="tree" select="rt:twig($A)"/>

20 / 29 http://www.sun.com/

RDF Twig in Action

slide-21
SLIDE 21

At this point, $tree contains an XML document that can be queried and transformed with XSLT like any other input docu- ment.

RDF Twig in Action (Continued)

slide-22
SLIDE 22

Construct a property: <xsl:variable name="label" select="rt:property('http://example.com/graph#', 'label')"/> Find some nodes: <xsl:variable name="findResults" select="rt:find($label, 'D')"/> Turn the results into a tree: <xsl:variable name="tree" select="rt:twig($findResults)/twig:result"/>

22 / 29 http://www.sun.com/

RDF Twig in Action

slide-23
SLIDE 23
  • load() (and store()) RDF graphs.
  • resource() gets (or creates) a single resource.
  • property() gets (or creates) a property.
  • twig(), dftwig(), branch(), leaf() get parts of a

graph.

23 / 29 http://www.sun.com/

RDF Twig Functions

slide-24
SLIDE 24
  • find() finds resources (that have a property).
  • get() finds resources (that are a property).
  • filter(), filterNot() trim a set of resources.
  • union(), intersection(), difference() perform

the obvious boolean operations on sets of resources.

24 / 29 http://www.sun.com/

RDF Twig Functions (Continued)

slide-25
SLIDE 25

RDF Twig now supports RDQL: <xsl:variable name="a"> <rq:rdql return="a"> SELECT ?a, ?b WHERE (?a, &lt;http://somewhere/pred1&gt;, ?b) AND ?b < 5 </rq:rdql> </xsl:variable> This is a result tree, so you need a node-set extension to ac- cess it.

25 / 29 http://www.sun.com/

RDQL Support

slide-26
SLIDE 26

Wouldn't it be better to extend XPath (XSLT?) to operate over graphs? Yes, probably. But RDF Twig satisfies an immediate need: to access RDF graphs in XSLT stylesheets today.

26 / 29 http://www.sun.com/

Isn't There a Better Way?

slide-27
SLIDE 27

<xsl:variable name="contactType" select="rt:resource('http://nwalsh.com/rdf/palm#Contact')"/> <xsl:variable name="allContacts" select="rt:twig(rt:find($rdf:type, $contactType),1)/twig:result"/> ... <xsl:for-each select="$allContacts"> <xsl:apply-templates select="rt:leaf(string(@rdf:about))" mode="Contact"/> </xsl:for-each>

27 / 29 http://www.sun.com/

A “Real” Example

slide-28
SLIDE 28
  • Deep trees can be prohibitively large.
  • “Serialize on the fly” is conceptually different.
  • Trying to build trees that are “just big enough” sometimes

introduces the inline/reference problem again.

  • Function dispatch oddness in the current implementation.

28 / 29 http://www.sun.com/

Warts

slide-29
SLIDE 29
  • RDF Twig: http://rdftwig.sourceforge.net/
  • Saxon: http://saxon.sourceforge.net/
  • Xalan Java: http://xml.apache.org/xalan-j/
  • Jena: http://www.hpl.hp.com/semweb/jena

29 / 29 http://www.sun.com/

References