I'm sure I'm not approaching this question in the right way, but is there a way I can query for the same WHERE about multiple literal subjects?

For example, this query from the venerable site http://www.ldodds.com/blog/2005/06/sample-sparql-queries-updated/ will find all the Noble gasses from the periodic table and select their name, symbol, atomic weight, and atomic number:

PREFIX table: <http://www.daml.org/2003/01/periodictable/PeriodicTable#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?group_name ?name ?symbol ?weight ?number
WHERE { 
  ?element table:group ?group.
  ?group table:name "Noble gas"^^xsd:string.
  ?element table:name ?name.
  ?element table:symbol ?symbol.
  ?element table:atomicWeight ?weight.
  ?element table:atomicNumber ?number. 
}

What would I do If I wanted to get the same information about Carbon, Hydrogen, Oxygen, and Nitrogen?

One obvious solution involves a UNION, repeating the predicates about each literal.

Is there something more efficient and more elegant?

If there were an "is" parallel to the "a" abbreviation for rdf:type to allow literal resources, is there some way I could use union there? This doesn't quite work either.

 WHERE {  
    {
          { ?element is <#Carbon> } 
    UNION { ?element is <#Hydrogen> } 
    UNION { ?element is <#Oxygen> } 
    UNION { ?element is <#Nitrogen> }  
    } .
  ?element table:name ?name.
  ?element table:symbol ?symbol.
  ?element table:atomicWeight ?weight.
  ?element table:atomicNumber ?number. 
  }

I also thought about using the XQuery 1.1 BIND/AS but that works only for a single subject.

  BIND (?element as <#Carbon> }

asked 30 Apr '12, 19:50

klotz's gravatar image

klotz
414
accept rate: 0%

edited 01 May '12, 18:10

Another excellent resource along similar lines is @lee and Eric's "SPARQL by Example".

http://www.cambridgesemantics.com/semantic-university/sparql-by-example

And welcome to the site. :)

http://answers.semanticweb.com/questions/14494/welcome-to-answerssemanticwebcom

(30 Apr '12, 20:05) Signified ♦ Signified's gravatar image

Can you clarify what exactly you mean with "literal subjects" in this context? Because ?element in your example is bound to resource URIs, not literals.

(30 Apr '12, 20:08) Jeen Broekstra ♦ Jeen%20Broekstra's gravatar image

Yes, absolutely. I meant multiple resource URIs that are typed into the query and not the result of some other statement. I did not mean literal string or other value, but a typed-in resource URI.

(01 May '12, 13:41) klotz klotz's gravatar image

There's a few ways to do this, but the most elegant is the SPARQL 1.1 BINDINGS feature.

So you could do something like:

PREFIX table: <http://www.daml.org/2003/01/periodictable/PeriodicTable#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?group_name ?name ?symbol ?weight ?number
WHERE { 
  ?element table:group ?group.
  ?group table:name ?table.
  ?element table:name ?name.
  ?element table:symbol ?symbol.
  ?element table:atomicWeight ?weight.
  ?element table:atomicNumber ?number. 
}
BINDINGS ?name {
   ("Carbon")
   ("Hydrogen")
   ("Oxygen")
   ("Nitrogen")
}
link

answered 30 Apr '12, 20:02

Signified's gravatar image

Signified ♦
23.5k1623
accept rate: 37%

edited 30 Apr '12, 20:02

The spec example is unclear: the first has only one binding, and the second use UNDEF. Your example makes it quite clear! Unfortunately, I can't get it to work. The FILTER version below works, but the BINDINGS one just provides all the elements.

PREFIX table: <http: www.daml.org="" 2003="" 01="" periodictable="" periodictable#="">    
PREFIX xsd: <http: www.w3.org="" 2001="" xmlschema#="">
SELECT ?element ?name
WHERE { 
  ?element table:name ?name.
  ## FILTER (?element IN (table:C, table:H, table:O, table:N))
}
BINDINGS ?element {
   (table:C)
   (table:H)
   (table:O)
   (table:N)
}
(01 May '12, 13:43) klotz klotz's gravatar image
1

That is quite strange and looks like a bug in the endpoint you use perhaps. I can't give you a quick answer, so maybe you could ask a separate question here? Make sure to include (1) SPARQL engine used; (2) a relevant snippet of triples indexed; (3) queries tried and answers received.

(01 May '12, 15:11) Signified ♦ Signified's gravatar image

For posterity, since this answer, BINDINGS was changed to VALUES in the SPARQL 1.1 spec.

(07 Jan '13, 10:27) Signified ♦ Signified's gravatar image

There's a few things not quite clear in your question, but as an alternative to your UNION construct, you could use the IN operator:

FILTER(?element IN (<#Carbon>, <#Hydrogen>, <#Oxygen>, ...))
link

answered 30 Apr '12, 20:09

Jeen%20Broekstra's gravatar image

Jeen Broekstra ♦
11.4k412
accept rate: 38%

This works; thank you! I had the data format slightly wrong and have to use table:C, table:H etc.

(01 May '12, 14:16) klotz klotz's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×1,243
×68

Asked: 30 Apr '12, 19:50

Seen: 1,834 times

Last updated: 07 Jan '13, 10:27