### SPARQL Property Path Pattern Examples
Source: https://www.w3.org/TR/sparql11-query
Illustrates the translation of SPARQL property path patterns into algebra, including examples for sequential paths and quantified paths. These examples show how different SPARQL path syntaxes map to algebra operators like `seq`, `ZeroOrMorePath`, and `Path`.
```pseudocode
Examples of the whole path translation process (`?_V` is a fresh variable):
?s :p/:q ?o
?s :p ?_V .
?_V :q ?o
?s :p* ?o
Path(?s, ZeroOrMorePath(link(:p)), ?o)
:list rdf:rest*/rdf:first ?member
Path(:list, ZeroOrMorePath(link(rdf:rest)), ?_V) .
?_V rdf:first ?member
```
--------------------------------
### Turtle Namespace and Data Example
Source: https://www.w3.org/TR/sparql11-query
Demonstrates how to define namespaces using prefixes and represent RDF data using Turtle syntax. This is a common convention for describing data in SPARQL examples.
```turtle
@prefix dc: .
@prefix : .
:book1 dc:title "SPARQL Tutorial" .
```
--------------------------------
### SPARQL: IRI Syntax Examples
Source: https://www.w3.org/TR/sparql11-query
These examples illustrate different ways to represent IRIs in SPARQL. They show the use of full IRI references, base URIs with relative references, and prefixed names.
```sparql
```
```sparql
BASE
```
```sparql
PREFIX book:
book:book1
```
--------------------------------
### SPARQL Triple Pattern Examples
Source: https://www.w3.org/TR/sparql11-query
Provides examples of SPARQL triple patterns, including the use of prefixes, SELECT queries, and different ways to specify subjects, predicates, and objects. It demonstrates alternative syntaxes for the same query.
```sparql
PREFIX dc:
SELECT ?title
WHERE { dc:title ?title }
```
```sparql
PREFIX dc:
PREFIX :
SELECT $title
WHERE { :book1 dc:title $title }
```
```sparql
BASE
PREFIX dc:
SELECT $title
WHERE { dc:title ?title }
```
--------------------------------
### RDF Data for ASK Query Examples
Source: https://www.w3.org/TR/sparql11-query
Provides sample RDF data using Turtle syntax, defining names and email addresses for individuals.
```turtle
@prefix foaf: .
_:a foaf:name "Alice" .
_:a foaf:homepage .
_:b foaf:name "Bob" .
_:b foaf:mbox .
```
--------------------------------
### Basic Group Graph Pattern Examples in SPARQL
Source: https://www.w3.org/TR/sparql11-query
These examples illustrate basic group graph patterns. They show combinations of triple patterns, filters, and empty groups within a single group, demonstrating how SPARQL processes these structures.
```sparql
{
?x foaf:name ?name .
?x foaf:mbox ?mbox .
}
```
```sparql
{
?x foaf:name ?name . FILTER regex(?name, "Smith")
?x foaf:mbox ?mbox .
}
```
```sparql
{
?x foaf:name ?name .
{}
?x foaf:mbox ?mbox .
}
```
--------------------------------
### RDF Data: Default Graph Example
Source: https://www.w3.org/TR/sparql11-query
Defines an RDF graph in Turtle format. This example shows a default graph containing publisher information for two named graphs. This data is separate from the content within the named graphs themselves.
```turtle
@prefix dc: .
dc:publisher "Bob" .
dc:publisher "Alice" .
```
--------------------------------
### SPARQL Property Path: Sequence Path Example
Source: https://www.w3.org/TR/sparql11-query
Illustrates the use of the sequence path operator (/) in SPARQL to traverse multiple properties in order. This example finds the names of people that a specific person knows.
```sparql
{
?x foaf:mbox .
?x foaf:knows/foaf:name ?name .
}
```
```sparql
{
?x foaf:mbox .
?x foaf:knows/foaf:knows/foaf:name ?name .
}
```
--------------------------------
### SPARQL Property Path: Subproperty Example
Source: https://www.w3.org/TR/sparql11-query
Illustrates how to use property paths to find resources where a property is a subproperty of a specified property, including transitive subproperty relationships (*).
```sparql
{ ?x ?p ?v . ?p rdfs:subPropertyOf* :property }
```
--------------------------------
### SPARQL Aggregate Example with GROUP BY and HAVING
Source: https://www.w3.org/TR/sparql11-query
This example demonstrates SPARQL aggregation using SUM, GROUP BY to group results by organization, and HAVING to filter groups where the total price exceeds 10. It requires data defining organizations, authors, books, and prices.
```sparql
PREFIX :
SELECT (SUM(?lprice) AS ?totalPrice)
WHERE {
?org :affiliates ?auth .
?auth :writesBook ?book .
?book :price ?lprice .
}
GROUP BY ?org
HAVING (SUM(?lprice) > 10)
```
--------------------------------
### RDF Data: Named Graph 'http://example.org/alice' (Merged Example)
Source: https://www.w3.org/TR/sparql11-query
Defines an RDF graph in Turtle format for a named graph identified by the IRI 'http://example.org/alice'. This is part of an RDF merge example, containing FOAF information.
```turtle
@prefix foaf: .
_:a foaf:name "Alice" .
_:a foaf:mbox .
```
--------------------------------
### SPARQL SELECT Projection Example
Source: https://www.w3.org/TR/sparql11-query
Demonstrates how to select specific variables (?nameX, ?nameY, ?nickY) from a dataset using SPARQL. The query projects names and optional nicknames of individuals. It requires FOAF vocabulary.
```turtle
@prefix foaf: .
_:a foaf:name "Alice" .
_:a foaf:knows _:b .
_:a foaf:knows _:c .
_:b foaf:name "Bob" .
_:c foaf:name "Clare" .
_:c foaf:nick "CT" .
```
```sparql
PREFIX foaf:
SELECT ?nameX ?nameY ?nickY
WHERE
{ ?x foaf:knows ?y ;
foaf:name ?nameX .
?y foaf:name ?nameY .
OPTIONAL { ?y foaf:nick ?nickY }
}
```
--------------------------------
### RDF Data: Example for Default Graph
Source: https://www.w3.org/TR/sparql11-query
This is an example of RDF data that would be used as a default graph in a SPARQL query. It defines properties for an individual using foaf:name and foaf:mbox prefixes.
```turtle
@prefix foaf: .
_:a foaf:name "Alice" .
_:a foaf:mbox .
```
--------------------------------
### SPARQL Group Graph Pattern Example
Source: https://www.w3.org/TR/sparql11-query
Demonstrates a SPARQL query with a group graph pattern delimited by curly braces. It shows how triple patterns within the braces are combined to form the query pattern.
```sparql
PREFIX foaf:
SELECT ?name ?mbox
WHERE {
?x foaf:name ?name .
?x foaf:mbox ?mbox .
}
The same solutions would be obtained from a query that grouped the triple patterns into two basic graph patterns. For example, the query below has a different structure but would yield the same solutions as the previous query:
PREFIX foaf:
SELECT ?name ?mbox
WHERE { { ?x foaf:name ?name . }
{ ?x foaf:mbox ?mbox . }
}
```
--------------------------------
### SPARQL Literal Syntax Examples
Source: https://www.w3.org/TR/sparql11-query
Demonstrates various ways to represent literals in SPARQL, including strings with and without language tags or datatypes, numeric literals, and boolean literals. It highlights the convenience of direct numeric and boolean notation.
```sparql
"chat"
```
```sparql
'chat'@fr
```
```sparql
"xyz"^^
```
```sparql
"abc"^^appNS:appDataType
```
```sparql
'''The librarian said, "Perhaps you would enjoy 'War and Peace'."'''
```
```sparql
1
```
```sparql
1.3
```
```sparql
1.300
```
```sparql
1.0e6
```
```sparql
true
```
```sparql
false
```
--------------------------------
### SPARQL Blank Node Abbreviation Examples
Source: https://www.w3.org/TR/sparql11-query
Demonstrates the abbreviated syntax for blank nodes in SPARQL, particularly when a blank node is used as the subject for multiple predicate-object pairs. It shows the equivalence to using explicit blank node labels.
```sparql
[
:p "v"
] .
```
```sparql
[]
:p "v"
.
```
```sparql
_:b57 :p "v" .
```
```sparql
[
:p "v"
] :q "w" .
```
```sparql
_:b57 :p "v" .
_:b57 :q "w" .
```
```sparql
:x :q [
:p "v"
] .
```
```sparql
:x :q _:b57 .
_:b57 :p "v" .
```
```sparql
[
foaf:name ?name ;
foaf:mbox
]
```
```sparql
_:b18 foaf:name ?name .
_:b18 foaf:mbox .
```
--------------------------------
### RDF Data: Example for Combined Default and Named Graphs
Source: https://www.w3.org/TR/sparql11-query
This RDF data illustrates the content for both a default graph (using dc:publisher) and named graphs (using foaf:name and foaf:mbox). This data would be referenced by FROM and FROM NAMED clauses in a SPARQL query.
```turtle
# **Default graph (located at http://example.org/dft.ttl)**
@prefix dc: .
dc:publisher "Bob Hacker" .
dc:publisher "Alice Hacker" .
# **Named graph: http://example.org/bob**
@prefix foaf: .
_:a foaf:name "Bob" .
_:a foaf:mbox .
# **Named graph: http://example.org/alice**
@prefix foaf: .
_:a foaf:name "Alice" .
_:a foaf:mbox .
```
--------------------------------
### SPARQL Property Path: Alternative Path Example
Source: https://www.w3.org/TR/sparql11-query
Demonstrates how to use the alternative path operator (|) in SPARQL to match one or both specified properties. This is useful when a resource might have its title or label represented by different properties.
```sparql
{
:book1 dc:title|rdfs:label ?displayString
}
```
```sparql
{
:book1 | ?displayString
}
```
--------------------------------
### SPARQL UNION for Matching Alternatives
Source: https://www.w3.org/TR/sparql11-query
Illustrates using the UNION keyword in SPARQL to match graph patterns alternatively. This example retrieves book titles, regardless of whether they are tagged with dc10 or dc11 vocabulary.
```sparql
PREFIX dc10:
PREFIX dc11:
SELECT ?title
WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } }
```
--------------------------------
### SPARQL Aggregation Example Translation
Source: https://www.w3.org/TR/sparql11-query
Illustrates the algebraic translation of a SPARQL query with SUM and COUNT aggregates and a GROUP BY clause. It shows how the GROUP BY and aggregate functions are represented algebraically.
```pseudocode
PREFIX rdf:
SELECT (SUM(?val) AS ?sum) (COUNT(?a) AS ?count)
WHERE {
?a rdf:value ?val .
} GROUP BY ?a
Let G := Group((?a), BGP(?a rdf:value ?val))
A1 = Aggregation((?val), Sum, {}, G)
A2 = Aggregation((?a), Count, {}, G)
A := (A1, A2)
Let P := AggregateJoin(A)
```
--------------------------------
### SPARQL VALUES Example: Filtering with Specific Books
Source: https://www.w3.org/TR/sparql11-query
An example of a SPARQL SELECT query using the VALUES clause to filter results for specific books. It retrieves the title and price for books listed in the VALUES block.
```sparql
PREFIX dc:
PREFIX :
PREFIX ns:
SELECT ?book ?title ?price
{
VALUES ?book { :book1 :book3 }
?book dc:title ?title ;
ns:price ?price .
}
```
--------------------------------
### SPARQL Property Path: Alternatives in Arbitrary Length Path
Source: https://www.w3.org/TR/sparql11-query
Shows how to combine alternative paths (|) with arbitrary length path operators (+) in SPARQL. This example finds ancestors of a resource using either 'motherOf' or 'fatherOf' properties.
```sparql
{ ?ancestor (ex:motherOf|ex:fatherOf)+ <#me> }
```
--------------------------------
### SPARQL Property Path: Arbitrary Length Match (Zero or More)
Source: https://www.w3.org/TR/sparql11-query
Demonstrates the zero-or-more path operator (*) in SPARQL for matching paths of any length, including zero. This example retrieves all types and supertypes of a given resource.
```sparql
{ rdf:type/rdfs:subClassOf* ?type }
```
```sparql
{ ?x rdf:type/rdfs:subClassOf* ?type }
```
--------------------------------
### RDF/XML Serialization of CONSTRUCT Results
Source: https://www.w3.org/TR/sparql11-query
This is an example of the RDF/XML serialization for the results of a SPARQL CONSTRUCT query. It represents RDF descriptions with foaf:name properties.
```xml
Alice
Bob
```
--------------------------------
### RDF Description of an Employee
Source: https://www.w3.org/TR/sparql11-query
This is an example of RDF data that might be returned by a DESCRIBE query for an employee. It includes the employee's ID, a SHA1 hash of their email (mbox_sha1sum), and their name structured using the vcard vocabulary. It demonstrates blank node closures and InverseFunctionalProperty.
```turtle
@prefix foaf: .
@prefix vcard: .
@prefix exOrg: .
@prefix rdf: .
@prefix owl:
_:a exOrg:employeeId "1234" ;
foaf:mbox_sha1sum "bee135d3af1e418104bc42904596fe148e90f033" ;
vcard:N
[ vcard:Family "Smith" ;
vcard:Given "John" ] .
foaf:mbox_sha1sum rdf:type owl:InverseFunctionalProperty .
```
--------------------------------
### SPARQL DESCRIBE Query Example
Source: https://www.w3.org/TR/sparql11-query
This SPARQL query uses the DESCRIBE clause to retrieve a description of a resource identified by an employee ID. The result is an RDF graph containing details about the employee, potentially including related information. It utilizes common RDF prefixes like foaf and vcard.
```sparql
PREFIX ent:
DESCRIBE ?x WHERE { ?x ent:employeeId "1234" }
```
--------------------------------
### SPARQL Property Path: Inverse Path Sequence Example
Source: https://www.w3.org/TR/sparql11-query
Combines sequence and inverse path operators to find related resources. This example finds all people who know someone that a given person `?x` knows, excluding `?x` itself.
```sparql
{
?x foaf:knows/^foaf:knows ?y .
FILTER(?x != ?y)
}
```
```sparql
{
?x foaf:knows ?gen1 .
?y foaf:knows ?gen1 .
FILTER(?x != ?y)
}
```
--------------------------------
### Basic Graph Pattern: Two Triples
Source: https://www.w3.org/TR/sparql11-query
Shows a basic graph pattern composed of two triple patterns. This example demonstrates how multiple triple patterns within a group are represented.
```sparql
{ ?s :p1 ?v1 ; :p2 ?v2 }
BGP( ?s :p1 ?v1 . ?s :p2 ?v2 )
```
--------------------------------
### SPARQL VALUES Example: Handling Undefined Values
Source: https://www.w3.org/TR/sparql11-query
This SPARQL query demonstrates using the VALUES clause with UNDEF to handle cases where a variable might not have a defined value for a specific solution. It retrieves book details and shows how UNDEF is represented.
```sparql
PREFIX dc:
PREFIX :
PREFIX ns:
SELECT ?book ?title ?price
{
?book dc:title ?title ;
ns:price ?price .
VALUES (?book ?title)
{
(UNDEF "SPARQL Tutorial")
(:book2 UNDEF)
}
}
```
--------------------------------
### SPARQL Property Path: RDF Collection Elements Example
Source: https://www.w3.org/TR/sparql11-query
Shows how to extract elements from an RDF collection using property paths in SPARQL. This query retrieves all elements from a list identified by ':list'.
```sparql
{ :list rdf:rest*/rdf:first ?element }
```
--------------------------------
### SPARQL SELECT Expression for Calculated Price
Source: https://www.w3.org/TR/sparql11-query
Demonstrates using SELECT expressions in SPARQL to calculate a new variable '?price' based on existing variables '?p' and '?discount'. This example calculates the discounted price of books. It requires DC and custom namespaces.
```turtle
@prefix dc: .
@prefix : .
@prefix ns: .
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book1 ns:discount 0.2 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
:book2 ns:discount 0.25 .
```
```sparql
PREFIX dc:
PREFIX ns:
SELECT ?title (?p*(1-?discount) AS ?price)
{
?x ns:price ?p .
?x dc:title ?title .
?x ns:discount ?discount
}
```
--------------------------------
### SPARQL Property Path: Inverse Path Example
Source: https://www.w3.org/TR/sparql11-query
Shows how to use the inverse path operator (^) in SPARQL to reverse the direction of a property. This allows querying from the object to the subject.
```sparql
{ ?x foaf:mbox }
```
```sparql
{ ^foaf:mbox ?x }
```
--------------------------------
### SPARQL Query: Specify Named Graphs using FROM NAMED
Source: https://www.w3.org/TR/sparql11-query
This SPARQL query illustrates how to specify multiple named graphs using the FROM NAMED clause. The example shows the structure for including graphs identified by two different IRIs.
```sparql
...
FROM NAMED
FROM NAMED
...
```
--------------------------------
### SPARQL GROUP BY Example for Calculating Average
Source: https://www.w3.org/TR/sparql11-query
This SPARQL query demonstrates the GROUP BY clause to group solutions by the value of ?x and then calculates the average of ?y for each group using the AVG aggregate function. It assumes data with subjects having properties :x and :y.
```sparql
SELECT (AVG(?y) AS ?avg)
WHERE {
?a :x ?x ;
:y ?y .
}
GROUP BY ?x
```
--------------------------------
### Check if String Starts With (STRSTARTS)
Source: https://www.w3.org/TR/sparql11-query
The STRSTARTS function checks if the lexical form of the first string literal begins with the lexical form of the second. It aligns with the XPath fn:starts-with function and returns an xsd:boolean. Arguments must be compatible; otherwise, an error occurs. Compatibility rules are detailed in the Argument Compatibility Rules section.
```N/A
xsd:boolean STRSTARTS(string literal arg1, string literal arg2)
```
--------------------------------
### Optional SPARQL Pattern with a FILTER Constraint
Source: https://www.w3.org/TR/sparql11-query
This example demonstrates using a FILTER within an OPTIONAL clause. It retrieves book titles and their prices, but only includes the price if it's less than 30. Books without a price or with a price >= 30 will show no price.
```sparql
@prefix dc: .
@prefix : .
@prefix ns: .
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
```
```sparql
PREFIX dc:
PREFIX ns:
SELECT ?title ?price
WHERE { ?x dc:title ?title .
OPTIONAL { ?x ns:price ?price . FILTER (?price < 30) }
}
```
--------------------------------
### RDF Data: Merged Default Graph Example
Source: https://www.w3.org/TR/sparql11-query
Defines an RDF graph in Turtle format representing a default graph that is an RDF merge of named graphs. Blank nodes are re-labeled to ensure distinctness between the merged graphs and the default graph.
```turtle
@prefix foaf: .
_:x foaf:name "Bob" .
_:x foaf:mbox .
_:y foaf:name "Alice" .
_:y foaf:mbox .
```
--------------------------------
### Evaluate Slice (SPARQL Algebra)
Source: https://www.w3.org/TR/sparql11-query
Slices a list of solution mappings, returning a subset of the results based on start position and length. Operates on the evaluated results of a list or pattern.
```text
eval(D(G), Slice(L, start, length)) = Slice(eval(D(G), L), start, length)
```
--------------------------------
### SPARQL Property Path: Negated Property Set Example
Source: https://www.w3.org/TR/sparql11-query
Demonstrates the use of negated property sets with the '!' operator in SPARQL to find nodes connected by properties, excluding specific ones (e.g., not 'rdf:type' in either direction).
```sparql
{ ?x !(rdf:type|^rdf:type) ?y }
```
--------------------------------
### SPARQL VALUES Example: Post-Query Data Filtering
Source: https://www.w3.org/TR/sparql11-query
This SPARQL query illustrates using the VALUES clause after the main graph pattern. The VALUES block filters the results generated by the preceding SELECT query, demonstrating an alternative placement for data constraints.
```sparql
PREFIX dc:
PREFIX :
PREFIX ns:
SELECT ?book ?title ?price
{
?book dc:title ?title ;
ns:price ?price .
}
VALUES (?book ?title)
{
(UNDEF "SPARQL Tutorial")
(:book2 UNDEF)
}
```
--------------------------------
### ALP Algorithm for RDF Path Traversal
Source: https://www.w3.org/TR/sparql11-query
The ALP (Algorithm for path traversal) function recursively evaluates an RDF path starting from a given term. It keeps track of visited nodes to avoid cycles within a path. The 'eval' function is a prerequisite for path step evaluation.
```pseudo-code
Let eval(x:term, path) be the evaluation of 'path', starting at RDF term x,
and returning a multiset of RDF terms reached
by repeated matches of path.
ALP(x:term, path) =
Let V = empty multiset
ALP(x:term, path, V)
return is V
# V is the set of nodes visited
ALP(x:term, path, V:set of RDF terms) =
if ( x in V ) return
add x to V
X = eval(x,path)
For n:term in X
ALP(n, path, V)
End
```
--------------------------------
### SPARQL Query: Building RDF Graphs with CONSTRUCT
Source: https://www.w3.org/TR/sparql11-query
This SPARQL CONSTRUCT query demonstrates building an RDF graph. It selects employee names and constructs triples using foaf:name based on the matched pattern in the org data.
```sparql
PREFIX foaf:
PREFIX org:
CONSTRUCT { ?x foaf:name ?name }
WHERE { ?x org:employeeName ?name }
```
--------------------------------
### RDF Literals with and without Datatypes
Source: https://www.w3.org/TR/sparql11-query
This RDF snippet shows two examples of date literals. The first is a plain literal without explicit datatype information. The second is an xsd:dateTime typed literal, clearly indicating its data type for precise interpretation and comparison.
```turtle
@prefix a: .
@prefix dc: .
_:a a:annotates .
_:a dc:date "2004-12-31T19:00:00-05:00" .
_:b a:annotates .
_:b dc:date "2004-12-31T19:01:00-05:00"^^ .
```
--------------------------------
### SPARQL Querying Named Graphs with GRAPH
Source: https://www.w3.org/TR/sparql11-query
Illustrates how to use the GRAPH keyword in SPARQL to query specific named graphs within an RDF Dataset. This allows for targeted retrieval of information from different graphs based on their IRIs. The example shows selecting publisher information from the default graph and other details from named graphs.
```sparql
# SPARQL query to select from default and named graphs
# Assumes an RDF Dataset with default graph and named graphs and
SELECT ?publisher ?name ?mbox
WHERE {
# Query the default graph for publisher information
?graphIRI dc:publisher ?publisher .
# Query the named graph for http://example.org/bob
GRAPH {
?s foaf:name ?name .
?s foaf:mbox ?mbox .
FILTER(?name = "Bob")
}
# Query the named graph for http://example.org/alice
GRAPH {
?t foaf:name ?name2 .
?t foaf:mbox ?mbox2 .
FILTER(?name2 = "Alice")
}
# Optional: Join based on some condition if needed, or select distinct info
# For this example, we are just showing how to access different graphs.
# A more realistic query would link data across graphs.
}
```
--------------------------------
### SPARQL DESCRIBE Query by Variable Bound to Name
Source: https://www.w3.org/TR/sparql11-query
Describes resources identified by a variable (?x) bound to the name 'Alice'. If 'Alice' has multiple associated resources, all will be described.
```sparql
PREFIX foaf:
DESCRIBE ?x
WHERE { ?x foaf:name "Alice" }
```
--------------------------------
### SPARQL ASK Query for Existence
Source: https://www.w3.org/TR/sparql11-query
An ASK query to test if a pattern has a solution. It checks for a resource named 'Alice' and returns true if found.
```sparql
PREFIX foaf:
ASK { ?x foaf:name "Alice" }
```
--------------------------------
### SPARQL Property Path: Filtering Duplicates Example
Source: https://www.w3.org/TR/sparql11-query
Demonstrates how to use a FILTER clause with a property path to exclude specific results, such as preventing self-references when traversing relationships like 'knows'.
```sparql
{ ?x foaf:mbox .
?x foaf:knows/foaf:knows ?y .
FILTER ( ?x != ?y )
?y foaf:name ?name
}
```
--------------------------------
### Evaluate Basic Graph Pattern (SPARQL Algebra)
Source: https://www.w3.org/TR/sparql11-query
Evaluates a Basic Graph Pattern (BGP) against a dataset with an active graph. Returns a multiset of solution mappings.
```text
eval(D(G), BGP) = multiset of solution mappings
```
--------------------------------
### Get Timezone as Literal - XPath
Source: https://www.w3.org/TR/sparql11-query
The TZ() function returns the timezone component of an xsd:dateTime argument as a simple literal. It returns an empty string if the dateTime value has no timezone information.
```XPath
simple literal TZ (xsd:dateTime arg)
```
--------------------------------
### SPARQL Query: Data with RDF Term Constraints
Source: https://www.w3.org/TR/sparql11-query
This section provides sample data used for demonstrating SPARQL FILTERs. It includes book titles and prices, which can be used in queries with RDF term constraints.
```sparql
@prefix dc: .
@prefix : .
@prefix ns: .
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
```
--------------------------------
### Remove Solutions with MINUS in SPARQL
Source: https://www.w3.org/TR/sparql11-query
Employs the MINUS operator to exclude solutions from the left-hand side that are compatible with the right-hand side. This example removes individuals named 'Bob' from the results. Requires FOAF prefix.
```sparql
PREFIX :
PREFIX foaf:
SELECT DISTINCT ?s
WHERE {
?s ?p ?o .
MINUS {
?s foaf:givenName "Bob" .
}
}
```
--------------------------------
### SPARQL Arbitrary Length Path Matching (Zero or More)
Source: https://www.w3.org/TR/sparql11-query
Illustrates finding all possible types of a resource, including supertypes, using the 'zero or more' property path operator (*).
```sparql
PREFIX rdfs: .
PREFIX rdf:
SELECT ?x ?type
{
?x rdf:type/rdfs:subClassOf* ?type
}
```
--------------------------------
### Evaluate Project (SPARQL Algebra)
Source: https://www.w3.org/TR/sparql11-query
Projects a list of solution mappings onto a specified set of variables. It selects only the specified variables from the results.
```text
eval(D(G), Project(L, vars)) = Project(eval(D(G), L), vars)
```
--------------------------------
### Get Current DateTime - XPath
Source: https://www.w3.org/TR/sparql11-query
The NOW() function returns an XSD dateTime value representing the current query execution time. All calls within a single query return the same value. The exact moment is unspecified.
```XPath
xsd:dateTime NOW ()
```
--------------------------------
### SPARQL CONSTRUCT WHERE Short Form
Source: https://www.w3.org/TR/sparql11-query
Demonstrates the short form of the CONSTRUCT query, where the template and pattern are identical basic graph patterns. This syntax requires the WHERE keyword.
```sparql
PREFIX foaf:
CONSTRUCT WHERE { ?x foaf:name ?name }
```
--------------------------------
### Numeric Functions (ABS, ROUND, CEIL, FLOOR)
Source: https://www.w3.org/TR/sparql11-query
Provides documentation for ABS, ROUND, CEIL, and FLOOR functions used for numeric operations.
```APIDOC
## Numeric Functions
### Description
These functions perform standard mathematical operations on numeric terms.
### Method
SPARQL Function
### Endpoint
N/A
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Functions
#### 1. ABS (numeric term)
Returns the absolute value of `arg`. An error is raised if `arg` is not a numeric value.
*Example:* `abs(1)` returns `1`, `abs(-1.5)` returns `1.5`.
#### 2. ROUND (numeric term)
Returns the number with no fractional part that is closest to the argument. If there are two such numbers, then the one that is closest to positive infinity is returned. An error is raised if `arg` is not a numeric value.
*Example:* `round(2.4999)` returns `2.0`, `round(2.5)` returns `3.0`, `round(-2.5)` returns `-2.0`.
#### 3. CEIL (numeric term)
Returns the smallest (closest to negative infinity) number with no fractional part that is not less than the value of `arg`. An error is raised if `arg` is not a numeric value.
*Example:* `ceil(10.5)` returns `11.0`, `ceil(-10.5)` returns `-10.0`.
#### 4. FLOOR (numeric term)
Returns the largest (closest to positive infinity) number with no fractional part that is not greater than the value of `arg`. An error is raised if `arg` is not a numeric value.
*Example:* `floor(10.5)` returns `10.0`, `floor(-10.5)` returns `-11.0`.
### Request Example
```sparql
SELECT (ABS(-5) AS ?abs_val)
(ROUND(3.7) AS ?round_val)
(CEIL(8.2) AS ?ceil_val)
(FLOOR(-4.9) AS ?floor_val)
```
### Response
#### Success Response (200)
- **abs_val** (numeric) - The absolute value.
- **round_val** (numeric) - The rounded value.
- **ceil_val** (numeric) - The ceiling value.
- **floor_val** (numeric) - The floor value.
#### Response Example
```json
{
"abs_val": [{"type": "literal", "value": "5"}],
"round_val": [{"type": "literal", "value": "4.0"}],
"ceil_val": [{"type": "literal", "value": "9.0"}],
"floor_val": [{"type": "literal", "value": "-5.0"}]
}
```
### Error Handling
- An error is raised if the argument is not a numeric value.
```
--------------------------------
### Get Language Tag with lang (SPARQL)
Source: https://www.w3.org/TR/sparql11-query
The `lang` function returns the language tag of an RDF literal. If the literal does not have a language tag, it returns an empty string. This function is crucial for filtering or selecting literals based on their language.
```SPARQL
PREFIX foaf:
SELECT ?name ?mbox
WHERE { ?x foaf:name ?name ;
foaf:mbox ?mbox .
FILTER ( lang(?name) = "es" ) }
```
--------------------------------
### SPARQL CONSTRUCT Full Form Comparison
Source: https://www.w3.org/TR/sparql11-query
Presents the full form of a CONSTRUCT query, which is equivalent to the short form. This explicitly separates the CONSTRUCT template from the WHERE clause.
```sparql
PREFIX foaf:
CONSTRUCT { ?x foaf:name ?name }
WHERE
{ ?x foaf:name ?name }
```
--------------------------------
### Sample SPARQL Set Function
Source: https://www.w3.org/TR/sparql11-query
The Sample function returns an arbitrary value from the input multiset. It takes a multiset M and returns an RDF term. The returned value must be present in the input multiset, but the selection is not required to be deterministic. An empty multiset results in an error.
```SPARQL
RDFTerm Sample(multiset M)
Sample(M) = v, where v in Flatten(M)
Sample({}) = error
```
--------------------------------
### SPARQL Query for Literals with Language Tag (No Match)
Source: https://www.w3.org/TR/sparql11-query
This SPARQL query attempts to match a literal without a language tag. It will not find a solution if the data contains a literal with the same string value but includes a language tag, as shown in the example.
```sparql
SELECT ?v WHERE { ?v ?p "cat" }
```
--------------------------------
### Simplification Step for Join
Source: https://www.w3.org/TR/sparql11-query
Simplifies join operations where one of the operands is an empty basic graph pattern (Z). Replaces `join(Z, A)` or `join(A, Z)` with `A`, as Z is the identity for join.
```pseudo
Replace join(Z, A) by A
Replace join(A, Z) by A
```
--------------------------------
### Evaluate Property Path Pattern (SPARQL Algebra)
Source: https://www.w3.org/TR/sparql11-query
Evaluates a Property Path Pattern against a dataset with an active graph. Returns a multiset of solution mappings.
```text
eval(D(G), Path(X, path, Y)) = multiset of solution mappings
```
--------------------------------
### Get Datatype IRI of a Literal (SPARQL)
Source: https://www.w3.org/TR/sparql11-query
The DATATYPE function returns the datatype IRI of a literal. For typed literals, it returns the datatype IRI. For simple literals, it returns 'xsd:string'. For literals with a language tag, it returns 'rdf:langString'.
```sparql
SELECT ?datatype WHERE {
?s ?p ?literal .
BIND(DATATYPE(?literal) AS ?datatype)
}
```
--------------------------------
### Evaluate ToMultiSet (SPARQL Algebra)
Source: https://www.w3.org/TR/sparql11-query
Converts a list of solution mappings back into a multiset. This is the inverse of ToList in some contexts.
```text
eval(D(G), ToMultiSet(L)) = ToMultiSet(eval(D), M))
```
--------------------------------
### SPARQL Aggregation with Property Path
Source: https://www.w3.org/TR/sparql11-query
Shows how to calculate the total cost of an order using a SPARQL property path expression combined with the SUM aggregation function.
```sparql
PREFIX :
SELECT (sum(?x) AS ?total)
{
:order :item/:price ?x
}
```
--------------------------------
### SPARQL DESCRIBE Query by Variable Bound to IRI
Source: https://www.w3.org/TR/sparql11-query
Uses a DESCRIBE query where the resource to be described is identified by a variable (?x) bound in the WHERE clause to an IRI matching a specific email.
```sparql
PREFIX foaf:
DESCRIBE ?x
WHERE { ?x foaf:mbox }
```
--------------------------------
### SPARQL HAVING Example for Filtering Aggregated Results
Source: https://www.w3.org/TR/sparql11-query
This SPARQL query uses the HAVING clause to filter groups based on an aggregate condition. It calculates the average size for each subject and only returns groups where the average size is greater than 10. It requires data where subjects have a :size property.
```sparql
PREFIX :
SELECT (AVG(?size) AS ?asize)
WHERE {
?x :size ?size
}
GROUP BY ?x
HAVING(AVG(?size) > 10)
```
--------------------------------
### SPARQL Query with ORDER BY, LIMIT, and OFFSET
Source: https://www.w3.org/TR/sparql11-query
This SPARQL query demonstrates the combined use of ORDER BY, LIMIT, and OFFSET clauses. It first orders the results by 'name', then skips the first 10 solutions, and finally returns a maximum of 5 solutions, effectively paginating the results.
```sparql
PREFIX foaf:
SELECT ?name
WHERE { ?x foaf:name ?name }
ORDER BY ?name
LIMIT 5
OFFSET 10
```
--------------------------------
### SPARQL Property Path: Arbitrary Length Match (One or More)
Source: https://www.w3.org/TR/sparql11-query
Utilizes the one-or-more path operator (+) in SPARQL to find resources reachable through a property path repeated one or more times. This example finds names of people reachable from Alice via 'foaf:knows'.
```sparql
{
?x foaf:mbox .
?x foaf:knows+/foaf:name ?name .
}
```
--------------------------------
### Predicate Property Path Evaluation (SPARQL)
Source: https://www.w3.org/TR/sparql11-query
Demonstrates the evaluation of a simple predicate property path, equivalent to a basic triple pattern. This is used when the path consists of a single IRI.
```sparql
SELECT * { _X P Y_ }
```
--------------------------------
### Basic Graph Pattern: Single Triple
Source: https://www.w3.org/TR/sparql11-query
Illustrates the simplest form of a basic graph pattern (BGP) with a single triple. It shows the direct mapping of the SPARQL triple pattern to its internal representation.
```sparql
{ ?s ?p ?o }
Join(Z, BGP(?s ?p ?o) )
BGP(?s ?p ?o)
```
--------------------------------
### SPARQL Query Variable Syntax
Source: https://www.w3.org/TR/sparql11-query
Illustrates the syntax for query variables in SPARQL, which are marked by a '?' or '$' prefix. It clarifies that both prefixes denote the same variable within a query.
```sparql
$abc
```
```sparql
?abc
```
--------------------------------
### Sequence Property Path Evaluation (SPARQL)
Source: https://www.w3.org/TR/sparql11-query
Illustrates the evaluation of a sequence property path, which involves two or more paths connected in order. It's equivalent to joining subqueries.
```sparql
SELECT * { X P _:a . _:a Q Y }
```
--------------------------------
### Check for Numeric Value with isNumeric (SPARQL)
Source: https://www.w3.org/TR/sparql11-query
The `isNumeric` function tests if an RDF term represents a numeric value. It returns `true` for terms with appropriate numeric datatypes and valid lexical forms. Examples show that direct numbers are not considered numeric, but strings with numeric datatypes are.
```SPARQL
# Example usage (not a complete query):
# isNumeric(12) returns true
# isNumeric("12") returns false
# isNumeric("12"^^xsd:nonNegativeInteger) returns true
```
--------------------------------
### SPARQL MINUS with Fixed Pattern
Source: https://www.w3.org/TR/sparql11-query
Demonstrates that MINUS with a fixed pattern (e.g., `{ :a :b :c }`) does not eliminate solutions if the main query pattern can be matched, as there are no compatible bindings to remove. Uses a specific prefix.
```sparql
PREFIX :
SELECT *
{
?s ?p ?o
MINUS { :a :b :c }
}
```
--------------------------------
### SPARQL Aggregate Example with Error Handling
Source: https://www.w3.org/TR/sparql11-query
This SPARQL query demonstrates how errors in aggregate calculations (due to non-numeric data or blank nodes) can cause entire groups to be omitted from the results. It calculates AVG and a derived average for groups, showing the impact of invalid data points.
```sparql
PREFIX :
SELECT ?g (AVG(?p) AS ?avg) ((MIN(?p) + MAX(?p)) / 2 AS ?c)
WHERE {
?g :p ?p .
}
GROUP BY ?g
```
--------------------------------
### Alternative Property Path Evaluation (SPARQL)
Source: https://www.w3.org/TR/sparql11-query
Explains the evaluation of an alternative property path, which allows matching either one path or another. This is achieved using the UNION operator.
```sparql
SELECT * { { X P Y } UNION { X Q Y } }
```
--------------------------------
### Basic Graph Pattern, Filter, and Optional Pattern
Source: https://www.w3.org/TR/sparql11-query
Illustrates a sequence of operations: a basic graph pattern, a filter on a variable, and an optional graph pattern. The filter is applied before the optional part is considered.
```sparql
{ ?s :p1 ?v1 FILTER (?v1 < 3 ) OPTIONAL {?s :p2 ?v2} }
Filter( ?v1 < 3 ,
LeftJoin( BGP(?s :p1 ?v1), BGP(?s :p2 ?v2), true) ,
)
```
--------------------------------
### SPARQL SELECT Expressions Algebra
Source: https://www.w3.org/TR/sparql11-query
Details the algebraic steps for processing SELECT expressions, including SELECT * and SELECT with explicit item lists. It handles variable binding and expression evaluation using Extend.
```pseudocode
Let X := algebra from earlier steps
Let VS := list of all variables visible in the pattern, ...
Let PV := {}, a set of variable names
Note, E is a list of pairs of the form (variable, expression), ...
If "SELECT *"
PV := VS
If "SELECT selItem ...:"
For each selItem:
If selItem is a variable
PV := PV ∪ { variable }
End
If selItem is (expr AS variable)
variable must not appear in VS nor in PV; ...
PV := PV ∪ { variable }
E := E append (variable, expr)
End
End
For each pair (var, expr) in E
X := Extend(X, var, expr)
End
Result is X
The set PV is used later for projection.
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.