How to include variable in SPARQL query using PHP

落花浮王杯 提交于 2019-12-14 04:22:40

问题


I am creating a query form and let the user to enter the keyword from the form. Then the query form will bring to the next page where I carry the variable created in the query form to the next page. The excerpt code for the new page is as folows:

//received variable
$abc1=$_POST['querykeyword'];

$querystring = '
Prefix try <http://www.semanticweb.org/ontologies/2009/5/test.owl#>
SELECT ?name ?age
WHERE
  {     ?url try:has-name ${"abc1"} ?name 
        ?url try:has-age ?age }'; 

However, it did not give the output. Can anybody help?


回答1:


Both the query in the question and laalto's answer aren't valid SPARQL, but laalto is getting closer.

It seems like Ismet wants to replace the ?name variable with a fixed value. If so, the ?name variable must be removed from the SELECT and the query body, or it shouldn't parse and certainly won't return the desired results. The PHP used also has the wrong escaping for a T_VARIABLE, the PREFIX was missing a colon required by SPARQL syntax rules.

Try:

  $querystring = "
  PREFIX try: <http://www.semanticweb.org/ontologies/2009/5/test.owl#>
  SELECT ?age
  WHERE {
    ?url try:has-name \"${abc1}\" .
    ?url try:has-age ?age
  }";

This should at least emit a syntactically correct SPARQL query which contains your variable.




回答2:


There seems to be a number of syntax issues in your query. try:has-name property probably has a literal domain. So you should put your literal in quotes. There's also a . missing between the graph patterns, and some other quirks, some of which are related to stackoverflow's formatting of non-code text (fixed by reformatting the question). However, try this:

$querystring = '
  PREFIX try: <http://www.semanticweb.org/ontologies/2009/5/test.owl#>
  SELECT ?name ?age {
    ?url try:has-name ?name .
    ?url try:has-age ?age .
    FILTER(?name = \"${"abc1"}\")
  }';

The PREFIX statement declares the try namespace prefix. Then there's the SELECT clause that selects two variables nameand age. Note that WHERE is optional. The two graph patterns select triples that match both patterns, and the FILTER retains only those triples that satisfy the name matching criteria. @tialaramex's solution would probably also work for you since you already know the name. I'm editing this to be slightly different solution, in case you want to use more complex filtering operations in the future.




回答3:


An old question, but for FWIW: I'd recommend using Fresnel, it's more work initially but you'll have more flexibility and they've already worked through some of the pain points. Fresnel is an RDF presentation vocabulary for describing how to display RDF. You could use a SPARQL CONSTRUCT query to gather your data and then pass it to a Fresnel engine to generate the HTML, with a cache layer in between for performance.

Some Fresnel engine implementations, see Horus especially since you're working in PHP:

  • SIMILE's Fresnel Engine; Java, available in a Maven repository

  • Emmanuel's IsaViz (partial; fully implements FSL); Java, likely will utilize SIMILE engine

  • Freie Universität Berlin's Horus; PHP

  • OlinCollege (unknown)



来源:https://stackoverflow.com/questions/1092182/how-to-include-variable-in-sparql-query-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!