2013-08-06 57 views
0

如何寫SPARQL爲包含這些類型的元組的在RDF文件SPARQL嵌套元組

<rdf:Description rdf:about="http://example.com/BEROVO"> 
      <j.0:destDetails rdf:resource="http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661"/> 
      <j.0:destDetails rdf:resource="http://example.com/31dd238d-0356-4d78-b6fb-26647902ecd3"/> 
      <j.0:destDetails rdf:resource="http://example.com/76248058-5dd1-42a5-b988-affcf732ac6a"/> 
      <j.0:destDetails rdf:resource="http://example.com/f8541d66-4107-464a-bc31-60df76a4f7a4"/> 
      <j.0:destDetails rdf:resource="http://example.com/1298d38c-e69f-42ca-a329-f0cd8091a524"/> 
      <j.0:destDetails rdf:resource="http://example.com/5122c7a1-ca0d-4302-84ad-fea4909e8551"/> 
      <j.0:destDetails rdf:resource="http://example.com/cb25a063-0787-4079-8e4a-21e3440df8c3"/> 
      <j.0:destDetails rdf:resource="http://example.com/f72eeb27-4852-4737-b248-338bc05206b4"/> 
      <j.0:destDetails rdf:resource="http://example.com/0fc999e9-0a5a-4555-a7bf-6a4a41508ae8"/> 
      <j.0:destDetails rdf:resource="http://example.com/3303225e-f2dd-4712-9cb3-d10a69bd4357"/> 
      <j.0:destDetails rdf:resource="http://example.com/31d37728-a72d-445e-b554-d5cdc91830e5"/> 
</rdf:Description> 
<rdf:Description rdf:about="http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661"> 
      <j.0:distrName>BEROVOTRANS-BEROVO</j.0:distrName> 
      <j.0:moneyTwoDir>520 den.</j.0:moneyTwoDir> 
      <j.0:moneyOneDir>420 den.</j.0:moneyOneDir> 
      <j.0:hasTimeStop>17:15</j.0:hasTimeStop> 
      <j.0:hasTimeStart>13:20</j.0:hasTimeStart> 
</rdf:Description> 

對於從元組列表我想獲得信息的屬性每一個項目一個RDF文件:

  • j.0:distrName
  • j.0:moneyTwoDir
  • j.0:moneyOneDir
  • j.0:hasTimeStop
  • j.0:hasTimeStart

回答

6

雖然Turtle和SPARQL的語法並不完全相同,但很多Turtle表達式都適合作爲SPARQL模式。如果我們把你的數據的輕微修改(使之成爲完整的RDF文件),我們得到:

<rdf:RDF xmlns:j.0="http://example.com/" 
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> 
    <rdf:Description rdf:about="http://example.com/BEROVO"> 
    <j.0:destDetails rdf:resource="http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661"/> 
    <j.0:destDetails rdf:resource="http://example.com/31dd238d-0356-4d78-b6fb-26647902ecd3"/> 
    <j.0:destDetails rdf:resource="http://example.com/76248058-5dd1-42a5-b988-affcf732ac6a"/> 
    <j.0:destDetails rdf:resource="http://example.com/f8541d66-4107-464a-bc31-60df76a4f7a4"/> 
    <j.0:destDetails rdf:resource="http://example.com/1298d38c-e69f-42ca-a329-f0cd8091a524"/> 
    <j.0:destDetails rdf:resource="http://example.com/5122c7a1-ca0d-4302-84ad-fea4909e8551"/> 
    <j.0:destDetails rdf:resource="http://example.com/cb25a063-0787-4079-8e4a-21e3440df8c3"/> 
    <j.0:destDetails rdf:resource="http://example.com/f72eeb27-4852-4737-b248-338bc05206b4"/> 
    <j.0:destDetails rdf:resource="http://example.com/0fc999e9-0a5a-4555-a7bf-6a4a41508ae8"/> 
    <j.0:destDetails rdf:resource="http://example.com/3303225e-f2dd-4712-9cb3-d10a69bd4357"/> 
    <j.0:destDetails rdf:resource="http://example.com/31d37728-a72d-445e-b554-d5cdc91830e5"/> 
    </rdf:Description> 
    <rdf:Description rdf:about="http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661"> 
    <j.0:distrName>BEROVOTRANS-BEROVO</j.0:distrName> 
    <j.0:moneyTwoDir>520 den.</j.0:moneyTwoDir> 
    <j.0:moneyOneDir>420 den.</j.0:moneyOneDir> 
    <j.0:hasTimeStop>17:15</j.0:hasTimeStop> 
    <j.0:hasTimeStart>13:20</j.0:hasTimeStart> 
    </rdf:Description> 
</rdf:RDF> 

使用耶拿的rdfcat工具,我們可以在龜得到這樣的:

$ rdfcat -out Turtle data.rdf 
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . 

<http://example.com/BEROVO> 
     <http://example.com/destDetails> 
       <http://example.com/31dd238d-0356-4d78-b6fb-26647902ecd3> , <http://example.com/5122c7a1-ca0d-4302-84ad-fea4909e8551> , <http://example.com/cb25a063-0787-4079-8e4a-21e3440df8c3> , <http://example.com/3303225e-f2dd-4712-9cb3-d10a69bd4357> , <http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661> , <http://example.com/1298d38c-e69f-42ca-a329-f0cd8091a524> , <http://example.com/31d37728-a72d-445e-b554-d5cdc91830e5> , <http://example.com/0fc999e9-0a5a-4555-a7bf-6a4a41508ae8> , <http://example.com/76248058-5dd1-42a5-b988-affcf732ac6a> , <http://example.com/f72eeb27-4852-4737-b248-338bc05206b4> , <http://example.com/f8541d66-4107-464a-bc31-60df76a4f7a4> . 

<http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661> 
     <http://example.com/distrName> 
       "BEROVOTRANS-BEROVO" ; 
     <http://example.com/hasTimeStart> 
       "13:20" ; 
     <http://example.com/hasTimeStop> 
       "17:15" ; 
     <http://example.com/moneyOneDir> 
       "420 den." ; 
     <http://example.com/moneyTwoDir> 
       "520 den." . 

果然,這幾乎是相應的SPARQL查詢會是什麼樣子:

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 

select ?details ?name ?moneyTwoDir ?moneyOneDir ?timeStop ?timeStart where { 

    <http://example.com/BEROVO> 
    <http://example.com/destDetails> 
     ?details . 

    ?details 
    <http://example.com/distrName> 
     ?name ; 
    <http://example.com/hasTimeStart> 
     ?timeStart ; 
    <http://example.com/hasTimeStop> 
     ?timeStop ; 
    <http://example.com/moneyOneDir> 
     ?moneyOneDir ; 
    <http://example.com/moneyTwoDir> 
     ?moneyTwoDir . 
} 

您可以查詢了一下清潔劑,如果你w ^螞蟻:

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
prefix ex: <http://example.com/> 

select ?details ?name ?moneyTwoDir ?moneyOneDir ?timeStop ?timeStart where { 

    ex:BEROVO ex:destDetails ?details . 

    ?details ex:distrName ?name ; 
      ex:hasTimeStart ?timeStart ; 
      ex:hasTimeStop ?timeStop ; 
      ex:moneyOneDir ?moneyOneDir ; 
      ex:moneyTwoDir ?moneyTwoDir . 
} 

結果如你所期望的:

$ arq --data data.rdf --query query.sparql 
--------------------------------------------------------------------------------------------------------------------- 
| details         | name     | moneyTwoDir | moneyOneDir | timeStop | timeStart | 
===================================================================================================================== 
| ex:0c64a8f2-fd39-4e9d-a03b-527359af8661 | "BEROVOTRANS-BEROVO" | "520 den." | "420 den." | "17:15" | "13:20" | 
--------------------------------------------------------------------------------------------------------------------- 
+0

我可以用你的SPARQL我的RDF數據,而無需將其轉換成龜? – vikifor

+1

@vikifor是的!我轉換爲Turtle是因爲Turtle語法更接近SPARQL三重模式,因此可以很容易地基於Turtle語法序列化數據構建S​​PARQL查詢。 Turtle和RDF/XML都是RDF的串行化,SPARQL引擎應該能夠讀取數據的RDF/XML序列化。 –