简体   繁体   English

使用Turtle文件的SPARQL查询(公共数据源)

[英]SPARQL query with Turtle file (Public data source)

I am new to Turtle format files and querying them with SPARQL. 我是Turtle格式文件的新手,并使用SPARQL查询它们。 So I have many questions to be solved, I hope you can help me! 因此,我有许多问题需要解决,希望您能帮助我!

I have a file called equipamentsCURT3.ttl and contains the following: 我有一个名为equipamentsCURT3.ttl的文件,其中包含以下内容:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix v: <http://www.w3.org/2006/vcard/ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://opendata.gencat.cat/recursos/equipaments/30883> a v:VCard ;
v:adr [ a v:Work ;
        v:country-name "Spain" ;
        v:locality "Sabadell" ;
        v:postal-code "08202" ;
        v:region "Vallès Occidental" ;
        v:street-address " c.  Sant Josep" ] ;
v:category "2. Parvulari"@ca,
    "3. Educació primària"@ca,
    "4. Educació secundària obligatòria"@ca,
    "Educació. Formació"@ca,
    "Ensenyaments de règim general"@ca ;
v:fn "Escolàpies Sabadell"@ca ;
v:geo [ v:latitude 4.154826e+01 ;
        v:longitude 2.111243e+00 ] ;
v:nickname "Escolàpies Sabadell"@ca ;
v:tel [ a v:Pref,
            v:Tel,
            v:Work ;
        rdf:value "937255348" ] .

<http://opendata.gencat.cat/recursos/equipaments/31264> a v:VCard ;
v:adr [ a v:Work ;
        v:country-name "Spain" ;
        v:locality "Molins de Rei" ;
        v:postal-code "08750" ;
        v:region "Baix Llobregat" ;
        v:street-address " c.  Ntra. Sra. de Lourdes" ] ;
v:category "4. Educació secundària obligatòria"@ca,
    "7. Batxillerat"@ca,
    "8. Cicles formatius d'FP de grau mitjà (CFPM)"@ca,
    "9. Cicles formatius d'FP de grau superior (CFPS)"@ca,
    "Educació. Formació"@ca,
    "Ensenyaments de règim general"@ca ;
v:fn "Institut Bernat el Ferrer"@ca ;
v:geo [ v:latitude 4.14105e+01 ;
        v:longitude 2.02704e+00 ] ;
v:nickname "Institut Bernat el Ferrer"@ca ;
v:tel [ a v:Pref,
            v:Tel,
            v:Work ;
        rdf:value "936683762" ] .

<http://opendata.gencat.cat/recursos/equipaments/31265> a v:VCard ;
v:adr [ a v:Work ;
        v:country-name "Spain" ;
        v:locality "Castellar del Vallès" ;
        v:postal-code "08211" ;
        v:region "Vallès Occidental" ;
        v:street-address " NC  Bonavista" ] ;
v:category "2. Parvulari"@ca,
    "3. Educació primària"@ca,
    "Educació. Formació"@ca,
    "Ensenyaments de règim general"@ca ;
v:fn "Escola Bonavista"@ca ;
v:geo [ v:latitude 4.161903e+01 ;
        v:longitude 2.091745e+00 ] ;
v:nickname "Escola Bonavista"@ca ;
v:tel [ a v:Pref,
            v:Tel,
            v:Work ;
        rdf:value "937144195" ] .

I am using Python3.5 and a library called RDFLib ( https://github.com/RDFLib/rdflib ). 我正在使用Python3.5和一个名为RDFLib( https://github.com/RDFLib/rdflib )的库。 I need to read from a file called equipamentsCURT.rdf, serialize it into equipamentsCURT3.ttl and then retrieve all information related to an equipment. 我需要读取一个名为equipamentsCURT.rdf的文件,将其序列化为equipamentsCURT3.ttl,然后检索与设备有关的所有信息。 For example, for the equipment 30883 ( http://opendata.gencat.cat/recursos/equipaments/30883 ), I want v:adr,v:category,v:fn,v:geo and v:tel . 例如,对于设备30883http://opendata.gencat.cat/recursos/equipaments/30883 ),我想要v:adr,v:category,v:fn,v:geo和v:tel To obtain this data, I use SPARQL but I don't know why the query doesn't work. 要获取此数据,我使用SPARQL,但不知道为什么查询不起作用。 I'm very confused in how to query the information. 我对如何查询信息感到非常困惑。

Here is my code: 这是我的代码:

import rdflib , pprint
from rdflib import URIRef, Graph
from rdflib.plugins import sparql

g = Graph()
g.load("equipamentsCURT3.ttl", format='turtle')

queryTest = 'prefix v: <http://www.w3.org/2006/vcard/ns#> ' \
'select ?y where {?x  a <http://opendata.gencat.cat/recursos/equipaments 30883>; ?y v:VCard .}'
qresult = g.query(queryTest)

for st in qresult:
 print rdflib.term.Literal(st).value

The whole query doesn't make any sense nor does it match the data. 整个查询没有任何意义,也不匹配数据。 I'd suggest reading a SPARQL tutorial first. 我建议先阅读SPARQL教程。 The whole query looks like copy-paste from something else + some random stuff from your side. 整个查询看起来像是从其他位置复制粘贴,再从您的侧面随机插入一些东西。

  1. the URI http://opendata.gencat.cat/recursos/equipaments 30883 contains a white space which is wrong URI http://opendata.gencat.cat/recursos/equipaments 30883包含错误的空白

  2. http://opendata.gencat.cat/recursos/equipaments/30883 is not a class. http://opendata.gencat.cat/recursos/equipaments/30883 不是一个类。 Thus, a triple pattern ?xa <http://opendata.gencat.cat/recursos/equipaments/30883> , which means to all resources that belong to the class http://opendata.gencat.cat/recursos/equipaments/30883 doesn't match your data. 因此,三元模式?xa <http://opendata.gencat.cat/recursos/equipaments/30883> ,这意味着属于类http://opendata.gencat.cat/recursos/equipaments/30883所有资源与您的数据不匹配。

  3. The second triple pattern is ?x ?yv:VCard . 第二个三重模式是?x ?yv:VCard And you're selecting the predicate ?y as the final result of your query. 并且您选择谓词?y作为查询的最终结果。 But you want the objects for a given subject and a given set of predicates. 但是您想要给定主题和给定谓词的对象。 Syntax of a triple /resp. 三元组/ resp的语法。 triple pattern) is subject-predicate-object. 三元模式)是主语-谓语-宾语。 Thus, for example for v:category it should be 因此,例如对于v:category应该是

PREFIX v: <http://www.w3.org/2006/vcard/ns#> 
SELECT ?o WHERE {
  <http://opendata.gencat.cat/recursos/equipaments/30883>  v:category ?o 
}

For the other properties it will be more complicated since the values itself are blank nodes that have attached multiple values via additional properties. 对于其他属性,将更加复杂,因为值本身是通过附加属性附加了多个值的空白节点。 Eg for v:adr it would be 例如对于v:adr

PREFIX v: <http://www.w3.org/2006/vcard/ns#> 
SELECT ?p ?o WHERE {
  <http://opendata.gencat.cat/recursos/equipaments/30883>  v:adr ?adr .
  ?adr ?p ?o 
}

Update 更新资料

If you don't want the values but the properties it's correct to have the variable in predicate position. 如果您不希望值而是属性,则将变量置于谓词位置是正确的。 But it's wrong to restrict it to those properties that occur only in triples with the object v:VCard because there is no such property besides rdf:type ( a is just a synonym for it). 但是将其限制为仅与对象v:VCard一起出现在三元组中的那些属性是错误的,因为除了rdf:type之外没有这样的属性( a只是它的同义词)。 In that case it should be 在这种情况下,应该

PREFIX v: <http://www.w3.org/2006/vcard/ns#> 
SELECT DISTINCT ?p WHERE {
  <http://opendata.gencat.cat/recursos/equipaments/30883>  ?p ?o 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM