Search code examples
sparqlrdf

SPARQL: Specify datatype in INSERT query


Let's same I want to add via a SPARQL query these triples to my graph

<?xml version="1.0"?>

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:my="http://example.com/ontology#">

<rdf:Description rdf:about="http:/example.com/entity/thisEntity">
    <rdf:type rdf:resource="http://example.com/ontology#Identification" />
    <my:someID rdf:datatype="http://www.w3.org/2001/XMLSchema#string">003</my:someID>
    <my:hasSource  rdf:resource="http:/example.com/entity/thisSource" />
  </rdf:Description>

</rdf:RDF>

How am I supposed to specify the data type of the third triple?

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX my: <http://example.com/ontology#>

INSERT DATA {
              <http:/example.com/entity/thisEntity> rdf:type <http://example.com/ontology#Identification>;
                                                 my:hasSource  <http:/example.com/entity/thisSource>;
                                                 my:someID "003"@xsd:string.

            }

Solution

  • You're almost there. The @ is for language tags. Datatypes are appended by using the ^^ separator instead. So use "003"^^xsd:string.

    You will also need to add the namespace prefix to your update:

    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
    

    As an aside: in RDF 1.1, xsd:string is the default datatype for any literal that does not have an explicitly defined datatype nor a language tag. So "003" and "003"^^xsd:string are both the same, string-typed, literal.