3

Probably I just can't grasp some basic idea behind ontologies but here's my question. I'm trying to extract triples from an RDF storage (using 4store, but also tried XML ArmyKnife) with a SPARQL query specifying the predicate and get empty results.

To be sure I don't mess anything up with RDF syntax I use LUBM generated data (stripped down to an example-suitable size).

<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF
  xml:base = "http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl"
  xmlns:rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
  xmlns:owl="http://www.w3.org/2002/07/owl#"
  xmlns:ub="univ-bench.owl#"
>

<owl:DatatypeProperty rdf:ID="name">
  <rdfs:label>name</rdfs:label>
</owl:DatatypeProperty>

<owl:Class rdf:ID="Organization">
  <rdfs:label>organization</rdfs:label>
</owl:Class>

<owl:Class rdf:ID="University">
  <rdfs:label>university</rdfs:label>
  <rdfs:subClassOf rdf:resource="#Organization" />
</owl:Class>

<ub:University rdf:about="http://www.University0.edu">
   <ub:name>University0</ub:name>
</ub:University>

</rdf:RDF>

Then I run a query to see what triples my database actually contains after import:

SELECT * WHERE {?s ?p ?o} ORDERBY ?s

Here's the result:

<http://www.University0.edu>    <univ-bench.owl#name>   "University0"
<http://www.University0.edu>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>       <univ-bench.owl#University>
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#Organization>         <http://www.w3.org/2000/01/rdf-schema#label>    "organization"
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#Organization>     <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>       <http://www.w3.org/2002/07/owl#Class>
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#University>       <http://www.w3.org/2000/01/rdf-schema#subClassOf>       <http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#Organization>
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#University>       <http://www.w3.org/2000/01/rdf-schema#label>    "university"
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#University>       <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>       <http://www.w3.org/2002/07/owl#Class>
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#name>     <http://www.w3.org/2000/01/rdf-schema#label>    "name"
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#name>     <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>       <http://www.w3.org/2002/07/owl#DatatypeProperty>

It's clearly visible that I have <univ-bench.owl#name> predicate as a part of the first triple.

Nevertheless following query returns no results:

SELECT * WHERE {?s <univ-bench.owl#name> ?o}

I've tried dozens of combinations, with namespaces and without, but can't make it work. Can anyone explain why RDF engine doesn't find a predicate that's obviously there?

BTW, extracting the same triple with OBJECT="University0" works OK.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353

2 Answers2

6

This is invalid:

xmlns:ub="univ-bench.owl#"

Namespace URIs must be absolute in RDF/XML. (They can be relative in other syntaxes like Turtle, but not in RDF/XML.) If your example is indeed generated straight by LUBM, then LUBM is broken. This should work:

xmlns:ub="http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#"

Then of course you need to match that URI in the SPARQL query like @RobV said.

cygri
  • 9,412
  • 1
  • 25
  • 47
  • Thanks, guys. Both you and RobV (in his last comment) were right. Namespace URI should have been defined as absolute from the very beginning. Just adding `http://` before it solves the problem. And yes, this is what LUBM generator outputs. – Anton Zherzdev Aug 22 '11 at 14:06
  • 2
    **Update**: Actually there's a `-onto` command-line switch for LUBM generator which you pass ontology URI to. My mistake was I passed just `univ-bench.owl` file name to it instead of a fully specified URI. – Anton Zherzdev Aug 23 '11 at 11:31
5

I suspect your problem is that you are not trying to match the correct URI in your query:

SELECT * WHERE {?s <univ-bench.owl#name> ?o}

Anything enclosed in angle brackets is treated as a URI and since you've used a relative URI the SPARQL processor is presumeably resolving it against some arbitrary base resulting in an absolute URI different from the one in your data thus your query won't match anything.

What you need to do is either specify the full URI like so:

SELECT * WHERE {?s <http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#name> ?o}

Or use a prefix like so:

PREFIX ub: <univ-bench.owl#>
SELECT * WHERE { ?s ub:name ?o }

If none of these are working then I suspect it is some issue with relative URIs. My advice would be never use relative URIs, always use absolute URIs wherever possible. If you must use relative URIs always specify a Base URI explicitly i.e. ensure your data used absolute URIs, reload it into your store and try again.

You can do this with xml:base="http://example.org" in RDF/XML or with BASE <http://example.org/> in SPARQL. If using 4store I believe their importer command line has arguments that can be used to set the default base URI for when none is specified.

The other thing you might want to do is to try putting your data into a different triple store and see if you experience the same behaviour, if you do then this would imply an issue with relative vs absolute URIs as I have suggested. If the other store answers the query fine that would suggest a possible bug with 4store in which case you should contact them on their mailing list - http://groups.google.com/group/4store-support

RobV
  • 28,022
  • 11
  • 77
  • 119
  • That was exactly my conclusion also. So, believe me, I've tried many namespace/absolute naming systems (including those you suggested). Well, maybe just haven't hit the right one :) You may be right that I have "some issue with relative URIs", but what kind of issue is that? What am I getting wrong? Also, I've tried other RDF endgine, it's the XML ArmyKnife here (http://xmlarmyknife.com/api/rdf/sparql/query). – Anton Zherzdev Aug 22 '11 at 10:24
  • Oh, I think I might recognise this. Check the encoding of '~' in the data? I know that's caught me out with LUBM. – user205512 Aug 22 '11 at 10:39
  • I see your point. After I've read your post I realized there's some mangling with encoding going on (e.g. `UTF-8` is specified in the header). But nevertheless that is not the problem I think. At least not with '~' (I've tested it by simply removing the symbol). – Anton Zherzdev Aug 22 '11 at 11:02
  • 1
    @Anton You've used relative URIs so the RDF store and/or SPARQL engine must have at some point turned them into absolute URIs. In general using relative URIs with no Base URI is an error in most RDF syntaxes - trying your sample data on http://www.w3.org/RDF/Validator/ brings back several error messages about just this. If 4store is accepting this data then it must be turning in into absolute URIs somehow, without knowing how it is doing this (i.e. what base URI it is using) it is impossible to tell you what URI you actually need to query for – RobV Aug 22 '11 at 13:16
  • Thanks, RobV. Your last suggestion is absolutely correct. cygri has posted it as an "answer" so I marked that one =) – Anton Zherzdev Aug 22 '11 at 14:08