Visions of Aestia

06 Sep 2005

Exploring RDF with XPath

Filed under: PlanetRDF, XML — JBowtie @ 9:15 pm

If I keep up this string of posts I risk turning into Danny Ayers (not necessarily a bad thing). Most I’ve written in ages.

It’s been my observation that XPath is one of the things keeping people chained to XML. It’s familiar, easy to explore with, and heavily supported. So why not co-opt it for exploring RDF? If we do that, we might be able to show people some of the virtues of the semantic model.

This is a thought experiment, not a concrete, fully-thought out proposal. In fact, it’s probably not even cohesive. I just wanted to get it down in writing while I thought of it.

So here’s a bit of RDF in abbreviated XML format:

<rpg:skill rdf:about="https://nzlinux.org.nz/srd#acrobatics">
<rpg:name>Acrobatics</rpg:name>
<rpg:keyAbility>DEX</rpg:keyAbility>
<rpg:blurb>You can flip, dive, roll, tumble, and perform other acrobatic maneuvers.</rpg:blurb>
</rpg:skill>

Assume we have namespace definitions for rpg and srd and that we’re positioned on the srd:acrobatics node. What kind of XPath expressions could we write?

“rpg:name” would follow the arc and return the value “Acrobatics”.
“rdf:type” would follow the arc and return the “rpg:skill” node. “rdf:type/rdf:label[@xml:lang=’en’]” would return the value “Skill”. Without the qualifier it wound return all the labels.

Assuming owl:instanceOf is the inverse of rdf:type -
“rdf:type/owl:instanceOf” would return all nodes with the “rpg:skill” type.
“rdf:type/owl:instanceOf[rpg:name=’Acrobatics’]” would return the “srd:acrobatics” node.

A couple of XPath constructs would need special interpretation.
There’s no root as such, so “/” would not be a node of itself, but rather simply indicate that the next part of the path should be interpreted as a subject. So, “/srd:acrobatics” positions us on the “srd:acrobatics” node.
“/srd:acrobatics/rpg:name” is therefore the same as the triple “srd:acrobatics rpg:name ?”, whereas “/rpg:name/rdf:type” would presumably return the “rdf:Literal” node.

There’s no parent node as such, so “..” would best be interpreted as following the arc back to the original node. If there is no arc to follow back, it doesn’t do anything.
“/srd:acrobatics/rpg:name/../rpg:keyAbility” would return the value “DEX”.

I think this would be a fruitful area for research. XPath is implicitly graph-oriented and a reasonably well-understood. By using it to explore RDF graphs, it opens up new opportunities to bring people on board and exposes some of the power of the underlying model.

5 Responses to “Exploring RDF with XPath”

  1. Ora Lassila Says:

    XPath applied directly on the XML representation does not make much sense (it does not account for inferred arcs, and multiple XML representations of the same graph may exist). Perhaps some existing path query language for RDF could be “hidden” behind the XPath syntax. For example, Wilbur (http://wilbur-rdf.sourceforge.net/) has such a query language; more details in a paper I wrote a few years ago: http://www.lassila.org/publications/2002/lassila-iswc2002-abstract.shtml

  2. Chimezie Ogbuji Says:

    The idea has been researched considerably. See:

    RDF Templating Language:
    http://www.semanticplanet.com/2003/08/rdft/spec

    Versa (XPath-motivated RDF query language)
    http://en.wikipedia.org/wiki/Versa
    http://www.xml.com/pub/a/2005/07/20/versa.html

    RDF Path
    http://infomesh.net/2003/rdfpath/

    N3 RDF Path
    http://infomesh.net/2002/notation3/#rdfpath

    Rio Path
    http://www.xulplanet.com/ndeakin/arts/reopath/rpath-templates.php

    Tree Hugger
    http://rdfweb.org/people/damian/treehugger/introduction.html

    RxPath
    http://rx4rdf.liminalzone.org/RxPath

  3. Josh Tauberer Says:

    I had the same idea while working on my C# RDF library (http://taubz.for.net/code/semweb). In C#/.NET, there’s an abstract XPathNavigator class you can implement to get XPath over any underlying data. I did that (it’s there but undocumented in the library), and found XPath is really not meant for graphs in general, but like you said could probably be adapted. Another issue were expressions like rdf:type//*, which in XML would return all of the descendents of rdf:type. In RDF that expression might not ever return. In XPath everything has to be a QName, which means namespaces must be set up for every predicate used. And, there are some issues with the built-in XPath functions that take a node as an argument and return the node’s namespace or localname.

    What’s needed, I guess, is a simplified XPath-like format with some extra things, e.g.:

    /foaf:knows[foaf:knows/foaf:name=”Someone Else”]\ex:parentOf

    Which could just be evaluated as getting the values of ?z by matching this graph against the data (with some existing query language/engine):

    foaf:knows ?x .
    ?x foaf:knows ?y .
    ?y foaf:name “Someone Else” .
    ?z ex:parentOf ?x .

    - Josh

  4. Josh Tauberer Says:

    Hmm… HTML glitch. I had <http://taubz.for.net#me> at the beginning of my modified-XPath example and as the first (missing) subject in the RDF graph.

  5. Richard Cyganiak Says:

    Adding to the list above:

    Fresnel Selector Language
    http://www.w3.org/2005/04/fresnel-info/fsl/

Leave a Reply

Powered by WordPress