1

I am trying to send a URL as a parameter to the sesame workbench, using sparql. How can I do that?

More specifically, A working sample from my code is;

p="OSPF"
queryString = ("""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX FOAF: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://something.com>

INSERT DATA {
: rdf:type FOAF:productdetails ;
FOAF:price :%s .
}""")% p

In the output the object for the predicate 'price' is 'http://something.com/OSPF'. I want www.myurl.com as the object.

Aravind
  • 15
  • 4

1 Answers1

0

I'm no Python programmer, but it looks like you're using string formatting (with the % operator) to insert a value into your SPARQL query. In this instance, the value of p ("OSPF") is inserted into your string, replacing the %s. So the outcome of this is:

  FOAF:price :OSPF .

The : in front of OSPF means that you are using a prefixed name here, with the default prefix. The default prefix is defined in your namespace declarations as:

  PREFIX : <http://something.com>

This leads to the eventual URI inserted in your SPARQL query being:

  FOAF:price <http://something.comOSPF> .

If you want a different URL, you have to change the way this is substituted. One solution is to have the full URI as the value of p. You then need to remove the leading colon from %s and instead surround it with angle brackets (to indicate it's a full URI):

   p = "http://www.myurl.com/OSPF" 
   queryString = ("""
   PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
   PREFIX FOAF: <http://www.w3.org/2000/01/rdf-schema#>
   PREFIX : <http://something.com>

   INSERT DATA {
    : rdf:type FOAF:productdetails ;
      FOAF:price <%s> .
    }""")% p

Another option is to introduce another namespace declaration in your SPARQL query, and use that in front of %s. For example:

   p = "OSPF" 
   queryString = ("""
   PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
   PREFIX FOAF: <http://www.w3.org/2000/01/rdf-schema#>
   PREFIX my: <http://www.myurl.com/> 
   PREFIX : <http://something.com>

   INSERT DATA {
    : rdf:type FOAF:productdetails ;
      FOAF:price my:%s .
    }""")% p
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • I have no idea what you mean with your first clarification. If there is something missing from your question, can you edit it to include the necessary information? – Jeen Broekstra Apr 29 '16 at 03:56
  • Edited: Two clarifications;1. IF I am using <%s> inside the query, the object part of triplet output (when viewing the output of 'SELECT * where { ?a ?b ?c }', with the direct query facility of sesame-work bench) also contains the angle brackets like . How can I get rid of it? _one more thing is_ if I want to pass a variable containing some special characters(especially spaces), how to do that? Thanks in advance. – Aravind Apr 29 '16 at 04:58
  • That sounds more like a different question really. If you have a new question post it *as a separate new question*. You can provide a link back to this question if it helps provide context. – Jeen Broekstra Apr 29 '16 at 20:16
  • Sure. The link follows: http://stackoverflow.com/questions/36974964/url-special-character-parameter-in-sparql-query-python-sesasmworkbench – Aravind May 02 '16 at 08:17