我想从DBpedia获取有关人员的数据,包括他们的出生日期和死亡日期,但是对于日期,当我需要xsd:Date
格式的日期时,返回的类型为xsd:DateTime
。
我们可以在查询中将结果(xsd:Date
)转换为xsd:DateTime
吗?如果是,怎么样?
xsd:Date
- > "1940-01-01"
xsd:DateTime
- > "1940-01-01T00:00:00"
我尝试了下面的查询,但它不起作用......
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
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#>
SELECT ?person ?name ?birthDate ?deathDate
WHERE {
?person rdf:type <http://dbpedia.org/ontology/Person>
?person rdfs:label ?name.
?person dbo:birthDate ?birthDate.
?person dbo:deathDate ?deathDate.
strdt(concat(substr(?birthDate, 7, 4), '-', substr(?birthDate, 1, 2), '-', substr(?birthDate, 4, 2), 'T00:00:00'), xsd:dateTime).
strdt(concat(substr(?deathDate, 7, 4), '-', substr(?deathDate, 1, 2), '-', substr(?deathDate, 4, 2), 'T00:00:00'), xsd:dateTime).
}
谢谢!
答案 0 :(得分:2)
(作为答案,因为评论不是非常易读)
正如AKSW所说,数据稍有破坏(例如&#39; 1800-1-1&#39;)如果你试图施放xsd:dateTime(?birthDate)
,艺术家会抱怨。由于一个大师级的怪癖,你不能使用更令人愉快的coalesce(xsd:dateTime(?birthDate), ?birthDate)
,它应该返回第一个非空的非错误值。
所以我们必须用正则表达式保护自己:
if(
regex(str(?birthDate), '\\d{4}-\\d\\d-\\d\\d'),
xsd:dateTime(?birthDate),
?birthDate)
即:如果?birthDate
的字符串值是正确的形式(4位数 - 2位数 - 2位数),则转换,否则使用原始值。
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
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#>
SELECT ?person ?name
(if(regex(str(?birthDate), '\\d{4}-\\d\\d-\\d\\d'), xsd:dateTime(?birthDate), ?birthDate) as ?birthDateDT)
(if(regex(str(?deathDate), '\\d{4}-\\d\\d-\\d\\d'), xsd:dateTime(?deathDate), ?deathDate) as ?deathDateDT)
WHERE {
?person rdf:type <http://dbpedia.org/ontology/Person> .
?person rdfs:label ?name.
?person dbo:birthDate ?birthDate.
?person dbo:deathDate ?deathDate.
}