Filter n3 / turtle file with rdflib and sparql queries

I am trying to filter a turtle file using pyrdf with sparql. But I noticed that sparql queries lose information in the context of elements. I would then like to retype the query result in the form of a turtle file, can this be done without manually scanning all the subfields of the element? We have data on places formatted as follows:

:pt0001
     vcard:category "Poste e Telegrafi"
    ; vcard:fn "Ufficio Bologna 1"
    ; vcard:extended-address "Via Cairoli 9, Bologna BO, Italy"
    ; vcard:latitude "44.504192"
    ; vcard:longitude "11.338661"
    ; vcard:tel "051 243425"
    ; vcard:fax "051 244459"
    ; cs:opening "Mon, Tue, Wed, Thu, Fri: 0800-1330. Sat: 0800-1230."
    ; cs:closing "01-01, 01-06, P, LA, 04-25, 05-01, 06-02, 08-15, 11-01, 12-08, 12-25, 12-26: .".

For example, we only need places that have a name (fn). Thanks for any advice you find ...

+3
source share
1 answer

To return all locations with a name, you can do something simple:

SELECT DISTINCT ?location
WHERE { 
   ?location vcard:fn [].
}

(:pt0001 ), , , .

( , , , )

:

SELECT ?location ?prop ?value
WHERE { 
   ?location vcard:fn [];
             ?prop ?value .
} ORDER BY ?location

:

?location    ?prop           ?value
:pt0001      vcard:category  "Poste e Telegrafi"
:pt0001      vcard:name      "Ufficio Bologna 1"
:pt0001      vcard:tel       "051 243425"
(etc...)   

, , :

SELECT ?location ?name ?cat ?tel
WHERE { 
   ?location vcard:fn ?name ;
             vcard:category ?cat ;
             vcard:tel ?tel .
} ORDER BY ?location

:

?location ?name               ?cat                ?tel
:pt0001   "Ufficio Bologna 1" "Poste e Telegrafi" "051 243425"

.

SPARQL - . --, SPARQL .

: , . CONSTRUCT , . SELECT ( ) , CONSTRUCT RDF:

 CONSTRUCT { ?subject ?predicate ?object }
 WHERE { 
   ?subject ?predicate ?object ;
            vcard:fn [] 
 }
+4

All Articles