为什么在形成SPARQL构造查询时前缀被替换为ns2



我想用构造替换我的SPARQL结果集中的属性,它基本上可以工作,除了前缀会自动替换为"ns2"。有谁知道为什么,以及如何避免它?

查询主管

" PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
" PREFIX ma: <http://www.w3.org/ns/ma-ont#> " +
" PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
" CONSTRUCT { ?subject ma:title ?label }  " +
 " WHERE { "+
 " ?subject rdfs:label ?label. " +

示例结果:

<http://dbpedia.org/resource/Japantown,_San_Francisco> ns2:title  "Japantown, San Francisco"@en 

问题

就其价值而言,看起来耶并没有这样做,而是(假设存在 DBpedia 资源意味着您正在查询 DBpedia(DBpedia 的端点。 给定以下简单数据:

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ma: <http://www.w3.org/ns/ma-ont#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco> rdfs:label  "Japantown, San Francisco"@en .

和这个查询:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ma: <http://www.w3.org/ns/ma-ont#>
CONSTRUCT { ?subject ma:title ?label }
WHERE { 
  ?subject rdfs:label ?label 
}

Jena的ARQ似乎保留了RDF/XML和TURTLE中的前缀:

$ arq --data data.n3 --query construct.sparql --results RDF/XML
<rdf:RDF
    xmlns:ma="http://www.w3.org/ns/ma-ont#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <rdf:Description rdf:about="http://dbpedia.org/resource/Japantown,_San_Francisco">
    <ma:title xml:lang="en">Japantown, San Francisco</ma:title>
  </rdf:Description>
</rdf:RDF>
$ arq --data data.n3 --query construct.sparql
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ma:      <http://www.w3.org/ns/ma-ont#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco>
      ma:title      "Japantown, San Francisco"@en .

但是,在 DBpedia 公共 SPARQL 端点上运行非常相似的查询(请注意将使结果保持较小的值(将获取您提到的自动生成的命名空间前缀。

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ma: <http://www.w3.org/ns/ma-ont#>
CONSTRUCT { ?subject ma:title ?label }
WHERE { 
  VALUES ?subject { <http://dbpedia.org/resource/Japantown,_San_Francisco> }
  ?subject rdfs:label ?label 
}

SPARQL结果

@prefix ns0:    <http://www.w3.org/ns/ma-ont#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco>  ns0:title   "Japantown, San Francisco"@en .

请注意,这些结果是相同的RDF图;前缀只是使某些输出更具可读性的便捷方法。 任何RDF处理工具都会看到它们是完全相同的图形。 耶拿没有做错任何事(事实上,耶拿的很好,因为它保留了前缀(,但在 DBpedia 上运行的 Virtuoso 实例也不是。

保留前缀

虽然前缀仅影响图形序列化的人类可读性

,但图形序列化的人类可读性可能很重要。 有一些方法可以保留前缀,即使 DBpedia 已经在您的领导下对其进行了更改。

使用联合查询 (SERVICE(

命令行上使用 ARQ(如上所示(和本地可用的数据在构建的模型中保留所需的前缀。 我们可以编写一个类似的查询来实际远程查询 DBpedia,但由于 ARQ 的工作是实际构造图形,因此前缀最终会以您期望的方式结束。 例如:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ma: <http://www.w3.org/ns/ma-ont#>
CONSTRUCT { ?subject ma:title ?label }
WHERE { 
  VALUES ?subject { <http://dbpedia.org/resource/Japantown,_San_Francisco> }
  SERVICE <http://dbpedia.org/sparql> {
    ?subject rdfs:label ?label 
  }
}

ARQ 仍然需要一个--data参数,所以我创建了一个空文件,empty-data.n3 . 我们在这里想要的数据实际上来自DBpedia。

$ arq --data empty-data.n3 --query construct-remote.sparql
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ma:      <http://www.w3.org/ns/ma-ont#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco>
      ma:title      "Japantown, San Francisco"@en .

更改耶拿模型中的前缀(前缀映射(

由于耶拿模型是前缀映射,因此您可以更改使用的前缀。 下面是运行远程查询(不使用联合查询(并在之后更新前缀的 Java 代码。

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.vocabulary.RDFS;
public class RemoteQueryPrefixChange {
    final static String MA_NS = "http://www.w3.org/ns/ma-ont#";
    final static String queryString = "" +
            "PREFIX rdfs: <"+RDFS.getURI()+">n" +
            "PREFIX ma: <"+MA_NS+">n" +
            "n" +
            "CONSTRUCT { ?subject ma:title ?label }n" +
            "WHERE {n" +
            "  VALUES ?subject { <http://dbpedia.org/resource/Japantown,_San_Francisco> }n" +
            "  ?subject rdfs:label ?labeln" +
            "}n" +
            ""; 
    final static String DBPEDIA_SERVICE = "http://dbpedia.org/sparql";
    public static void main(String[] args) {
        Model results = QueryExecutionFactory.sparqlService( DBPEDIA_SERVICE, queryString ).execConstruct();
        System.out.println( "== Original Prefixes ==" );
        results.write( System.out, "TTL" );
        System.out.println( "== Updated Prefixes ==" );
        results.removeNsPrefix( results.getNsURIPrefix( MA_NS ));
        results.setNsPrefix( "ma", MA_NS);
        results.write( System.out, "TTL" );
    }
}

输出是(请注意模型的原始序列化中的ns2,更新的序列化中的ma(:

== Original Prefixes ==
@prefix ns2:     <http://www.w3.org/ns/ma-ont#> .
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco>
      ns2:title "Japantown, San Francisco"@en .
== Updated Prefixes ==
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ma:      <http://www.w3.org/ns/ma-ont#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<http://dbpedia.org/resource/Japantown,_San_Francisco>
      ma:title "Japantown, San Francisco"@en .

最新更新