Search code examples
pythonrdfsesamesparqlwrapper

sparql query: URL as a parameter, using with Python and Sesame-Workbench


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.


Solution

  • 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