我创建了一个SPARQL查询来比较dbo:birthDate的年份与dbo:birthYear的值是否不同:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

Select distinct ?s ?vv1 ?vv2 where
  { ?s a dbo:Person ; dbo:birthDate ?v1 .
    ?s dbo:birthYear ?v2 .
    bind(year(xsd:date(?v1)) as ?vv1) .
    bind(xsd:gYear(?v2) as ?vv2) .
    filter(?vv1 !=  ?vv2)
  }


我得到以下答案:

                S                  vv1    vv2
<http://example.com/resource/1>   1993   1993
<http://example.com/resource/5>   1998   1993
<http://example.com/resource/7>   1978   1978
<http://example.com/resource/13>  1993   1993


正确的答案仅是资源5,该资源的birthDate年份等于1998,而birthYear年份等于1993。答案表明vv1vv2的值不包含" "的年份,并且显然都具有相同的类型。因此,我不明白为什么SPARQL说记录vv1中的vv21是不同的。我寻找了如何在SPARQL中比较日期,我认为查询是正确的。

我使用Fuseki 2.3.1测试了此查询

我的实例的例子:

@prefix dbo:   <http://dbpedia.org/ontology/> .
@prefix :      <http://example.com/resource/> .
@prefix dbp:   <http://dbpedia.org/property/> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml:   <http://www.w3.org/XML/1998/namespace> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix mu:    <http://example.com/resource/> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

mu:1    a                dbo:Actor ,
                         dbo:Person ,
                         dbo:MusicalArtist ,
                         mu:MusicActor ,
                         owl:NamedIndividual ;
        dbo:birthDate    "1993-03-11"^^xsd:date ;
        dbo:birthYear    "1993" ;
        foaf:name        "John Joe"@en ,
                         "Joe, John"@en .

mu:5    a                <http://example.com/MusicActor> ,
                         dbo:Actor ,
                         dbo:Person ,
                         mu:MusicActor ,
                         owl:NamedIndividual ;
        dbo:birthDate    "1998-04-11" ,
                         "1998-04-11"^^xsd:date ;
        dbo:birthYear    "1993" ;
        foaf:name        "Barry Hannah"@en ,
                         "Barry B. Hannah"@en .

mu:7    a                dbo:Person ,
                         dbo:MusicalArtist ,
                         owl:NamedIndividual ;
        dbo:birthDate    "1978-05-11" ,
                         "1978-05-11"@en ;
        dbo:birthYear    "1978" ;
        foaf:name        "Helen Petty" ,
                         "Helen Petty"@en .


任何人都可以说为什么会这样吗?不幸的是我不知道。任何帮助将不胜感激!

最佳答案

SPARQL year()函数返回一个整数,因此您需要将?v2强制转换为整数:

SPARQL DISTINCT ?s ?vv1 ?vv2
WHERE {
   ?s a dbo:Person ; dbo:birthDate ?v1 .
   ?s dbo:birthYear ?v2 .
   BIND(year(xsd:date(?v1)) as ?vv1) .
   BIND(xsd:integer(?v2) as ?vv2) .
   FILTER(?vv1 != ?vv2)
}

关于date - 如何在SPARQL中将日期值(xsd:date)与年份进行比较?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39000608/

10-15 18:45