Skip to content

Instantly share code, notes, and snippets.

@JeniT
Created April 8, 2011 19:32
Show Gist options
  • Save JeniT/910558 to your computer and use it in GitHub Desktop.
Save JeniT/910558 to your computer and use it in GitHub Desktop.
These SPARQL queries can be run on http://services.data.gov.uk/reference/sparql. The first gets a list of public bodies in the triplestore (which includes all departments but not all public bodies). The second gets a list of units and the public body that
PREFIX gov: <http://reference.data.gov.uk/def/central-government/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?body ?label
WHERE {
{ ?body a gov:PublicBody . }
UNION
{ ?body a gov:Department . }
?body rdfs:label ?label .
}
PREFIX gov: <http://reference.data.gov.uk/def/central-government/>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?unit ?label ?body
WHERE {
?unit org:unitOf ?body .
?unit rdfs:label ?label .
}
@robmckinnon
Copy link

How do I get the name, phone, email for a person holding a post with a unit?

This query works to get posts by unit:

PREFIX gov: 
PREFIX org: 
PREFIX rdfs: 

SELECT DISTINCT ?heldBy ?hasPostComment ?hasPost ?unit ?unitLabel ?unitOf
WHERE {
  ?unit gov:hasPost ?hasPost .
  ?unit org:unitOf ?unitOf .
  ?unit rdfs:label ?unitLabel .

  ?hasPost rdfs:comment ?hasPostComment .
  ?hasPost gov:heldBy ?heldBy .
}

LIMIT 4

I tried this to get phone numbers and it didn't work:

PREFIX gov: 
PREFIX org: 
PREFIX rdfs: 

SELECT DISTINCT ?phone ?heldBy ?hasPostComment ?hasPost ?unit ?unitLabel ?unitOf
WHERE {
  ?unit gov:hasPost ?hasPost .
  ?unit org:unitOf ?unitOf .
  ?unit rdfs:label ?unitLabel .

  ?hasPost rdfs:comment ?hasPostComment .
  ?hasPost gov:heldBy ?heldBy .
  ?heldBy foaf:phone ?phone .
}

LIMIT 4

@robmckinnon
Copy link

Figured it out, I needed to add a foaf PREFIX to top of query:

PREFIX gov: <http://reference.data.gov.uk/def/central-government/>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT *
WHERE {
  ?unit gov:hasPost ?hasPost .
  ?unit org:unitOf ?unitOf .
  ?unit rdfs:label ?unitLabel .

  ?hasPost rdfs:comment ?hasPostComment .
  ?hasPost gov:heldBy ?heldBy .

  ?heldBy foaf:phone ?phone .
  ?heldBy foaf:name ?name .
  ?heldBy foaf:mbox ?mbox .
}

LIMIT 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment