|
| 1 | +<h2>Introduction</h2> |
| 2 | +The queries shown here can be found in the SPARQL specifications: |
| 3 | +<a href="https://www.w3.org/TR/sparql11-query/">https://www.w3.org/TR/sparql11-query/</a> |
| 4 | + |
| 5 | +<h2>query 2.1.6 Examples of Query Syntax</h2> |
| 6 | + |
| 7 | +<pre><code>PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 8 | + SELECT ?title |
| 9 | + WHERE { <http://example.org/book/book1> dc:title ?title } |
| 10 | +</code></pre> |
| 11 | + |
| 12 | +<h2>query 2.1.6-q1 Examples of Query Syntax</h2> |
| 13 | + |
| 14 | +<pre><code>PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 15 | +PREFIX : <http://example.org/book/> |
| 16 | +SELECT $title |
| 17 | +WHERE { :book1 dc:title $title } |
| 18 | +</code></pre> |
| 19 | + |
| 20 | +<h2>query 2.1.6-q2 Examples of Query Syntax</h2> |
| 21 | + |
| 22 | +<pre><code>BASE <http://example.org/book/> |
| 23 | +PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 24 | +SELECT $title |
| 25 | +WHERE { <book1> dc:title ?title } |
| 26 | +</code></pre> |
| 27 | + |
| 28 | +<h2>query 2.5.3 Example of Basic Graph Pattern Matching</h2> |
| 29 | + |
| 30 | +<pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 31 | +SELECT ?mbox |
| 32 | +WHERE |
| 33 | + { ?x foaf:name "Johnny Lee Outlaw" . |
| 34 | + ?x foaf:mbox ?mbox } |
| 35 | +</code></pre> |
| 36 | + |
| 37 | +<h2>query 3.1.1 Matching Integers</h2> |
| 38 | + |
| 39 | +<pre><code>SELECT ?v WHERE { ?v ?p 42 }</code></pre> |
| 40 | + |
| 41 | +<h2>query 3.1.2 Matching Arbitrary Datatypes</h2> |
| 42 | + |
| 43 | +<pre><code>SELECT ?v WHERE { ?v ?p "abc"^^<http://example.org/datatype#specialDatatype> }</code></pre> |
| 44 | + |
| 45 | +<h2>query 3.1.3-q1 Matching Language Tags</h2> |
| 46 | + |
| 47 | +<pre><code>SELECT ?x WHERE { ?x ?p "cat"@en }</code></pre> |
| 48 | + |
| 49 | +<h2>query 3.2 Value Constraints</h2> |
| 50 | + |
| 51 | +<pre><code>PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 52 | +PREFIX ns: <http://example.org/ns#> |
| 53 | +SELECT ?title ?price |
| 54 | +WHERE { ?x ns:price ?price . |
| 55 | + FILTER (?price < 30) . |
| 56 | + ?x dc:title ?title . } |
| 57 | +</code></pre> |
| 58 | + |
| 59 | +<h2>query 5.5 Nested Optional Graph Patterns</h2> |
| 60 | + |
| 61 | +<pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 62 | +PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> |
| 63 | +SELECT ?foafName ?mbox ?gname ?fname |
| 64 | +WHERE |
| 65 | + { ?x foaf:name ?foafName . |
| 66 | + OPTIONAL { ?x foaf:mbox ?mbox } . |
| 67 | + OPTIONAL { ?x vcard:N ?vc . |
| 68 | + ?vc vcard:Given ?gname . |
| 69 | + OPTIONAL { ?vc vcard:Family ?fname } |
| 70 | + } |
| 71 | + } |
| 72 | +</code></pre> |
| 73 | + |
| 74 | +<h2>query 6.1-q2 Joining Patterns with UNION</h2> |
| 75 | + |
| 76 | +<pre><code>PREFIX dc10: <http://purl.org/dc/elements/1.1/> |
| 77 | +PREFIX dc11: <http://purl.org/dc/elements/1.0/> |
| 78 | + |
| 79 | +SELECT ?title ?author |
| 80 | +WHERE { { ?book dc10:title ?title . ?book dc10:creator ?author } |
| 81 | + UNION |
| 82 | + { ?book dc11:title ?title . ?book dc11:creator ?author } |
| 83 | + } |
| 84 | + |
| 85 | +</code></pre> |
| 86 | + |
| 87 | +<h2>query 8.3 Restricting by Bound Variables</h2> |
| 88 | + |
| 89 | +<pre><code>PREFIX data: <http://example.org/foaf/> |
| 90 | +PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 91 | +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 92 | + |
| 93 | +SELECT ?mbox ?nick ?ppd |
| 94 | +WHERE |
| 95 | +{ |
| 96 | + GRAPH data:aliceFoaf |
| 97 | + { |
| 98 | + ?alice foaf:mbox <mailto:alice@work.example> ; |
| 99 | + foaf:knows ?whom . |
| 100 | + ?whom foaf:mbox ?mbox ; |
| 101 | + rdfs:seeAlso ?ppd . |
| 102 | + ?ppd a foaf:PersonalProfileDocument . |
| 103 | + } . |
| 104 | + GRAPH ?ppd |
| 105 | + { |
| 106 | + ?w foaf:mbox ?mbox ; |
| 107 | + foaf:nick ?nick |
| 108 | + } |
| 109 | +} |
| 110 | +</code></pre> |
| 111 | + |
| 112 | +<h2>query 9.3 Combining FROM and FROM NAMED</h2> |
| 113 | + |
| 114 | +<pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 115 | +PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 116 | + |
| 117 | +SELECT ?who ?g ?mbox |
| 118 | +FROM <http://example.org/dft.ttl> |
| 119 | +FROM NAMED <http://example.org/alice> |
| 120 | +FROM NAMED <http://example.org/bob> |
| 121 | +WHERE |
| 122 | +{ |
| 123 | + ?g dc:publisher ?who . |
| 124 | + GRAPH ?g { ?x foaf:mbox ?mbox } |
| 125 | +} |
| 126 | +</code></pre> |
| 127 | + |
| 128 | +<h2>query 10.1.2 DISTINCT</h2> |
| 129 | + |
| 130 | +<pre><code> |
| 131 | + PREFIX foaf: <http://xmlns.com/foaf/0.1//> |
| 132 | +SELECT DISTINCT ?name WHERE { ?x foaf:name ?name } |
| 133 | +</code></pre> |
| 134 | + |
| 135 | +<h2>query 10.1.3-q2 ORDER BY</h2> |
| 136 | + |
| 137 | +<pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 138 | + |
| 139 | +SELECT ?name |
| 140 | +WHERE { ?x foaf:name ?name ; :empId ?emp } |
| 141 | +ORDER BY ?name DESC(?emp) |
| 142 | +</code></pre> |
| 143 | + |
| 144 | +<h2>query 10.1.5 OFFSET</h2> |
| 145 | + |
| 146 | +<pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 147 | + |
| 148 | +SELECT ?name |
| 149 | +WHERE { ?x foaf:name ?name } |
| 150 | +ORDER BY ?name |
| 151 | +LIMIT 5 |
| 152 | +OFFSET 10 |
| 153 | +</code></pre> |
| 154 | + |
| 155 | +<h2>query 10.3.1 Templates with Blank Nodes</h2> |
| 156 | + |
| 157 | +<pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 158 | +PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> |
| 159 | + |
| 160 | +CONSTRUCT { ?x vcard:N _:v . |
| 161 | + _:v vcard:givenName ?gname . |
| 162 | + _:v vcard:familyName ?fname } |
| 163 | +WHERE |
| 164 | + { |
| 165 | + { ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname } . |
| 166 | + { ?x foaf:surname ?fname } UNION { ?x foaf:family_name ?fname } . |
| 167 | + } |
| 168 | +</code></pre> |
| 169 | + |
| 170 | +<h2>query 10.3.3 Solution Modifiers and CONSTRUCT</h2> |
| 171 | + |
| 172 | +<pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 173 | +PREFIX site: <http://example.org/stats#> |
| 174 | + |
| 175 | +CONSTRUCT { [] foaf:name ?name } |
| 176 | +WHERE |
| 177 | +{ [] foaf:name ?name ; |
| 178 | + site:hits ?hits . |
| 179 | +} |
| 180 | +ORDER BY desc(?hits) |
| 181 | +LIMIT 2</code></pre> |
| 182 | + |
| 183 | +<h2>query 10.4.3 Descriptions of Resources</h2> |
| 184 | + |
| 185 | +<pre><code>PREFIX ent: <http://org.example.com/employees#> |
| 186 | +DESCRIBE ?x WHERE { ?x ent:employeeId "1234" } |
| 187 | +</code></pre> |
| 188 | + |
| 189 | +<h2>query 10.5-q1 Asking "yes or no" questions</h2> |
| 190 | + |
| 191 | +<pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 192 | +ASK { ?x foaf:name "Alice" ; |
| 193 | + foaf:mbox <mailto:alice@work.example> } |
| 194 | +</code></pre> |
| 195 | + |
| 196 | +<h2>query 11.4.1 bound</h2> |
| 197 | + |
| 198 | +<pre><code>PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 199 | +PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 200 | +PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> |
| 201 | +SELECT ?name |
| 202 | + WHERE { ?x foaf:givenName ?givenName . |
| 203 | + OPTIONAL { ?x dc:date ?date } . |
| 204 | + FILTER ( bound(?date) ) }</code></pre> |
| 205 | + |
| 206 | +<h2>query 11.4.3 isBlank</h2> |
| 207 | + |
| 208 | +<pre><code> |
| 209 | +PREFIX a: <http://www.w3.org/2000/10/annotation-ns#> |
| 210 | +PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 211 | +PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 212 | + |
| 213 | +SELECT ?given ?family |
| 214 | +WHERE { ?annot a:annotates <http://www.w3.org/TR/rdf-sparql-query/> . |
| 215 | +?annot dc:creator ?c . |
| 216 | +OPTIONAL { ?c foaf:given ?given ; foaf:family ?family } . |
| 217 | +FILTER isBlank(?c) |
| 218 | +}</code></pre> |
| 219 | + |
| 220 | +<h2>query 11.4.5 str</h2> |
| 221 | + |
| 222 | +<pre><code> |
| 223 | + PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 224 | + SELECT ?name ?mbox |
| 225 | + WHERE { ?x foaf:name ?name ; |
| 226 | + foaf:mbox ?mbox . |
| 227 | +FILTER regex(str(?mbox), "@work.example") } |
| 228 | +</code></pre> |
| 229 | + |
| 230 | +<h2>query 11.4.7 datatype</h2> |
| 231 | + |
| 232 | +<pre><code> |
| 233 | + PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| 234 | + PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> |
| 235 | + PREFIX eg: <http://biometrics.example/ns#> |
| 236 | + SELECT ?name ?shoeSize |
| 237 | + WHERE { ?x foaf:name ?name ; eg:shoeSize ?shoeSize . |
| 238 | + FILTER ( datatype(?shoeSize) = xsd:integer ) } |
| 239 | +</code></pre> |
| 240 | + |
| 241 | +<h2>query 11.4.10-q1 RDFterm-equal</h2> |
| 242 | + |
| 243 | +<pre><code> |
| 244 | + PREFIX a: <http://www.w3.org/2000/10/annotation-ns#> |
| 245 | + PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| 246 | + PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> |
| 247 | + |
| 248 | + SELECT ?annotates |
| 249 | + WHERE { ?annot a:annotates ?annotates . |
| 250 | + ?annot dc:date ?date . |
| 251 | + FILTER ( ?date = xsd:dateTime("2004-01-01T00:00:00Z") || ?date = xsd:dateTime("2005-01-01T00:00:00Z") ) } |
| 252 | +</code></pre> |
| 253 | + |
| 254 | +<h2>query 11.6-q1 Extensible Value Testing</h2> |
| 255 | + |
| 256 | +<pre><code> |
| 257 | + PREFIX aGeo: <http://example.org/geo#> |
| 258 | + |
| 259 | + SELECT ?neighbor |
| 260 | + WHERE { ?a aGeo:placeName "Grenoble" . |
| 261 | + ?a aGeo:location ?axLoc . |
| 262 | + ?a aGeo:location ?ayLoc . |
| 263 | + |
| 264 | + ?b aGeo:placeName ?neighbor . |
| 265 | + ?b aGeo:location ?bxLoc . |
| 266 | + ?b aGeo:location ?byLoc . |
| 267 | + |
| 268 | + FILTER ( aGeo:distance(?axLoc, ?ayLoc, ?bxLoc, ?byLoc) < 10 ) . |
| 269 | + } |
| 270 | +</code></pre> |
| 271 | + |
| 272 | +The final example query is not based on the SPARQL 1.1 queries. |
| 273 | + |
| 274 | +<h2>Full Example query</h2> |
| 275 | + |
| 276 | +<pre><code> |
| 277 | + base <http://example.org/geo#> |
| 278 | + prefix geo: <http://www.opengis.net/ont/geosparql#> |
| 279 | + prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 280 | + select ?shape ?shapeColor ?shapeHeight ?shapeName (sample(?shapeLabel) as ?shapeLabel) { |
| 281 | + { |
| 282 | + select * { |
| 283 | + values (?placeName ?streetName) { |
| 284 | + "Grenoble" "Paul Mistral" |
| 285 | + } |
| 286 | + ?place geo:NamePlace ?placeName. |
| 287 | + ?pand geo:hasGeometry/geo:asWKT ?shape; |
| 288 | + } |
| 289 | + |
| 290 | + } |
| 291 | + ?pand geo:measuredHeight ?shapeHeight. |
| 292 | + # Only retrieve buildings larger then 10 meters. |
| 293 | + FILTER ( ?shapeHeight < 10 ) . |
| 294 | + BIND(IF(!bound(?EindGeldigheid5), "#22b14c", "#ed1c24" ) AS?tColor) |
| 295 | + # tekst label |
| 296 | + bind(concat(str(?streetName),' ',str(?houseNumber),', ',str(?PlaceName)) as ?shapeName) |
| 297 | + bind("""Multi-line |
| 298 | + String Element |
| 299 | + """ as ?shapeLabel) |
| 300 | + } |
| 301 | + group by ?shape ?shapeColor ?shapeHeight ?shapeName |
| 302 | + limit 10 |
| 303 | +</code></pre> |
0 commit comments