[redland-dev] How does OPTIONAL work?

Marinaio di terra marinaioditerra at gmail.com
Sun Dec 18 15:21:18 GMT 2005


Hi all,
I'm working on a project based on Semantic Web technologies.
I'm using Python and the Redland-Python-binding, but I'm having some
troubles querying using the OPTIONAL parameter.

If I have a datafile like this:

<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#">]>
<rdf:RDF   
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"  
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:toy="http://www.example.org/toy-schema.rdf#"
  xml:base="http://www.example.org/sample.rdf">

<toy:Toy rdf:ID="Ball">
  <toy:name>BallName</toy:name>
</toy:Toy>

<toy:Toy rdf:ID="BigBall">
  <toy:name>Big Ball Name</toy:name>
  <toy:version>2.0</toy:version>
</toy:Toy>

</rdf:RDF>

and I query for the toys name and optionally their version with this:

RDF.Query("PREFIX toy: <http://www.example.org/toy-schema.rdf#> " +
          "SELECT ?name ?ver " +
          "WHERE { ?x toy:name ?name . " +
          "OPTIONAL {?x toy:version ?ver} }",
          query_language='sparql')

I obtain the expected results:
<results>
    <result>
      <binding name="name"><literal>Big Ball</literal></binding>
      <binding name="ver"><literal>2.0</literal></binding>
    </result>
    <result>
      <binding name="name"><literal>BallName</literal></binding>
      <binding name="ver"><unbound/></binding>
    </result>
</results>


But if the query is like this (i.e. I specify the subject of the triple):

RDF.Query("PREFIX toy: <http://www.example.org/toy-schema.rdf#> " +
          "SELECT ?name ?ver " +
          "WHERE { <http://www.example.org/sample.rdf#Ball> toy:name ?name . " +
          "OPTIONAL {<http://www.example.org/sample.rdf#Ball> toy:version ?ver} }",
           query_language='sparql')

I don't obtain anything:
<results>
</results>

Is it a correct behavior (and why) or a bug in the library?

Thanks a lot!

Christian.



More information about the redland-dev mailing list