Search code examples
rdfsemantic-webrdf4j

IS there a simple SPARQL Construct query to select all subject related statements by certain predicate value


I am a complete Semantic Web beginner and RDF4J beginner. Currently i have some RDF xml and i can not write a simple construct query to select all related statements by subject value. I have this:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<rdf:Description rdf:about="http://example.org/cocktail#Mimosa">
    <rdf:type rdf:resource="http://www.w3.org/2004/02/skos/core#Concept"/>
    <prefLabel xmlns="http://www.w3.org/2004/02/skos/core#">Mimosa</prefLabel>
    <altLabel xmlns="http://www.w3.org/2004/02/skos/core#">bla</altLabel>
    <altLabel xmlns="http://www.w3.org/2004/02/skos/core#">huuh</altLabel>
    <altLabel xmlns="http://www.w3.org/2004/02/skos/core#">owiii</altLabel>
    <broader xmlns="http://www.w3.org/2004/02/skos/core#">Wine cocktail</broader>
</rdf:Description>

<rdf:Description rdf:about="http://example.org/cocktail#White Russian">
    <rdf:type rdf:resource="http://www.w3.org/2004/02/skos/core#Concept"/>
    <prefLabel xmlns="http://www.w3.org/2004/02/skos/core#">White Russian</prefLabel>
    <altLabel xmlns="http://www.w3.org/2004/02/skos/core#">Ruski</altLabel>
    <altLabel xmlns="http://www.w3.org/2004/02/skos/core#">kasdnjkldfan</altLabel>
    <altLabel xmlns="http://www.w3.org/2004/02/skos/core#">oasdasi</altLabel>
    <broader xmlns="http://www.w3.org/2004/02/skos/core#">Wine cocktail</broader>
</rdf:Description>

</rdf:RDF>

I want to write a simple query which takes prefLabel as an argument and selects a whole block of statements (everything in the description including the description it self). For example i have a value "Mimosa" for prefLabel and now i wish to get this:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<rdf:Description rdf:about="http://example.org/cocktail#Mimosa">
    <rdf:type rdf:resource="http://www.w3.org/2004/02/skos/core#Concept"/>
    <prefLabel xmlns="http://www.w3.org/2004/02/skos/core#">Mimosa</prefLabel>
    <altLabel xmlns="http://www.w3.org/2004/02/skos/core#">bla</altLabel>
    <altLabel xmlns="http://www.w3.org/2004/02/skos/core#">huuh</altLabel>
    <altLabel xmlns="http://www.w3.org/2004/02/skos/core#">owiii</altLabel>
    <broader xmlns="http://www.w3.org/2004/02/skos/core#">Wine cocktail</broader>
</rdf:Description>

</rdf:RDF>


Solution

  • You can do this as follows:

       CONSTRUCT 
       WHERE { 
             ?c a skos:Concept ;
                skos:prefLabel "Mimosa" ;
                ?property ?value .
       }
    

    Explanation: the first line in the WHERE clause selects all resources of type skos:Concept. The second line further narrows it down to only those concepts that have a prefLabel with the value "Mimosa". The last line then grabs all possible properties and values for the selected concepts.

    A tip: it helps to not focus on the RDF/XML syntax. Think of RDF in terms of a graph, rather than an XML document. It may help you to work with RDF files in a different syntax, like Turtle (which more closely matches how things work in SPARQL, too).