SPARQL嵌套在rdf文件中的元组

时间:2013-08-06 22:17:31

标签: rdf 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>

对于带有元组的列表中的每个项目,我想获取属性的信息:

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

1 个答案:

答案 0 :(得分: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>

使用Jena的rdfcat工具,我们可以在Turtle中找到它:

$ 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 .
}

如果您愿意,可以使查询更清晰:

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