|
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> } |
|
There's a few ways to do this, but the most elegant is the SPARQL 1.1 So you could do something like:
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)
}
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. For posterity, since this answer, |


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
Can you clarify what exactly you mean with "literal subjects" in this context? Because
?elementin your example is bound to resource URIs, not literals.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.