You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Robert Campbell <rr...@gmail.com> on 2011/01/21 12:10:28 UTC

How to specify an optional predicate/object?

I have this query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
       PREFIX c:   <http://s.opencalais.com/1/pred/>
       SELECT ?company ?name ?ticker ?symbol WHERE {
         ?company  rdf:type  <http://s.opencalais.com/1/type/er/Company> .
         ?company  c:name    ?name    .
         ?company  c:ticker  ?ticker  .
         ?company  c:symbol  ?symbol
       }

It only returns /Company types that contain a c:name, c:ticker, and
c:symbol. I'd like only c:name to be required, with either "" or null
being returned for a c:ticker or c:symbol is not present. To be clear,
in the actual RDF, the entire <c:ticker></c:ticker> or
<c:symbol></c:symbol> elements can be missing OR they can just be
empty.

Is this possible to express only in the query? Or do I have to perform
some sort of pre/post processing on the RDF itself? I'm iterating
through the ResultSet, calling QuerySolution.getLiteral("ticker"),
etc.

Rob

Re: How to specify an optional predicate/object?

Posted by Robert Campbell <rr...@gmail.com>.
Thanks Andy, that's exactly what I was looking for.


On Fri, Jan 21, 2011 at 12:16 PM, Andy Seaborne
<an...@epimorphics.com> wrote:
>
>
> On 21/01/11 11:10, Robert Campbell wrote:
>>
>> I have this query:
>>
>> PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>>        PREFIX c:<http://s.opencalais.com/1/pred/>
>>        SELECT ?company ?name ?ticker ?symbol WHERE {
>>          ?company  rdf:type<http://s.opencalais.com/1/type/er/Company>  .
>>          ?company  c:name    ?name    .
>>          ?company  c:ticker  ?ticker  .
>>          ?company  c:symbol  ?symbol
>>        }
>>
>> It only returns /Company types that contain a c:name, c:ticker, and
>> c:symbol. I'd like only c:name to be required, with either "" or null
>> being returned for a c:ticker or c:symbol is not present. To be clear,
>> in the actual RDF, the entire<c:ticker></c:ticker>  or
>> <c:symbol></c:symbol>  elements can be missing OR they can just be
>> empty.
>>
>> Is this possible to express only in the query? Or do I have to perform
>> some sort of pre/post processing on the RDF itself? I'm iterating
>> through the ResultSet, calling QuerySolution.getLiteral("ticker"),
>> etc.
>>
>> Rob
>
> Use OPTIONAL:
> http://www.w3.org/TR/rdf-sparql-query/#optionals
>
>
> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>       PREFIX c:   <http://s.opencalais.com/1/pred/>
>       SELECT ?company ?name ?ticker ?symbol WHERE {
>         ?company  rdf:type  <http://s.opencalais.com/1/type/er/Company> .
>         ?company  c:name    ?name    .
>         OPTIONAL { ?company  c:ticker  ?ticker }
>         OPTIONAL { ?company  c:symbol  ?symbol }
>       }
>
> Missing values will cause ?ticker and ?symbol to unbound in a result set row
> for ?company.
>
>        Andy
>
>

Re: How to specify an optional predicate/object?

Posted by Andy Seaborne <an...@epimorphics.com>.

On 21/01/11 11:10, Robert Campbell wrote:
> I have this query:
>
> PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>         PREFIX c:<http://s.opencalais.com/1/pred/>
>         SELECT ?company ?name ?ticker ?symbol WHERE {
>           ?company  rdf:type<http://s.opencalais.com/1/type/er/Company>  .
>           ?company  c:name    ?name    .
>           ?company  c:ticker  ?ticker  .
>           ?company  c:symbol  ?symbol
>         }
>
> It only returns /Company types that contain a c:name, c:ticker, and
> c:symbol. I'd like only c:name to be required, with either "" or null
> being returned for a c:ticker or c:symbol is not present. To be clear,
> in the actual RDF, the entire<c:ticker></c:ticker>  or
> <c:symbol></c:symbol>  elements can be missing OR they can just be
> empty.
>
> Is this possible to express only in the query? Or do I have to perform
> some sort of pre/post processing on the RDF itself? I'm iterating
> through the ResultSet, calling QuerySolution.getLiteral("ticker"),
> etc.
>
> Rob

Use OPTIONAL:
http://www.w3.org/TR/rdf-sparql-query/#optionals


PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX c:   <http://s.opencalais.com/1/pred/>
        SELECT ?company ?name ?ticker ?symbol WHERE {
          ?company  rdf:type  <http://s.opencalais.com/1/type/er/Company> .
          ?company  c:name    ?name    .
          OPTIONAL { ?company  c:ticker  ?ticker }
          OPTIONAL { ?company  c:symbol  ?symbol }
        }

Missing values will cause ?ticker and ?symbol to unbound in a result set 
row for ?company.

	Andy