Functions on RDF Terms

isIRI
 xsd:boolean  isIRI (RDF term term)
 xsd:boolean  isURI (RDF term term)

Returns true if term is an IRI . Returns false otherwise. isURI is an alternate spelling for the isIRI operator.

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice".
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  foaf:name       "Bob" .
_:b  foaf:mbox       "bob@work.example" .

This query matches the people with a name and an mbox which is an IRI:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
 WHERE { ?x foaf:name  ?name ;
            foaf:mbox  ?mbox .
         FILTER isIRI(?mbox) }

Query result:

name mbox
"Alice" <mailto:alice@work.example>
isBlank
 xsd:boolean  isBlank (RDF term term)

Returns true if term is a blank node . Returns false otherwise.

@prefix a:          <http://www.w3.org/2000/10/annotation-ns#> .
@prefix dc:         <http://purl.org/dc/elements/1.1/> .
@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a   a:annotates   <http://www.w3.org/TR/rdf-sparql-query/> .
_:a   dc:creator    "Alice B. Toeclips" .

_:b   a:annotates   <http://www.w3.org/TR/rdf-sparql-query/> .
_:b   dc:creator    _:c .
_:c   foaf:given    "Bob".
_:c   foaf:family   "Smith".

This query matches the people with a dc:creator which uses predicates from the FOAF vocabulary to express the name.

PREFIX a:      <http://www.w3.org/2000/10/annotation-ns#>
PREFIX dc:     <http://purl.org/dc/elements/1.1/>
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>

SELECT ?given ?family
WHERE { ?annot  a:annotates  <http://www.w3.org/TR/rdf-sparql-query/> .
  ?annot  dc:creator   ?c .
  OPTIONAL { ?c  foaf:given   ?given ; foaf:family  ?family } .
  FILTER isBlank(?c)
}

Query result:

given family
"Bob" "Smith"

In this example, there were two objects of dc:creator predicates, but only one (_:c) was a blank node.

isLiteral
 xsd:boolean  isLiteral (RDF term term)

Returns true if term is a literal . Returns false otherwise.

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice".
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  foaf:name       "Bob" .
_:b  foaf:mbox       "bob@work.example" .

This query is similar to the one in 17.4.2.1 except that is matches the people with a name and an mbox which is a literal. This could be used to look for erroneous data (foaf:mbox should only have an IRI as its object).

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE { ?x foaf:name  ?name ;
        foaf:mbox  ?mbox .
        FILTER isLiteral(?mbox) }

Query result:

name mbox
"Bob" "bob@work.example"
isNumeric
 xsd:boolean  isNumeric (RDF term term)

Returns true if term is a numeric value. Returns false otherwise. term is numeric if it has an appropriate datatype (see the section Operand Data Types) and has a valid lexical form, making it a valid argument to functions and operators taking numeric arguments.

Examples:

isNumeric(12) true
isNumeric("12") false
isNumeric("12"^^xsd:nonNegativeInteger) true
isNumeric("1200"^^xsd:byte) false
isNumeric(<http://example/>) false
str
 simple literal  STR (literal ltrl)
 simple literal  STR (IRI rsrc)

Returns the lexical form of ltrl (a literal ); returns the codepoint representation of rsrc (an IRI ). This is useful for examining parts of an IRI, for instance, the host-name.

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice".
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  foaf:name       "Bob" .
_:b  foaf:mbox       <mailto:bob@home.example> .

This query selects the set of people who use their work.example address in their foaf profile:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
 WHERE { ?x foaf:name  ?name ;
            foaf:mbox  ?mbox .
         FILTER regex(str(?mbox), "@work\\.example$") }

Query result:

name mbox
"Alice" <mailto:alice@work.example>
lang
 simple literal  LANG (literal ltrl)

Returns the language tag of ltrl, if it has one. It returns "" if ltrl has no language tag . Note that the RDF data model does not include literals with an empty language tag .

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Robert"@en.
_:a  foaf:name       "Roberto"@es.
_:a  foaf:mbox       <mailto:bob@work.example> .

This query finds the Spanish foaf:name and foaf:mbox:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
 WHERE { ?x foaf:name  ?name ;
            foaf:mbox  ?mbox .
         FILTER ( lang(?name) = "es" ) }

Query result:

name mbox
"Roberto"@es <mailto:bob@work.example>
datatype
 iri  DATATYPE (literal literal)

Returns the datatype IRI of a literal.

  • If the literal is a typed literal, return the datatype IRI.
  • If the literal is a simple literal, return xsd:string
  • If the literal is literal with a language tag, return rdf:langString
@prefix foaf:       <http://xmlns.com/foaf/0.1/> .
@prefix eg:         <http://biometrics.example/ns#> .
@prefix xsd:        <http://www.w3.org/2001/XMLSchema#> .

_:a  foaf:name       "Alice".
_:a  eg:shoeSize     "9.5"^^xsd:float .

_:b  foaf:name       "Bob".
_:b  eg:shoeSize     "42"^^xsd:integer .

This query finds the foaf:name and foaf:shoeSize of everyone with a shoeSize that is an integer:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX eg:   <http://biometrics.example/ns#>
SELECT ?name ?shoeSize
 WHERE { ?x foaf:name  ?name ; eg:shoeSize  ?shoeSize .
         FILTER ( datatype(?shoeSize) = xsd:integer ) }

Query result:

name shoeSize
"Bob" 42

In SPARQL 1.0, the DATATYPE function was not defined for literals with a language tag. Therefore, an unextended implementation would raise an error when DATATYPE was called with a literal with a language tag. Operator extensibility allows implementations to return a result rather than raise an error. SPARQL 1.1 defines the result of DATATYPE applied to a literal with a language tag to be rdf:langString.

The SPARQL Working Group is using

rdf:langString based on the latest Working Drafts of the RDF Working Group. This usage should be considered experimental (and non-normative) until/unless

rdf:langString becomes part of an updated RDF Recommendation.

IRI
 iri  IRI(simple literal)
 iri  IRI(xsd:string)
 iri  IRI(iri)
 iri  URI(simple literal)
 iri  URI(xsd:string)
 iri  URI(iri)

The IRI function constructs an IRI by resolving the string argument (see RFC 3986 and RFC 3987 or any later RFC that superceeds RFC 3986 or RFC 3987). The IRI is resolved against the base IRI of the query and must result in an absolute IRI.

The URI function is a synonym for IRI.

If the function is passed an IRI, it returns the IRI unchanged.

Passing any RDF term other than a simple literal, xsd:string or an IRI is an error.

An implementation MAY normalize the IRI.

Examples:

IRI("http://example/") <http://example/>
IRI(<http://example/>) <http://example/>
BNODE
blank node  BNODE()
blank node  BNODE(simple literal)
blank node  BNODE(xsd:string)

The BNODE function constructs a blank node that is distinct from all blank nodes in the dataset being queried and distinct from all blank nodes created by calls to this constructor for other query solutions. If the no argument form is used, every call results in a distinct blank node. If the form with a simple literal is used, every call results in distinct blank nodes for different simple literals, and the same blank node for calls with the same simple literal within expressions for one solution mapping.

This functionality is compatible with the treatment of blank nodes in SPARQL CONSTRUCT templates.

17.4.2.10 STRDT
literal  STRDT(simple literal lexicalForm, IRI datatypeIRI)

The STRDT function constructs a literal with lexical form and type as specified by the arguments.

STRDT("123", xsd:integer) "123"^^<http://www.w3.org/2001/XMLSchema#integer>
STRDT("iiii", <http://example/romanNumeral>) "iiii"^^<http://example/romanNumeral>
STRLANG
literal  STRLANG(simple literal lexicalForm, simple literal langTag)

The STRLANG function constructs a literal with lexical form and language tag as specified by the arguments.

STRLANG("chat", "en") "chat"@en
UUID
iri  UUID()

Return a fresh IRI from the UUID URN scheme. Each call of UUID() returns a different UUID. It must not be the "nil" UUID (all zeroes). The variant and version of the UUID is implementation dependent.

UUID() <urn:uuid:b9302fb5-642e-4d3b-af19-29a8f6d894c9>
STRUUID
simple literal  STRUUID()

Return a string that is the scheme specific part of UUID. That is, as a simple literal, the result of generating a UUID, converting to a simple literal and removing the initial urn:uuid:.

STRUUID() "73cd4307-8a99-4691-a608-b5bda64fb6c1"