繁体   English   中英

SPARQL在rdf文件中嵌套元组

[英]SPARQL nested tuples in rdf file

如何为包含这些元组的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

尽管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工具,我们可以在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"   |
---------------------------------------------------------------------------------------------------------------------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM