Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing datatypes from literals in SPARQL results

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:

---------------------------------------------------------------------------------------------------------------------------
| Email                                           | ind              | ind1                                               |
===========================================================================================================================
| "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.

---------------------------------
| Email | ind           | ind1  |
=================================
| 5     | HotelCityPark | Hotel |
---------------------------------

Here is my Java code and my OWL ontology:

class jena_testing
    {  public static void main(String[] args) {

            String filename="modified2.owl";
            Model model=ModelFactory.createDefaultModel();
            OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM,model);

            try
            {
                File file=new File(filename);
                FileInputStream reader=new FileInputStream(file);
                model.read(reader,null);

                String query1="PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
                    "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
                        "PREFIX my: <http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#>"+
                        "SELECT ?Email ?ind ?ind1"+
                        " WHERE {"+
                         " ?ind rdf:type my:Accomodation ."+
                          "?ind my:hasStarRating ?Email."+
                          "?ind my:hasType ?ind1."+
                        "FILTER regex(str(?ind),'HotelCityPark')"+
                        "}";
                com.hp.hpl.jena.query.Query query=QueryFactory.create(query1);
                QueryExecution exe=QueryExecutionFactory.create(query, model1);
                ResultSet RES=exe.execSelect();
                ResultSetFormatter.out(System.out, RES, query);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
    <!ENTITY untitled-ontology-20 "http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#"
     xml:base="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:untitled-ontology-20="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Data properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasLatitude -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasLatitude"/>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasLongitude -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasLongitude"/>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasStarRating -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasStarRating"/>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasStreet -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasStreet"/>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasType -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasType"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Accomodation -->

    <owl:Class rdf:about="&untitled-ontology-20;Accomodation"/>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Attractions -->

    <owl:Class rdf:about="&untitled-ontology-20;Attractions"/>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Restuarants -->

    <owl:Class rdf:about="&untitled-ontology-20;Restuarants"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Individuals
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#AkkalkotSwamiSamarthMaharaj_temple -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;AkkalkotSwamiSamarthMaharaj_temple">
        <rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Ambassador_Executive -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Ambassador_Executive">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">3</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#BalajiSarovarPremier -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;BalajiSarovarPremier">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">3.5</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#BhagwatBigCinemas -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;BhagwatBigCinemas">
        <rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Bhuikotfort -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Bhuikotfort">
        <rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#DhruvaHotel -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;DhruvaHotel">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">3</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Durga_Mangesh_Garden_Restuarant -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Durga_Mangesh_Garden_Restuarant">
        <rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#GreatIndianBustardSancturay -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;GreatIndianBustardSancturay">
        <rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelCityPark -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelCityPark">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">5</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotelcitypark</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelLotus -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelLotus">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">3</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelNila -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelNila">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">1.2</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelTripursundari -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelTripursundari">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">1.6</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelVaishnavi -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelVaishnavi">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">4.5</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#KRAVINGS-THE-FOOD-WORLD -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;KRAVINGS-THE-FOOD-WORLD">
        <rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Kamat -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Kamat">
        <rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#NikitaPurVeg -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;NikitaPurVeg">
        <rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Pandurang_temple -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Pandurang_temple">
        <rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Parasnath_Jain_Temple -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Parasnath_Jain_Temple">
        <rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#PrathamHotel -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;PrathamHotel">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">1.6</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#RevanSiddeshwar_temple -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;RevanSiddeshwar_temple">
        <rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#RiteshHotel -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;RiteshHotel">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">3.5</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel1</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Rukmini_temple -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Rukmini_temple">
        <rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Shivparvati_International_Hotel -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Shivparvati_International_Hotel">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">4</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#ShriSiddheshwar_temple -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;ShriSiddheshwar_temple">
        <rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Sips_and_Bites -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Sips_and_Bites">
        <rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Spice_n_Ice -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Spice_n_Ice">
        <rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
        <hasStarRating rdf:datatype="&xsd;decimal">3.5</hasStarRating>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Srikamal_International_Hotel -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Srikamal_International_Hotel">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">4.8</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Supraja_Pav_Bhaji -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Supraja_Pav_Bhaji">
        <rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#SuryaInternational -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;SuryaInternational">
        <rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
        <hasStarRating rdf:datatype="&xsd;decimal">5</hasStarRating>
        <hasType rdf:datatype="&xsd;string">Hotel</hasType>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Temptations -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-20;Temptations">
        <rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
    </owl:NamedIndividual>
</rdf:RDF>
like image 463
fanky Avatar asked May 13 '14 08:05

fanky


1 Answers

You may want to become a bit more familiar with some of the concepts of RDF. The W3C's RDF Primer might be a good starting point. I say this because the way that you're referring to your results suggests some unfamiliarity with RDF. Let's take a look at them. In the results that you've shown, you're getting these values back:

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

The first is the literal 5 with datatype xsd:decimal. That's a number. In RDF 1.0, literals are either plain literals (just a string), language tagged strings (a string with a language tag), or a datatyped literal (a string (lexical form) and datatype). Literals are what you use to represent concrete data. "5"^^xsd:decimal is very specific about what it is. You can extract the lexical part of the datatyped literal to be left with the plain literal "5", but note that that's no longer a number. If you want a Java number, it'd be better to process to the literal and get its interpreted value.

The second is a URI that's been abbreviated as my:HotelCityPark. Based on your prefix, that's the URI http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelCityPark. URIs are used as identifiers in RDF. You can cast that URI to a plain literal, to get "http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelCityPark", and then do some string manipulation to remove everything up to and including the #, to be left with the plain literal "HotelCityPark".

The third is a datatyped literal (with the datatype xsd:string). Just like with the first, you can extract its lexical form to be left with the plain literal "Hotel", and that doesn't really lose much information. (In RDF 1.1, there are no plain literals; strings without an explicit datatype are implicitly typed with xsd:string.)

The values you requested

5
HotelCityPark
Hotel

don't really have enough information to tell us what kind of data you want. Are those supposed to be strings? If so, then you should use

"5"
"HotelCityPark"
"Hotel"

If you want the first to be a number, you could also cast it as an xsd:integer, because literals of the form "5"^^xsd:integer are often written as 5. The Turtle and N3 syntaxes support this, for instance.

You can do the casting with xsd:integer, string extraction with the str function, and removing the my: prefix using the strafter function. Thus you'd have a query like this:

prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix my: <http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#>

select ?Email ?ind ?ind1 where {
   #-- The values block just binds ?_Email, ?_ind, and ?_ind1 
   #-- to the original output that you were getting.  You'd still
   #-- have your original query text here.
   values (?_Email ?_ind ?_ind1) {
     ("5"^^xsd:decimal my:HotelCityPark "Hotel"^^xsd:string)
   }

   bind( xsd:integer(?_Email) as ?Email )
   bind( strafter(str(?_ind),str(my:)) as ?ind  )
   bind( str(?_ind1) as ?ind1 )
}
-------------------------------------
| Email | ind             | ind1    |
=====================================
| 5     | "HotelCityPark" | "Hotel" |
-------------------------------------
like image 73
Joshua Taylor Avatar answered Oct 17 '22 12:10

Joshua Taylor