简体   繁体   中英

Select literal in SPARQL?

I want to construct a SPARQL query populated with values I'm setting as literals.

eg

SELECT 'A', 'B', attribute
FROM 
    TABLE

Would return a table that might look like this:

  A    |    B    |    attribute
-------|---------|--------------
  A    |    B    |    Mary
  A    |    B    |    has
  A    |    B    |    a
  A    |    B    |    little
  A    |    B    |    lamb

What I want to do is run a query like this to get all the object types in a triplestore:

select distinct ?o ("class" as ?item_type) 
where {
    ?s rdf:type ?o.
} 

and then (ideally) UNION it with a second query that pulls out all the distinct predicate values:

select distinct ?p ("predicate" as ?item_type) 
where {
    ?s ?p ?o.
} 

the results of which might look like:

  item           |    item_type    
-----------------|-----------------
 a_thing         |    class
another_thing    |    class
a_relation       |    predicate
another_relation |    predicate

But a UNION in SPARQL only links in an additional where clause, which means I can't specify the item_type literal I want to inject into my results-set.

I think the following should get you what you want:

SELECT DISTINCT ?item ?item_type
WHERE {
   { ?s a ?item .
     BIND("class" AS ?item_type)
   }
   UNION
   { ?s ?item ?o
     BIND("predicate" AS ?item_type)
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM