带有特殊字符的sparql查询

发布于 2025-01-05 15:41:25 字数 1444 浏览 0 评论 0原文

我正在尝试通过 Ajax 向 dbperdia 发送查询。它工作正常,但如果搜索词中有特殊字符,我总是收到一条错误消息。

我的代码看起来像

var IKS_QUERY = "PREFIX ontology: <http://dbpedia.org/ontology/>  PREFIX property: <http://dbpedia.org/property/> PREFIX resource: <http://dbpedia.org/resource/> PREFIX position:<http://www.w3.org/2003/01/geo/wgs84_pos#> SELECT DISTINCT ?Abstract ?ThumbnailURL WHERE { resource:"+where+"  ontology:abstract  ?Abstract.  resource:"+where+" ontology:thumbnail ?ThumbnailURL. FILTER (lang(?Abstract)=\"en\")}";
var IKS_URL = "http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" + escape(IKS_QUERY) + "&format=json";

并且在搜索词中具有特殊字符我收到以下错误消息。

Virtuoso 37000 Error SP030: SPARQL compiler, line 3: syntax error at ',' before '_Italy'

    SPARQL query:
    define sql:big-data-const 0 
    #output-format:application/sparql-results+json
    define input:default-graph-uri <http://dbpedia.org> PREFIX ontology: <http://dbpedia.org/ontology/>  PREFIX property: <http://dbpedia.org/property/> PREFIX resource: <http://dbpedia.org/resource/> PREFIX position:<http://www.w3.org/2003/01/geo/wgs84_pos#> SELECT DISTINCT ?Abstract ?ThumbnailURL WHERE { resource:Venice,_Italy  ontology:abstract  ?Abstract.  resource:wherePlaceHolder ontology:thumbnail ?ThumbnailURL. FILTER (lang(?Abstract)="en")}

有什么建议吗?

提前致谢!

I am trying to send a query through Ajax to dbperdia. It works fine but if the search term has special characters in it i get always an error message.

My code looks like

var IKS_QUERY = "PREFIX ontology: <http://dbpedia.org/ontology/>  PREFIX property: <http://dbpedia.org/property/> PREFIX resource: <http://dbpedia.org/resource/> PREFIX position:<http://www.w3.org/2003/01/geo/wgs84_pos#> SELECT DISTINCT ?Abstract ?ThumbnailURL WHERE { resource:"+where+"  ontology:abstract  ?Abstract.  resource:"+where+" ontology:thumbnail ?ThumbnailURL. FILTER (lang(?Abstract)=\"en\")}";
var IKS_URL = "http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=" + escape(IKS_QUERY) + "&format=json";

and having special charactes in search term I get the following error message.

Virtuoso 37000 Error SP030: SPARQL compiler, line 3: syntax error at ',' before '_Italy'

    SPARQL query:
    define sql:big-data-const 0 
    #output-format:application/sparql-results+json
    define input:default-graph-uri <http://dbpedia.org> PREFIX ontology: <http://dbpedia.org/ontology/>  PREFIX property: <http://dbpedia.org/property/> PREFIX resource: <http://dbpedia.org/resource/> PREFIX position:<http://www.w3.org/2003/01/geo/wgs84_pos#> SELECT DISTINCT ?Abstract ?ThumbnailURL WHERE { resource:Venice,_Italy  ontology:abstract  ?Abstract.  resource:wherePlaceHolder ontology:thumbnail ?ThumbnailURL. FILTER (lang(?Abstract)="en")}

any suggestions?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2025-01-12 15:41:25

您生成的查询无效,这与您通过 AJAX 发送它无关。请注意,Virtuoso 错误消息中的查询包含诸如 resource:Venice,_Italy 之类的内容,这是非法的 SPARQL 语法。

一种解决方法是在此处使用完整的 URI,因此您应该构建完整的 URI,而不是像当前的 JS 那样使用构建 QName,例如,

<http://dbpedia.org/resource/" + where + ">

而不是像当前那样构建 QName:

resource:" + where + "

Your generated query is invalid, this has nothing to do with you sending it over AJAX. Notice that the query in the Virtuoso error message has things like resource:Venice,_Italy which is illegal SPARQL syntax.

One workaround is to use full URIs here so instead of using building QNames as your current JS does you should build full URIs e.g.

<http://dbpedia.org/resource/" + where + ">

Instead of QNames like you do currently:

resource:" + where + "
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文