从SPARQL结果中的文字中删除数据类型

时间:2014-05-13 08:27:26

标签: java rdf sparql jena semantic-web

我正在使用SPARQL查询OWL本体,但结果显示了链接和数据。我只想要数据。如何删除链接并显示数据?我得到的输出是:

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

我想要的输出是(注意^^<http://www.w3.org/2001/XMLSchema#decimal>之类的部分不再存在。

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

这是我的Java代码和我的OWL本体:

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>

2 个答案:

答案 0 :(得分:6)

您可能希望对RDF的某些概念更加熟悉。 W3C RDF Primer可能是一个很好的起点。我之所以这么说,是因为你引用结果的方式表明你对RDF有些不熟悉。我们来看看它们。在您显示的结果中,您将获得这些值:

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

第一个是文字5,数据类型为xsd:decimal。这是一个数字。在RDF 1.0中,文字是普通文字(只是一个字符串),语言标记字符串(带有语言标记的字符串)或数据类型文字(字符串(词法形式)和数据类型)。文字是用来表示具体数据的。 "5"^^xsd:decimal对于它是什么非常具体。你可以提取数据类型文字的词汇部分,留下普通文字"5",但请注意,这不再是一个数字。如果你想要一个Java编号,最好处理文字并获得它的解释值。

第二个是缩写为my:HotelCityPark的URI。根据您的前缀,这是URI http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelCityPark。 URI用作RDF中的标识符。您可以将该URI转换为普通文字,获取"http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelCityPark",然后执行一些字符串操作以删除所有内容,包括#,并留下普通文字"HotelCityPark"

第三个是数据类型的文字(数据类型为xsd:string)。就像第一个一样,你可以提取它的词法形式,留下普通的文字"Hotel",并没有真正丢失太多的信息。 (在RDF 1.1中,没有普通文字;没有显式数据类型的字符串使用xsd:string隐式输入。)

您请求的值

5
HotelCityPark
Hotel

没有足够的信息告诉我们您想要的类型的数据。那些应该是字符串吗?如果是这样,那么你应该使用

"5"
"HotelCityPark"
"Hotel"

如果您希望第一个成为数字,您也可以将其转换为xsd:integer,因为"5"^^xsd:integer形式的文字通常会写为5。例如,Turtle和N3语法支持这一点。

您可以使用xsd:integer进行转换,使用str函数进行字符串提取,并使用my:函数删除strafter前缀。因此,您有这样的查询:

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" |
-------------------------------------

答案 1 :(得分:0)

您应该使用BIND进行投射XPath Constructor