PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX fun: <http://w3id.org/sparql-generate/fn/>

SELECT <select/title.rqg>( ?ontology, ?lang ) ?before  ( fun:log("title is {?before}") AS ?null )
WHERE { 
  VALUES ( ?property ?priority ) { 
    ( dcterms:title 1 ) 
    ( dc:title 1 ) 
    ( rdfs:label 2 )  
    ( skos:prefLabel 2 ) 
    ( obo:IAO_0000118 2 ) }
  ?ontology ?property ?title .
  FILTER( LANG(?title) = str(?lang) || LANG(?title) = "")
  BIND(REPLACE(?title, "[\n|\r|\r\n].*", "") AS ?before )
} ORDER BY DESC ( LANG(?title)) ?priority
LIMIT 1 