SPARQL select optional with language

12,118

Solution 1

I don't see why you need OPTIONAL here at all. Jan's query is failing because there is no shared variable between the outer pattern and the optional so you are trying to calculate the cross product of every label for test:thing with every non/french labelled test:thing which may be huge and why the query processor is failing.

You simply want something like the following unless I've misunderstood your question

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label
WHERE 
{
   test:thing rdfs:label ?label 
   FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
}

If you want the two labels separately then you could do something like:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label ?preferredLabel
WHERE 
{
  {
   test:thing rdfs:label ?label . FILTER(LANG(?label) = "")
  }
  UNION
  {
   test:thing rdfs:label ?preferredLabel . FILTER(LANGMATCHES(LANG(?label), "fr"))
  }
}

Solution 2

The easiest way to check the language of literals is to use the lang() function. Using this, your query can be written as:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX test: <http://test#> 
SELECT ?label ?preferredLabel 
WHERE { 
   test:thing rdfs:label ?label 
   OPTIONAL { 
     test:thing rdfs:label ?preferredLabel . 
     FILTER (lang(?preferredLabel) = "" || lang(?preferredLabel) = "fr") 
   } 
}

Solution 3

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?uri ?label ?preferredLabel
WHERE 
{
  {
   ?uri rdfs:label ?label . FILTER(LANG(?label) = "" && regex(str(?label), '(^|\\\\W)fr', 'i'))
  }
  UNION
  {
   ?uri rdfs:label ?preferredLabel . FILTER(LANG(?preferredLabel) = "fr" && regex(str(?preferredLabel), '(^|\\\\W)fr', 'i'))
  }
}
Share:
12,118
Devin McQueeney
Author by

Devin McQueeney

Web Application Architect &amp; Full Stack Developer

Updated on June 12, 2022

Comments

  • Devin McQueeney
    Devin McQueeney about 2 years

    I have some triples that look like this:

    test:thing rdfs:label "Non-Language Label"
    test:thing rdfs:label "English Label"@en
    test:thing rdfs:label "French Label"@fr
    

    I'd like to form a sparql query that gives me the "Non-Language Label" AND the "French Label", if any exists.

    I tried this and it's not working:

    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
    SELECT ?label ?preferredLabel
    WHERE {
       test:thing rdfs:label ?label 
       OPTIONAL {
         test:thing rdfs:label ?preferredLabel . 
         FILTER (regex(str(?preferredLabel), '(^|\\\\W)fr', 'i'))
       }
    }
    

    Thanks in advance!

  • Devin McQueeney
    Devin McQueeney over 12 years
    Hi - thanks for the help. You are right in adding in the filtering logic somewhere, which looks to be good. This query just errors out though for me. I think the problem begins with me basically selecting the same data twice (as ?label and as ?preferredLabel). I'd like to have the ?label and the ?preferredLabel in the same record. I've been puzzled for a bit on this one - seemingly simple, yet not so much! Thanks for the help