SPARQL INSERT not working with PUT method. why?

2019-08-02 16:53发布

问题:

I am trying to create a new object with PUT method and to add some of my own prefixes with SPARQL query. But, the object is being created without the added prefixes. It works with POST and PATCH though. Why and is there alternative way for SPARQL to use with PUT method and add using user-defined prefixes?

 PREFIX dc: <http://purl.org/dc/elements/1.1/>
 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 PREFIX indexing: <http://fedora.info/definitions/v4/indexing#>

 DELETE { }
 INSERT {
   <> indexing:hasIndexingTransformation "default";
      rdf:type indexing:Indexable;
      dc:title "title3";
      dc:identifier "test:10";
 }
 WHERE { }

What I am saying was all the above values specified in the insert clause are not added at all.

EDIT1:

url = 'http://example.com/rest/object1'
payload = """
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX indexing: <http://fedora.info/definitions/v4/indexing#>
PREFIX custom: <http://customnamespaces/custom#/>
DELETE { }
INSERT {
<> indexing:hasIndexingTransformation "default"; 
rdf:type indexing:Indexable; 
dc:title "title1";
custom:objState "Active";
custom:ownerId "Owner1";
dc:identifier "object1";
}
WHERE { }
""" 
headers = {
    'content-type': "application/sparql-update",
    'cache-control': "no-cache"
    }
response = requests.request("PUT", url, data=payload, headers=headers, auth=('username','password'))

回答1:

Prefixes are not triples and therefore cannot be added using a SPARQL query. You can always specify prefixes in the SPARQL query and it will generate the correct URI for storage in your triple store.

Also note that your custom namespace is errantly defined by ending with both a hash and a slash. It should be either PREFIX custom: <http://customnamespaces/custom#> or PREFIX custom: <http://customnamespaces/custom/>.

I.e. by your query indexing:hasIndexingTransformation will be stored in the triple store as <http://fedora.info/definitions/v4/indexing#hasIndexingTransformation>.

There is no reason to store the prefix in the triple store (actually, prefixes are an artifact of the text serialization, not the data itself), so you can subsequently query this data in one of two ways.

1) Using a prefix

PREFIX indexing: <http://fedora.info/definitions/v4/indexing#>
SELECT ?o {
   [] indexing:hasIndexingTransformation ?o .
}

2) Using the full URI:

SELECT ?o {
   [] <http://fedora.info/definitions/v4/indexing#hasIndexingTransformation> ?o .
}