sparql查询:从列表

时间:2016-08-05 21:52:23

标签: sparql

我的查询返回的结果取决于两个sparql OPTIONS子句中的哪一个首先出现在图像列表中。尽管此时使用Jena ARQ不是一个选项,并且我想通过纯SPARQL查询解决这个问题,但我仍然想知道如何使用Jena解决它。 我的数据显示如下,数据可能包含图像列表。我的图像表示也在下面。我也附上了我的查询。 如果存在图像列表,则sprql查询有两个变量urlX和在2个OPTIONS块中声明的urlY。根据首先出现的OPTIONS,我得到了那个变量的值,而另一个变量没有达到。似乎问题与使用OPTIONS子句有关。我不确定我还能尝试什么,我远不是sparql查询的专家。我希望查询执行以下操作:如果存在图像集合,我想查看是否存在两个图像大小(dc:conformsTo)并获取urlX和urlY值,或者获取存在的值。非常感谢你的时间。

我的数据表示:

@prefix dc: <http://purl.org/dc/terms/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix lews: <http://lews.com/content/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

lews:26331340 lews:name "the Human Good Luck Charm is Back"^^xsd:token ;
        dc:created "2014-10-20T17:14:55.357-07:00"^^xsd:dateTime ;
        dc:identifier "26331340"^^xsd:int ;
        dc:modified "2016-08-04T13:43:00.897-07:00"^^xsd:dateTime ;
        dc:title "the Human Good Luck Charm is Back" ;
        dc:hasPart <http://lews.com/content/26331340#Images> ;
        dc:abstract "As the World Series gets underway..." ;
        dc:description "The super fan who rooted for the Royals is back to boost morale." ;
        dc:subject "hoping for a World Series victory".

<http://lews.com/content/26331340#Images> dc:identifier "Images"^^xsd:token ;
        rdf:first lews:26331375 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 ;
        rdf:li <http://lews.com/content/26331340#Images> , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 ;
        a rdf:List .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 rdf:first lews:26331376 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 rdf:first lews:26331377 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 rdf:first lews:26331378 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 rdf:first lews:26331379 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 rdf:first lews:26331380 ;
        rdf:rest rdf:nil .

我的图片表示:

@prefix dc: <http://purl.org/dc/terms/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix lews: <http://abcnews.com/content/> .
@prefix mrss: <http://search.yahoo.com/mrss/> .

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

lews:26331376 lews:name "141020_wn_donvan0_704x396.jpg"^^xsd:token ;
        lews:section "wnt"^^xsd:token ;
        lews:type "Image"^^xsd:token ;
        dc:conformsTo "704x396"^^xsd:token ;
        dc:created "2014-10-20T17:15:09.637-07:00"^^xsd:dateTime ;
        dc:hasFormat <http://lews.go.com/images/WNT/141020_wn_donvan0_704x396.jpg> ;
        dc:identifier "26331376"^^xsd:int ;
        dc:isPartOf <http://lews.go.com/WNT> ;
        dc:modified "2014-10-20T17:15:09.947-07:00"^^xsd:dateTime ;
        dc:type "StillImage"^^xsd:token ;
        mrss:height "396"^^xsd:int ;
        mrss:width "704"^^xsd:int ;
        xsd:date "2014-10-20"^^xsd:date ;
        xsd:gMonthDay "--10-20"^^xsd:gMonthDay ;
        xsd:gYear "2014"^^xsd:gYear ;
        xsd:gYearMonth "2014-10"^^xsd:gYearMonth .

我的查询:

PREFIX dc:     <http://purl.org/dc/terms/>
PREFIX mrss:   <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName
    ?subject dc:identifier ?id.

    OPTIONAL {?subject dc:title ?title.}
    OPTIONAL {?subject dc:description ?description.}
    OPTIONAL {?subject dc:isPartOf ?section.}

    OPTIONAL {  
                ?subject dc:hasPart ?imageCol.
                ?imageCol dc:identifier "Images"^^xsd:token. 

                OPTIONAL{
                    ?imageCol rdf:li ?bnode.
                    ?bnode rdf:first ?image.
                    ?image lews:name ?imgName;                               
                           dc:conformsTo "4x3";
                           dc:hasFormat ?urlX.                                   
                }        
                OPTIONAL{
                    ?imageCol rdf:li ?bnode.
                    ?bnode rdf:first ?image.
                    ?image lews:name ?imgName;                               
                           dc:conformsTo "16x9";
                           dc:hasFormat ?urlY.                                   
                }        

    }

}
LIMIT ${limit}

1 个答案:

答案 0 :(得分:1)

如果我理解了你想要的是什么,你只需要以不同的方式对选项进行分组:

PREFIX dc:     <http://purl.org/dc/terms/>
PREFIX mrss:   <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName {
    ?subject dc:identifier ?id.

    OPTIONAL { ?subject dc:title ?title. }
    OPTIONAL { ?subject dc:description ?description. }
    OPTIONAL { ?subject dc:isPartOf ?section. }

    OPTIONAL {
        ?subject dc:hasPart ?imageCol.
        ?imageCol dc:identifier "Images"^^xsd:token.

        OPTIONAL {
            ?imageCol rdf:li ?bnode.
            ?bnode rdf:first ?image.
            ?image lews:name ?imgName;
            # Here we optionally bind ?urlX and/or ?urlY
            OPTIONAL {
                ?image dc:conformsTo "4x3";
                    dc:hasFormat ?urlX.
            }
            OPTIONAL {
                ?image dc:conformsTo "16x9";
                    dc:hasFormat ?urlY.
            }
        }
    }
}