IT-Wissen/sparql
2024-10-03 19:45:41 +02:00
..
README.md rdf 2024-10-03 19:45:41 +02:00

Table of Contents

Sparql Queries

Select

Übersichts-Queries

Finde alle Classes

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?type
WHERE {
  ?s a ?type.
}

Finde alle Classes mit bestimmten Prefix

PREFIX bc: <http://base22.com/ont/bc#>
SELECT DISTINCT ?type
WHERE {
  ?subject a ?type.
  FILTER( STRSTARTS(STR(?type),str(bc:)) )
}

Select all Triples

select ?s ?p ?o
where {?s ?p ?o}

Select Triples with rdf:type as ?p

select ?s ?p ?o
where {
  ?s rdf:type ?o .
}

Select Triples with rdf:type and multiple ?o

select ?s ?p ?o
where {
  values ?o { <http://fedora.info/definitions/v4/repository#ArchivalGroup> <http://www.loc.gov/premis/rdf/v3/File> }
  ?s rdf:type ?o
}

Count

prefix rico: <https://www.ica.org/standards/RiC/ontology#>
SELECT (count (?c) as ?cCount)
WHERE {
  ?c a <http://www.loc.gov/premis/rdf/v3/File>
}

Suche nach String

Select resource with needle in 2nd level of haystack

prefix rico: <https://www.ica.org/standards/RiC/ontology#>
SELECT ?s
WHERE {
  ?s ^rico:isOrWasIdentifierOf ?o .
  FILTER EXISTS { ?o rico:name "CH-000249-X:666377" }
}

Date Queries

Events zwischen Start- und Enddatum

# Events zwischen Start- und Enddatum
# ***********************************
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
prefix dcterms: <http://purl.org/dc/terms/> 
prefix skos: <http://www.w3.org/2004/02/skos/core#> 
prefix schema: <http://schema.org/> 
prefix roru: <http://ld.rutishauser.info/rogerevents/> 
SELECT ?s ?event_name ?datestart ?startmonth ?dateend ?dateend2
WHERE {
  ?s schema:name ?event_name .
  ?s schema:startDate ?datestart .
  ?s schema:endDate ?dateend .
  BIND(xsd:date(?dateend) AS ?dateend2) .
  BIND(MONTH(?datestart) AS ?startmonth) .
  #BIND(STRLEN(?dateend) AS ?len_dateend) .
  #FILTER (?len_dateend > 0) .
  
  # achtung, nur xsd:dateTime lassen sich vergleichen, xsd:date nicht
  FILTER (xsd:dateTime(?datestart) < "2022-02-14T00:00:00"^^xsd:dateTime)
  FILTER (xsd:dateTime(?dateend) > "2022-02-14T00:00:00"^^xsd:dateTime)
}
order by desc(?dateend)

Events genaues Datum

# Events genaues Datum
# ********************
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
prefix dcterms: <http://purl.org/dc/terms/> 
prefix skos: <http://www.w3.org/2004/02/skos/core#> 
prefix schema: <http://schema.org/> 
prefix roru: <http://ld.rutishauser.info/rogerevents/> 
SELECT ?s ?event_name ?datestart ?dateend ?datestartyear ?dateendyear
WHERE {
  ?s schema:name ?event_name .
  OPTIONAL { ?s schema:startDate ?datestart .}
  OPTIONAL { ?s schema:endDate ?dateend .}
  
  BIND ( STR(YEAR(?datestart)) AS ?datestartyear ) .
  BIND ( STR(MONTH(?datestart)) AS ?datestartmonth ) .
  BIND ( STR(DAY(?datestart)) AS ?datestartday ) .
  
  BIND ( STR(YEAR(?dateend)) AS ?dateendyear ) .
  BIND ( STR(MONTH(?dateend)) AS ?dateendmonth ) .
  BIND ( STR(DAY(?dateend)) AS ?dateendday ) .
  
  FILTER (?datestartyear = "2022" || ?dateendyear = "2022") .
  FILTER (?datestartmonth = "3" || ?dateendmonth = "3") .
  FILTER (?datestartday = "15" || ?dateendday = "15") .
}
order by desc(?dateend)

RANDOM select example 1

# select all unitdate values of any resource

prefix rico: <https://www.ica.org/standards/RiC/ontology#>

SELECT DISTINCT ?resource ?s ?date 
WHERE {
  {
    ?s rico:normalizedDateValue ?date .
  } 
  UNION 
  {
    ?s rico:expressedDate ?date .
  }
  ?s rico:hasOrHadCategory <http://docuteam.ch/vocab/types/unitdate> .
  <http://docuteam.ch/vocab/types/unitdate> ^rico:hasOrHadCategory ?relation .
  ?relation rico:relationHasSource ?resource .
  
}
ORDER BY ASC (?resource)

Update

Delete Triples where ?s ?p ?o contains string

DELETE
{ ?s ?p ?o }
WHERE
{ 
  ?s ?p ?o .
  FILTER(regex(str(?s), "needle"))
}