3

I am trying to filter a turtle file using pyrdf with sparql. But I noticed that sparql queries lose information over the context of the elements. I would like then to re-print the result of the query as a turtle file, is it possible to do this without manually scanning all the subfields of the element? We have data about locations formatted like this:

: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 want only locations that have a name (fn). Thanks for any tip you find...

ashmikuz
  • 83
  • 5
  • Can you elaborate on what you mean with "sparql queries lose information over the context of the elements"? – Jeen Broekstra Apr 18 '12 at 18:46
  • I'm no expert when it comes to turtle/rdf, but what i have succeded is reading the single values from the query (es: telephone, id, name, etc). What if I want to keep the entire location "element" like the one i posted? Is it possible? I may just reformat single fields, I just wanted to know if there is an easier way... – ashmikuz Apr 18 '12 at 23:05
  • yes, that's possible, I've edited my answer to show some alternative ways to do this with SPARQL. – Jeen Broekstra Apr 19 '12 at 01:00

1 Answers1

4

To get back all locations which have a name, you could do something simple like:

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

This will give you back back the identifier (:pt0001 in your example), but of course the query can be adapted to return all the property values as well.

(Edit I added additional examples based on your clarification, I think this is what you're after)

Like so:

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

The result of this query will be a table of results of the form:

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

Or, as yet another alternative, you can explicitly formulate your query to get specific property values for each location:

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

which will give you back a result table of the form:

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

Take your pick.

The trick with SPARQL queries is to think in terms of triples. Your data contains subject-predicate-object triples, SPARQL queries formulate patterns over those triples.

As a further remark: I see you are thinking about re-printing the result of the query as turtle. In that case, a CONSTRUCT query might be what you're after. While the result of a SELECT query (as shown above) is a table structure, the result of a CONSTRUCT query is a collection of RDF triples:

 CONSTRUCT { ?subject ?predicate ?object }
 WHERE { 
   ?subject ?predicate ?object ;
            vcard:fn [] 
 }
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • I have tried your last proposal, and that worked, thanks for the tip... What I have noticed is that all the vcard info is "lost" when i add the triples to a result Graph. For example, the location above will have ns1:category instead of vcard:category. We wanted also to filter, for example, locations that have a certain name or latitude or whatever, but with this new query I cannot seem to understand how to use the FILTER regex() operator. Thanks again... – ashmikuz Apr 19 '12 at 10:08
  • The result graph still contains the correct vcard property URIs, but the namespace definition (which maps the prefix 'vcard' to the actual base URI) is not exported to your graph. It depends on the tool you're using, but there should be something in there to set a namespace definition. It's something you'll have to do outside of the SPARQL query though. – Jeen Broekstra Apr 19 '12 at 18:06
  • As for your other question: it can easily done of course but we'll need a bit more detail about what you're trying to do there. Perhaps create a new question? – Jeen Broekstra Apr 19 '12 at 18:07
  • Ok, thaks for the help over my first problem, construct did the job. I opened a new question today with this new problem: [link](http://stackoverflow.com/questions/10244600/search-by-name-in-rdf-n3-file) – ashmikuz Apr 20 '12 at 10:42