如何消除SPARQL中的空结果集?

发布于 2024-12-19 03:52:38 字数 1706 浏览 8 评论 0原文

我正在语义网下做一个项目。这是我正在使用的 RDF 文件的示例




    aricent
   www.placementhub.net
  

如果用户输入“prepare for aricent”来搜索链接,我以一种方式编码以获取每个值“prepare”,“for”,“aricent”并动态生成SPARQL查询以查找相关记录。这是SPARQL查询

字符串 queryString ="前缀 rdf: http://www.w3.org/1999/02/22- rdf-语法-ns# " + "前缀 foaf: http://www.xmlns.com/foaf/0.1 " + "SELECT ?name ?url WHERE { ?a foaf:name ?name FILTER regex(?name,'"+ value[i]+"') ?a foaf:url ?url }";

其中,values[i] 现在在第一个循环中为“prepare”,在第二个循环中为“for”,在第三个循环中为“aricent”。 在这种情况下,如果执行它,我将得到 as

--------------------------------------
| name       | url                   |
======================================
|            |                       |
--------------------------------------

--------------------------------------
| name       | url                   |
======================================
|            |                       |
--------------------------------------

--------------------------------------
| name       | url                   |
======================================
| "aricent" | "www.placementhub.net" |
--------------------------------------

因此,“prepare”和“for”的结果集是空的(没有值),并且仅获得“aricent”的结果,因为它在 RDF 中可用。请您电话我如何消除(仅应向用户显示有价值的表)获得的空结果集?我使用了 JENA API。谢谢

I am doing a project under Semantic Web.This is an example of RDF file i am using

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:foaf="http://www.xmlns.com/foaf/0.1">
<rdf:Description>
    <foaf:name>aricent</foaf:name>
   <foaf:url>www.placementhub.net</foaf:url>
  </rdf:Description>
</rdf:RDF>

if user enters "prepare for aricent" for searching the link,I coded in a way to take each value "prepare","for","aricent" and dynamically generate SPARQL query to find the related record.This is the SPARQL query

String queryString ="PREFIX rdf:
http://www.w3.org/1999/02/22-rdf-syntax-ns# " +
"PREFIX foaf: http://www.xmlns.com/foaf/0.1 " +
"SELECT ?name ?url WHERE { ?a foaf:name ?name FILTER regex(?name,'"+ values[i]+"') ?a foaf:url ?url }";

where values[i] is now " prepare" in 1st loop,"for" in 2nd loop and "aricent" in 3rd loop.
In such a case if it is executed i will get as

--------------------------------------
| name       | url                   |
======================================
|            |                       |
--------------------------------------

--------------------------------------
| name       | url                   |
======================================
|            |                       |
--------------------------------------

--------------------------------------
| name       | url                   |
======================================
| "aricent" | "www.placementhub.net" |
--------------------------------------

So the resultset is empty(with no values) for "prepare" and "for" and result is got only for "aricent" as it is available in RDF.Can you please tel me how to eliminate(only the table that has value should be shown to the user) the empty resultsets obtained?i used JENA API's.Thank You

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

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

发布评论

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

评论(1

一抹淡然 2024-12-26 03:52:38

我不确定您在这里所说的消除是什么意思,但您可以在一个查询中完成所有这些操作,例如:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://www.xmlns.com/foaf/0.1>
SELECT ?name ?url
WHERE {
  ?a foaf:name ?name
  FILTER regex(?name,'(prepare|for|aricent)')
  ?a foaf:url ?url
}

它将返回包含任何这些单词的任何字符串。

或者,如果您只想要包含所有这些单词的字符串,您可以

  FILTER regex(?name,'prepare')
  FILTER regex(?name,'for')
  FILTER regex(?name,'aricent')

这样做从您的问题中不清楚您想要实现什么目标。

I'm not sure what you mean by eliminate here, but you could do all this in one query, like:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://www.xmlns.com/foaf/0.1>
SELECT ?name ?url
WHERE {
  ?a foaf:name ?name
  FILTER regex(?name,'(prepare|for|aricent)')
  ?a foaf:url ?url
}

Which will return any string that contains any of those words.

Alternatively if you only want strings that have all those words you could do

  FILTER regex(?name,'prepare')
  FILTER regex(?name,'for')
  FILTER regex(?name,'aricent')

It's not clear from your question what you're trying to achieve.

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