如何在 SPARQL (dbpedia) 中将 xsd:Date 转换为 xsd:DateTime?
How to convert xsd:Date into xsd:DateTime in SPARQL (dbpedia)?
我想从 DBpedia 获取关于人的数据,包括他们的生日和他们的死亡日期,但是对于日期,当我需要 xsd:DateTime
格式的日期时,返回的类型是 xsd:Date
。
我们可以将查询中的结果 (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).
}
谢谢!
(作为回答,因为评论不是很可读)
正如 AKSW 所说,数据略有损坏(例如“1800-1-1”),如果您尝试施放 xsd:dateTime(?birthDate)
,演奏家会抱怨。由于专家的怪癖,您不能使用更令人愉快的 coalesce(xsd:dateTime(?birthDate), ?birthDate)
,它应该 return 第一个非空、非错误值。
所以我们必须用正则表达式来保护自己:
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.
}
我想从 DBpedia 获取关于人的数据,包括他们的生日和他们的死亡日期,但是对于日期,当我需要 xsd:DateTime
格式的日期时,返回的类型是 xsd:Date
。
我们可以将查询中的结果 (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).
}
谢谢!
(作为回答,因为评论不是很可读)
正如 AKSW 所说,数据略有损坏(例如“1800-1-1”),如果您尝试施放 xsd:dateTime(?birthDate)
,演奏家会抱怨。由于专家的怪癖,您不能使用更令人愉快的 coalesce(xsd:dateTime(?birthDate), ?birthDate)
,它应该 return 第一个非空、非错误值。
所以我们必须用正则表达式来保护自己:
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.
}