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

fan*_*nky 4 java rdf semantic-web sparql jena

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

---------------------------------------------------------------------------------------------------------------------------
| Email                                           | ind              | ind1                                               |
===========================================================================================================================
| "5"^^<http://www.w3.org/2001/XMLSchema#decimal> | my:HotelCityPark | "Hotel"^^<http://www.w3.org/2001/XMLSchema#string> |
---------------------------------------------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

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

---------------------------------
| Email | ind           | ind1  |
=================================
| 5     | HotelCityPark | Hotel |
---------------------------------
Run Code Online (Sandbox Code Playgroud)

这是我的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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)

Jos*_*lor 6

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

"5"^^<http://www.w3.org/2001/XMLSchema#decimal>
my:HotelCityPark
"Hotel"^^<http://www.w3.org/2001/XMLSchema#string>
Run Code Online (Sandbox Code Playgroud)

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

第二个是缩写为的URI my:HotelCityPark.根据您的前缀,这是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
Run Code Online (Sandbox Code Playgroud)

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

"5"
"HotelCityPark"
"Hotel"
Run Code Online (Sandbox Code Playgroud)

如果你想让第一个成为数字,你也可以把它作为一个数字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 )
}
Run Code Online (Sandbox Code Playgroud)
-------------------------------------
| Email | ind             | ind1    |
=====================================
| 5     | "HotelCityPark" | "Hotel" |
-------------------------------------
Run Code Online (Sandbox Code Playgroud)