SPARQL 1.1 paths – examples

Note: Examples use a small, simple citations data set taken from Bob DuCharme's "Learning SPARQL". You can download the data at http://www.learningsparql.com/2ndeditionexamples/ex074.ttl

Note: Examples assume the following prefixes:

prefix dc: <http://purl.org/dc/elements/1.1/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix c: <http://learningsparql.com/ns/citations#>
prefix : <http://learningsparql.com/ns/papers#>

Data:

------------------------------------
|    s    |     p      |     o     |
====================================
| :paperA | dc:title   | "Paper A" |
| :paperB | c:cites    | :paperA   |
| :paperB | rdfs:label | "Paper B" |
| :paperC | c:cites    | :paperA   |
| :paperD | c:cites    | :paperA   |
| :paperD | c:cites    | :paperB   |
| :paperE | c:cites    | :paperA   |
| :paperF | c:cites    | :paperC   |
| :paperF | c:cites    | :paperE   |
| :paperG | c:cites    | :paperC   |
| :paperG | c:cites    | :paperE   |
| :paperH | c:cites    | :paperD   |
| :paperI | c:cites    | :paperF   |
| :paperI | c:cites    | :paperG   |
------------------------------------

Property paths: operations

  1. ZeroOrMorePath (elt*)
  2. Example:

    ## find papers that cite paperA, and papers that cite papers that cite paperA, and
    so on
    SELECT ?s
    WHERE {
     ?s c:cites*/dc:title "Paper A" . }
    ORDER BY ?s
    =>
    -----------
    |    s    |
    ===========
    | :paperA |
    | :paperB |
    | :paperC |
    | :paperD |
    | :paperE |
    | :paperF |
    | :paperG |
    | :paperH |
    | :paperI |
    -----------

    Note: this includes :paperA.

    :paperA doesn’t cite anything, but it does have title “Paper A”.

  3. OneOrMorePath (elt+)
  4. Example:

    ## find papers that cite (papers that cite …) papers with title "Paper A"
    SELECT ?s
    WHERE {
     ?s c:cites+/dc:title "Paper A" . }
    ORDER BY ?s
    =>
    -----------
    |    s    |
    ===========
    | :paperB |
    | :paperC |
    | :paperD |
    | :paperE |
    | :paperF |
    | :paperG |
    | :paperH |
    | :paperI |
    ----------

    Note: paperA doesn’t appear here, since we insist there’s at least one “c:cites” step in the path.

  5. ZeroOrOnePath (elt?)
  6. Example:

    ## find papers that cite (papers that cite …) papers with title "Paper A"
    SELECT ?s
    WHERE {
     ?s c:cites?/dc:title "Paper A" . }
    ORDER BY ?s
    =>
    -----------
    |    s    |
    ===========
    | :paperA |
    | :paperB |
    | :paperC |
    | :paperD |
    | :paperE |
    -----------

    Note: Results include papers B, C, D, and E – which directly cite paper A – plus A, which has title “Paper A”.

    If we wanted only papers that directly cite paper A and not paper A itself, we would use no enumeration.

    Example:

    ## find papers that directly cite papers with title "Paper A"
    SELECT ?s
    WHERE {
     ?s c:cites/dc:title "Paper A" . }ORDER BY ?s
    =>
    -----------
    |    s    |
    ===========
    | :paperB |
    | :paperC |
    | :paperD |
    | :paperE |
    -----------

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.