简体   繁体   中英

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

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, eg, 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.

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.

PS rdfs:about doesn't exist anywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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