How to make a class label shown as the column name

2019-05-15 13:16发布

问题:

The SPARQL

SELECT ?c0 ?l0
WHERE 
{ 
  ?c0 rdf:type <XXXX>.
  OPTIONAL
  {
    ?c0 rdfs:label ?l0.
  }
}

shows two columns c0 and l0. Any way to show only one column c0,but with the content of the ?l0 as the title of the ?c0 ? The result would look like select ?co as ...

回答1:

If I understand you correctly, you can do this with coalesce, which returns its first non-error bound argument. First, here's some data where there's a class :dog with the label "Dog", and a class :cat which has no label:

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://example.org/> .

:dog a rdfs:Class ;
     rdfs:label "Dog" .
:cat a rdfs:Class .

Here are the query and results:

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select (coalesce(?label,?class) as ?co) where {
  ?class a rdfs:Class
  optional {
    ?class rdfs:label ?label
  }
}
----------------------------
| co                       |
============================
| <http://example.org/cat> |
| "Dog"                    |
----------------------------


标签: rdf sparql