Dynamic SPARQL queries and Datatypes
After some headaches I’ve found how to write dynamic SPARQL queries by passing parameters. For example, to query graph about resources collected last hour this code could be a first approach:
#!/usr/bin/env python
# Dynamic query (example code)
from datetime import datetime
import rdflib
from namespaces import XSD
def last_day():
""" get date """
now = datetime.now()
date = now - timedelta(hours=24)
return date
def get_date_filtered_query():
""" build query """
period = last_day()
mydate = Literal(period, datatype=URIRef(XSD))
query = """
PREFIX rdf:
PREFIX nie:
PREFIX nfo:
PREFIX nao:
PREFIX pimo:
PREFIX xsd:
SELECT ?id
WHERE
{
?id rdf:type ?type .
?id nao:created ?date .
FILTER (?type != pimo:Collection) .
FILTER (xsd:dateTime(?date) >= xsd:dateTime("%s")) .
}
ORDER BY DESC(?date)
""" % mydate
return query
# query graph
query = get_date_filtered_query()
result = graph.query(query)
for id in result.selected:
print id
For more information is worth to take a look W3C XML Schema Definition Language (XSD) 1.1 Part 2: Datatypes. A self explained list of XML datatypes:
- xsd:anyURI – URI (Uniform Resource Identifier)
- xsd:base64Binary – Binary content coded as “base64″
- xsd:boolean – Boolean (true or false)
- xsd:byte – Signed value of 8 bits
- xsd:date – Gregorian calendar date
- xsd:dateTime – Instant of time (Gregorian calendar)
- xsd:decimal – Decimal numbers
- xsd:double – IEEE 64-bit floating-point
- xsd:duration – Time durations
- xsd:ENTITIES – Whitespace-separated list of unparsed entity references
- xsd:ENTITY – Reference to an unparsed entity
- xsd:float – IEEE 32-bit floating-point
- xsd:gDay – Recurring period of time: monthly day
- xsd:gMonth – Recurring period of time: yearly month
- xsd:gMonthDay – Recurring period of time: yearly day
- xsd:gYear – Period of one year
- xsd:gYearMonth – Period of one month
- xsd:hexBinary – Binary contents coded in hexadecimal
- xsd:ID – Definition of unique identifiers
- xsd:IDREF – Definition of references to unique identifiers
- xsd:IDREFS – Definition of lists of references to unique identifiers
- xsd:int – 32-bit signed integers
- xsd:integer – Signed integers of arbitrary length
- xsd:language – RFC 1766 language codes
- xsd:long – 64-bit signed integers
- xsd:Name – XML 1.O name
- xsd:NCName – Unqualified names
- xsd:negativeInteger – Strictly negative integers of arbitrary length
- xsd:NMTOKEN – XML 1.0 name token (NMTOKEN)
- xsd:NMTOKENS – List of XML 1.0 name tokens (NMTOKEN)
- xsd:nonNegativeInteger – Integers of arbitrary length positive or equal to zero
- xsd:nonPositiveInteger – Integers of arbitrary length negative or equal to zero
- xsd:normalizedString – Whitespace-replaced strings
- xsd:NOTATION – Emulation of the XML 1.0 feature
- xsd:positiveInteger – Strictly positive integers of arbitrary length
- xsd:QName – Namespaces in XML-qualified names
- xsd:short – 32-bit signed integers
- xsd:string – Any string
- xsd:time – Point in time recurring each day
- xsd:token – Whitespace-replaced and collapsed strings
- xsd:unsignedByte – Unsigned value of 8 bits
- xsd:unsignedInt – Unsigned integer of 32 bits
- xsd:unsignedLong – Unsigned integer of 64 bits
- xsd:unsignedShort – Unsigned integer of 16 bits

