scalasparqlsesamerdf4j

RDF4J method for splitting literal into value and datatype


If I have connection cxn to an empty triplestore and I say:

val CheckSparql = "select (count(?s) as ?scount) where { ?s ?p ?o . }"

val PreparedSparql = cxn.prepareTupleQuery(QueryLanguage.SPARQL, CheckSparql)
val Result = PreparedSparql.evaluate()
val ResBindSet = Result.next()
val TripleCount = ResBindSet.getValue("scount")

println(TripleCount.toString())

I get

"0"^^<http://www.w3.org/2001/XMLSchema#integer>

Is there an RDF4J method for retrieving just the value of that literal?

I just want 0 or "0", without the datatype

I figured I should try to do this without string manipulation/regular expressions if possible.


Solution

  • The Literals util class is your friend:

    int scount = Literals.getIntValue(ResBindSet.getValue("scount"))
    

    or if you prefer a string:

    String scount =  Literals.getLabel(ResBindSet.getValue("scount"))
    

    You can also optionally add a fallback argument, for example if the supplied argument turns out not to be a literal.