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}
CONSTRUCT {
graph
pattern1}
vars
where {
graph
pattern2}
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#>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}
# show the loved wines of the CellarBook SELECT * WHERE {[cb:loved ?wine]}
10 # show the street on which the owner of the Cellarbook lives SELECT ?street WHERE {cb:CellarBook cb:ownedBy [cb:street ?street]}
# show all wines of the cellar
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}
![]()
|
Define namespace prefixes to be used in triples. The prefix
declaration has slightly different syntax than the one used in
Turtle (see line 1‑ |
|
Show all triples. |
|
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 |
|
Use the bracket notation to simplify getting information out of a complex graph structure. |
|
Display all the wines that are part of the |
|
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.