对于单个项目,这是一个使用 SPARQL 1.1 的子查询的非常简单的查询。诀窍是按日期对具有给定属性的修订进行排序,并从最新修订中获取值。该values
表单仅用于指定您选择的项目。如果您需要查询更多项目,您可以将它们添加到values
块中。
prefix mymeta: <http://www.mymeta.com/meta/>
prefix dc: <http://purl.org/dc/elements/1.1/>
select ?item ?title ?format ?extent where {
values ?item { <urn:ITEMID:12345> }
#-- Get the title by examining all the revisions that specify a title,
#-- ordering them by date, and taking the latest one. The same approach
#-- is used for the format and extent.
{ select ?title { ?item mymeta:itemchange [ dc:title ?title ; dc:issued ?date ] . }
order by desc(?date) limit 1 }
{ select ?format { ?item mymeta:itemchange [ dc:format ?format ; dc:issued ?date ] . }
order by desc(?date) limit 1 }
{ select ?extent { ?item mymeta:itemchange [ dc:extent ?extent ; dc:issued ?date ] . }
order by desc(?date) limit 1 }
}
$ sparql --data data.n3 --query query.rq
----------------------------------------------------------------------------------
| item | title | format | extent |
==================================================================================
| <urn:ITEMID:12345> | "Improved Product Name"@en | "4 x 6 x 1 in"@en | "200"@en |
----------------------------------------------------------------------------------
如果您确实需要对所有项目执行此操作,则可以使用另一个子查询来选择项目。也就是说,代替values ?item { ... }
,使用:
{ select ?item { ?item a mymeta:item } }
虽然原始问题中没有提到它,但它出现在评论中,如果您有兴趣获取所有属性的最新属性值,您可以使用以下子查询,该子查询基于How to限制 SPARQL 解决方案组的大小?
select ?item ?property ?value {
values ?item { <urn:ITEMID:12345> }
?item mymeta:itemchange [ ?property ?value ; dc:issued ?date ]
#-- This subquery finds the earliest date for each property in
#-- the graph for each item. Then, outside the subquery, we
#-- retrieve the particular value associated with that date.
{
select ?property (max(?date_) as ?date) {
?item mymeta:itemchange [ ?property [] ; dc:issued ?date_ ]
}
group by ?item ?property
}
}
---------------------------------------------------------------
| item | property | value |
===============================================================
| <urn:ITEMID:12345> | dc:issued | "2007-06-01"@en |
| <urn:ITEMID:12345> | dc:title | "Improved Product Name"@en |
| <urn:ITEMID:12345> | dc:extent | "200"@en |
| <urn:ITEMID:12345> | dc:format | "4 x 6 x 1 in"@en |
---------------------------------------------------------------