7.3. RDF queries

As RDF has an XML serialization, we might be tempted to use XSLT to query RDF content, but this is not practical because XSLT would be limited to a given form of XML serialization which is not unique. XSLT is strongly dependent on the syntactic organisation, i.e. the names and nesting of the elements of the XML document. But as we saw above, RDF is notation for a set of triples with many XML possible serializations. RDF queries should instead be based on the underlying triple-based data model and not on its XML form.

The RDF data model being triples, the W3C has proposed SPARQL[2], a query language analogous in goal with SQL in which it is possible to retrieve all triples in the model that correspond to a given pattern. SPARQL uses a notation similar to Turtle to identify triples to be extracted from the RDF database. The main difference being that the subject, the predicate or the object of a triple can be a variable, noted by an identifier preceded by ? or $. Such a triple is called triple pattern, many triple patterns can be combined to form a graph pattern. For example, in Example 7.2: the triple pattern

       cb:JudeRaisin cb:street ?address

matches the triple

       cb:JudeRaisin cb:street "1234 rue des Châteaux"

The triple

       cb:JudeRaisin cb:loved ?wine

matches two triples corresponding to the two wines.

       ?s ?p ?o

matches all triples. The variable name used in many triple patterns is constrained to the same value within a graph pattern.

It is thus possible to create more complex queries. For example,

       cb:CellarBook cb:ownedBy ?owner. 
       ?owner cb:loved ?wine.
            

would match variables ?owner to cb:JudeRaisin and ?wine to wc:C00043125 and wc:C00871996. Should the value of the variable ?owner not be needed, then we can use and blank node as variable simplified using the bracket notation as follows

       cb:CellarBook cb:ownedBy [cb:loved ?wine].

Graph patterns are used in two main types of SPARQL queries:

SELECT vars where {graph pattern}
The output of this query is a table containing the values of the selected variables from all triples that match the graph pattern.
CONSTRUCT {graph pattern1} vars where {graph pattern2}
The values of the variables in triples matching the second graph pattern are determined and used in building new triples corresponding to the first graph pattern. Variables are shared between both graph patterns.

Example 7.3. [CBWC-RDF-S.rq] SPARQL queries on Example 7.2

SPARQL queries to select triples. # is used to comment the rest of the line. As only one SELECT or CONSTRUCT query can be used at a time on RDF database, the other queries should be commented out before executing.

  1 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>            (1)
    PREFIX cb: <http://www.iro.umontreal.ca/lapalme/CellarBook#>
    PREFIX wc: <http://www.iro.umontreal.ca/lapalme/WineCatalog#>
    
  5 # show all triples
    SELECT * WHERE {?s ?p ?o}                                            (2)
    
    # show the loved wines of the CellarBook
    SELECT * WHERE {[cb:loved ?wine]}                                    (3)
 10 
    # show the street on which the owner of the Cellarbook lives
    SELECT ?street WHERE {cb:CellarBook cb:ownedBy [cb:street ?street]}  (4)
    
    # show all wines of the cellar                                       (5)
 15 SELECT ?wine WHERE {cb:CellarBook ?x ?wine FILTER regex(str(?x),"_[0-9]+$")}
    
    # define a new triple with an inverse relation
    CONSTRUCT {?who cb:owns ?what} WHERE {?what cb:ownedBy ?who}         (6)
    

1

Define namespace prefixes to be used in triples. The prefix declaration has slightly different syntax than the one used in Turtle (see line 1‑1 of Example 7.2)

2

Show all triples. * indicates that the value of all variables used in the queries will be displayed.

3

Display the loved wines from the cellar. As the name of the subject is not needed here, we used a blank node as variable as follows [] cb:loved ?wine which can be abbreviated with the bracket notation.

4

Use the bracket notation to simplify getting information out of a complex graph structure.

3

Display all the wines that are part of the Bag. To get them, we filter the triples by matching a regular expression on the string form of the URI. We check that it ends with an underscore followed by numbers.

6

Create a new triple from an existing triple.


Twinkle [89] is a Java application that provides a graphical user interface for SPARQL queries on RDF models. These models can either be local files or any file on the internet. More details about the syntax of SPARQL can be found at [84]. A list of simple queries is shown in [55]. There is also a Java API, used by Twinkle, to make SPARQL queries on an RDF database and get the corresponding triples.



[2] SPARQL is a recursive acronym of SPARQL Protocol and RDF Query Language