How can one extract rdf:about or rdf:ID properties from triples using SPARQL?

China☆狼群 提交于 2020-02-02 11:21:08

问题


It seemed a trivial matter at the beginning but so far I have not managed to get the unique identifier for a given resource using SPARQL. What I mean is given, e.g., rdf:Description rdf:about="http://..." and then some properties identifying this resource, what I want to do is to first find this very resource and then retrieve all the triples given some URI.

I have tried naïve approaches by writing statements in a WHERE clause such as:

?x rdf:about ?y and ?x rdfs:about ?y

I hope I am being precise.


回答1:


You're making a classic mistake: confusing RDF (which is what SPARQL queries) with (one of) its serialisation, namely RDF/XML. rdf:about (and rdf:ID, rdf:Description, rdf:resource) are part of RDF/XML, a way RDF is written down. You can play around with the RDF Validator to see what RDF triples result from a piece of RDF/XML.

In your case let's start with:

<?xml version="1.0"?>
<rdf:RDF 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:dc="http://purl.org/dc/terms/">
  <rdf:Description rdf:about="http://www.example.org/">
    <dc:title>Example for Donal Fellows</dc:title> 
  </rdf:Description>
</rdf:RDF>

Plug that into the validator and you get:

Number      Subject                      Predicate                        Object
1           http://www.example.org/      http://purl.org/dc/terms/title   "Example for Donal Fellows"

(you can also ask for a pictorial representation)

Notice that rdf:about is not present: its value provides the subject for the triple.

How do I do a query to find properties associated with http://www.example.org? Like this:

select * {
    <http://www.example.org/> ?predicate ?object
}

You'll get:

?predicate                        ?object
<http://purl.org/dc/terms/title>  "Example for Donal Fellows"

You'll notice that the query is a triple match with variables (?v) in places where we want to find values. We could also ask what predicate links http://www.example.org/ with "Example for..." by asking:

select * {
    <http://www.example.org/> ?predicate "Example for Donal Fellows"
}

This pattern matching is the heart of SPARQL.

RDF/XML is a tricky beast, and you might find it easier to work with N-Triples, which is very verbose but clear, or turtle, which is like N-Triples with a large number of shorthands and abbreviations. Turtle is often preferred by the rdf community.

P.S. rdfs:about doesn't exist anywhere.



来源:https://stackoverflow.com/questions/2760896/how-can-one-extract-rdfabout-or-rdfid-properties-from-triples-using-sparql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!