SPARQL 查询返回意外结果

46 阅读2分钟

有一位用户在使用 SPARQL 查询 RDF 数据时遇到了问题。他希望从 RDF 中读取 foaf:homepage 的值,但无论是使用 Python 和 RDflib 在本地运行查询,还是使用 Online Redland SPARQL Query Demo 在线查询,都无法获得结果。

以下是用户提供的 RDF 数据:

@prefix bibo: <http://purl.org/ontology/bibo/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ex: <http://example.org/thesis/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<ex:XY>     a bibo:Article;
     dc:creator ex:umstaetter;
     dc:date "2008-11-01";
     dc:isPartOf ex:bibdienst;
     dc:title "DDC in Europa"@de;
     bibo:endPage "1221";
     bibo:issue "11";
     bibo:language "de";
     bibo:pageStart "1194";
     bibo:uri <http://www.zlb.de/Erschliessung020309BD.pdf>;
     bibo:volume "42" .

<ex:bibdienst>     a bibo:Journal;
     dc:publisher ex:zlb;
     dc:title "Bibliotheksdienst"@de;
     bibo:issn "00061972" .

<ex:umstaetter>     a foaf:person;
     foaf:birthday "1941-06-12";
     foaf:gender "Male";
     foaf:givenName "Walther";
     foaf:homepage <http://www.ib.hu-berlin.de/~wumsta/index.html>;
     foaf:img "http://libreas.eu/ausgabe7/pictures/wumstaetter1.jpg";
     foaf:name "Walther Umst\u00E4tter";
     foaf:surname "Umst\u00E4tter";
     foaf:title "Prof. Dr. rer. nat." .

<ex:zlb>     a foaf:Organization;
     foaf:homepage <http://www.zlb.de>;
     foaf:name "Zentral- und Landesbibliothek Berlin"@de .

以下是用户希望运行的 SPARQL 查询:

PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX bibo: <http://purl.org/ontology/bibo/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX ex: <http://example.org/thesis/>

SELECT ?article ?publisher ?publisher_url
WHERE 
{
    ?article dc:isPartOf ?journal .
    ?journal dc:publisher ?publisher .
    ?publisher foaf:homepage ?publisher_url
}

2、解决方案

对于该问题,有两位用户给出了不同的解决方案:

解决方案一: 这位用户指出,在 XML 属性值中不能使用 QName,并建议将 RDF/XML 文件中的 QName 替换为对应的 URI。

修改后的 RDF/XML 文件:

@prefix bibo: <http://purl.org/ontology/bibo/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ex: <http://example.org/thesis/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<bibo:Journal rdf:about="http://example.org/thesis/bibdienst">
     dc:publisher <http://example.org/thesis/zlb>;
     dc:title "Bibliotheksdienst"@de;
     bibo:issn "00061972" .

<bibo:Article rdf:about="http://example.org/thesis/XY">
     dc:creator <http://example.org/thesis/umstaetter>;
     dc:date "2008-11-01";
     dc:isPartOf <http://example.org/thesis/bibdienst>;
     dc:title "DDC in Europa"@de;
     bibo:endPage "1221";
     bibo:issue "11";
     bibo:language "de";
     bibo:pageStart "1194";
     bibo:uri <http://www.zlb.de/Erschliessung020309BD.pdf>;
     bibo:volume "42" .

<foaf:person rdf:about="http://example.org/thesis/umstaetter">
     foaf:birthday "1941-06-12";
     foaf:gender "Male";
     foaf:givenName "Walther";
     foaf:homepage <http://www.ib.hu-berlin.de/~wumsta/index.html>;
     foaf:img "http://libreas.eu/ausgabe7/pictures/wumstaetter1.jpg";
     foaf:name "Walther Umst\u00E4tter";
     foaf:surname "Umst\u00E4tter";
     foaf:title "Prof. Dr. rer. nat." .

<foaf:Organization rdf:about="http://example.org/thesis/zlb">
     foaf:homepage <http://www.zlb.de>;
     foaf:name "Zentral- und Landesbibliothek Berlin"@de .

解决方案二: 这位用户指出,可以在 XML 属性值中使用 XML 实体,而无需使用 QName。

修改后的 RDF/XML 文件:

<!DOCTYPE rdf:RDF[
    <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
    <!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#'>
    <!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#'>
    <!ENTITY ex 'http://example.org/thesis/'>
    <!ENTITY dc 'http://purl.org/dc/elements/1.1/'>
    <!ENTITY foaf 'http://xmlns.com/foaf/0.1/'>
    <!ENTITY bibo 'http://purl.org/ontology/bibo/'>
]>

<bibo:Journal rdf:about="&ex;bibdienst">
     dc:publisher &ex;zlb;
     dc:title "Bibliotheksdienst"@de;
     bibo:issn "00061972" .

<bibo:Article rdf:about="&ex;XY">
     dc:creator &ex;umstaetter;
     dc:date "2008-11-01";
     dc:isPartOf &ex;bibdienst;
     dc:title "DDC in Europa"@de;
     bibo:endPage "1221";
     bibo:issue "11";
     bibo:language "de";
     bibo:pageStart "1194";
     bibo:uri <http://www.zlb.de/Erschliessung020309BD.pdf>;
     bibo:volume "42" .

<foaf:person rdf:about="&ex;umstaetter">
     foaf:birthday "1941-06-12";
     foaf:gender "Male";
     foaf:givenName "Walther";
     foaf:homepage <http://www.ib.hu-berlin.de/~wumsta/index.html>;
     foaf:img "http://libreas.eu/ausgabe7/pictures/wumstaetter1.jpg";
     foaf:name "Walther Umst\u00E4tter";
     foaf:surname "Umst\u00E4tter";
     foaf:title "Prof. Dr. rer. nat." .

<foaf:Organization rdf:about="&ex;zlb">
     foaf:homepage <http://www.zlb.de>;
     foaf:name "Zentral- und Landesbibliothek Berlin"@de .

在采用了