3 Replies Latest reply on Dec 10, 2014 10:05 PM by alwu-Oracle

    [JAVA] Removing datatypes from literals in SPARQL results

    2773504


      I'm querying an OWL ontology using SPARQL, but the results show links as well as data. I only want the data. How can I remove the link and show on the data? The output I'm getting is:

      ===========================================================================================================================
      | "5"^^<http://www.w3.org/2001/XMLSchema#decimal> | my:HotelCityPark | "Hotel"^^<http://www.w3.org/2001/XMLSchema#string> |
      ---------------------------------------------------------------------------------------------------------------------------


      The output I desire is (note that parts like ^^<http://www.w3.org/2001/XMLSchema#decimal> are no longer present.


      =================================
      | 5 | HotelCityPark | Hotel |
      ---------------------------------


      thanks

        • 1. Re: [JAVA] Removing datatypes from literals in SPARQL results
          alwu-Oracle

          Hi,

           

          Which Java API did you use? For example, one may use the following:

           

          QueryExecution qe = QueryExecutionFactory.create(query, model);

          ResultSet results = qe.execSelect();

           

          Thanks,


          Zhe Wu

          • 2. Re: [JAVA] Removing datatypes from literals in SPARQL results
            2773504

            I use orale Jena,, I want to have the result of the racket without URI? (the value of the literal)

            • 3. Re: [JAVA] Removing datatypes from literals in SPARQL results
              alwu-Oracle

              Hi,

               

              It is quite simple then. Assume we have a graph with a few triples.

               

              oracle = new oracle.spatial.rdf.client.jena.Oracle("jdbc:oracle:thin:@....", "scott", "password");
              model = oracle.spatial.rdf.client.jena.ModelOracleSem.createOracleSemModel(oracle,"graph");
              graph = model.getGraph();

               

              sub = Node.createURI("http://sub/a");
              pred = Node.createURI("http://pred/a");
              obj = NodeFactory.createLiteral("1.234", com.hp.hpl.jena.datatypes.xsd.XSDDatatype.XSDdecimal);
              triple = Triple.create(sub, pred, obj);
              graph.add(triple);

               

              ...

               

              sub = Node.createURI("http://sub/a");
              pred = Node.createURI("http://pred/a");
              obj = Node.createURI("http://pred/o");
              triple = Triple.create(sub, pred, obj);
              graph.add(triple);

               

               

              Say we want to get ?o bindings of the following SPARQL query,

              sparqlQuery=" SELECT ?o WHERE { ?s ?p ?o } limit 30 ";

               

              One can easily iterate through the result set, and get the literal value if the variable binding is a Literal.

               

              varO = com.hp.hpl.jena.sparql.core.Var.alloc("o");

              query = com.hp.hpl.jena.query.QueryFactory.create(sparqlQuery, com.hp.hpl.jena.query.Syntax.syntaxSPARQL);

              qexec = com.hp.hpl.jena.query.QueryExecutionFactory.create(query, model);

              rs = qexec.execSelect();

              while ( rs.hasNext() ) {

                resultBinding = rs.next();

                node =resultBinding.getBinding().get(varO);

                if (node.isLiteral()) {

                  System.out.println(node.getLiteralValue());

                }

                else {

                  System.out.println(node.toString());

                }

              }

               

              ==>

              1.234

              hello

              http://pred/o

               

              Hope it helps,

               

              Zhe Wu