Unicode 转义序列的 dotNetRdf 问题/Jena Fuseki 无法在 URI 中加载撇号

dotNetRdf issue with Unicode escape sequences / Jena Fuseki inability to load apostrophe in URI

我正在开发一个 Web 应用程序,我需要支持将 RDF 数据从多个数据源(DB 转储/URI)存储到我的 Jena Fuseki 服务器上。我遇到了 dotNetRdf 的问题。我正在使用作为 NuGet 包下载的最新版本 (2.2.0)。我认为这个问题可能是由于在解析时不幸处理了 unicode 转义序列造成的。

起初,当我遇到解析错误时,我试图从 dotNetRdf 的文档(部分:读取 RDF 数据,link 部分)中获取示例。 失败代码如下:

IGraph g = new Graph();
g.LoadFromUri(new Uri("http://dbpedia.org/resource/Barack_Obama"));

这在功能上应该等同于文档中的代码示例(https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Reading-RDF#reading-rdf-from-uris),我只是使用扩展方法。

我收到 VDS.RDF.Parsing.RdfParseException 消息:

[Line 2233 Column 42 to Line 2233 Column 83] 
Unexpected Token <b>'Integer'</b> encountered, expected a Property Value
describing one of the properties of an Object Node

来自给定 DBpedia 资源的第 2233 行应该如下:

"Barack Hussein Obama II (US /b\u0259\u02C8r\u0251\u02D0k hu\u02D0\u02C8se\u026An o\u028A\u02C8b\u0251\u02D0m\u0259/; born August 4, 1961) is an American politician who is the 44th and current President of the United States. He is the first African American to hold the office and the first president born outside the continental United States. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University and Harvard Law School, where he was president of the Harvard Law Review. He was a community organizer in Chicago before earning his law degree. He worked as a civil rights attorney and taught constitutional law at the University of Chicago Law School between 1992 and 2004. While serving three terms representing the 13th District in the Illinois Senate from 1997 to 2004, he ran unsuccessfully in the Democratic primary for the United States Hou"@en ,

在第 42 列和第 84 列之间有一些 unicode 转义序列,所以我想 dotNetRdf 没有正确解析它们?! (因为有关于意外整数的注释。)

我在 Whosebug 上看到了一些讨论 DBpedia 无法提供正确数据的问题,但这些问题似乎有些过时了,现在已经是 2019 年了。所以我认为 DBpedia 不是问题所在。我对 RDF 数据的处理经验很少,但我觉得一切都还好。


其次,我尝试通过 .NET 的 HttpClient 下载内容并指定一些 Accept-Headers(在我的例子中为 text/turtle),然后尝试将数据加载到 IGraph通过调用 IGraph.LoadFromString(...) 方法实例。没有帮助。同样的问题,但不同的例外。

第三 - 我终于找到了解决方法!我已经将内容加载到字符串变量中(正如所说 - 通过 HttpClient),然后我使用了 VDS.RDF.Parsing.Notation3Parser class。 这行得通,但是... 出现了另一个问题 - 当我试图将图形保存到我的 Jena Fuseki Triplestore 中时,我得到了一个 RdfStorageException 内部异常(WebException:远程服务器返回 400请求错误)。

异常信息:

A HTTP error (HTTP 400 Parse error: [line: 10, col: 50] 
The declaration for the entity "ns5" must end with '>'.) 
occurred while saving a Graph to the Store.
Empty response body, see aformentioned status line or the inner exception for further details

所以可能数据甚至没有被正确解析?甚至可能吗?

这是简化的解决方法代码:

string content = /* get content via HttpClient */;

IGraph g = new Graph();
IRdfReader reader = new Notation3Parser();
reader.Load(g, new StringReader(content));

string connectionStr = "...";
var store = new PersistentTripleStore(new FusekiConnector(connectionStr));
...
store.UnderlyingStore.SaveGraph(g); // this call causes the mentioned RdfStorageException

我使用扩展方法将 IGraph 保存到文件中以查看 IGraph 中的内容(文件内容在此处可用:https://pastebin.com/nULJtjXu)并再次 - 当我查找第 10 行时,这导致问题,有一个unicode转义序列:

@prefix ns5:    <http://dbpedia.org/resource/Buyer\u0027s_Remorse:> .

(注:\u0027是撇号('))

奇怪的是,在DBpedia返回的HTTP Response中,有很多unicode转义序列,第一次出现解析不会失败。

所以我的 Jena Fuseki 更有可能在加载 URI 中带有撇号的数据时遇到问题?

非常感谢对我的问题的任何帮助

Fuseki 错误可能是由 RDF/XML dotNetRDF 编写器中的错误引起的。

当您将 IGraph 写入文件时,您似乎使用了 Turtle 或 Notation3 编写器。但是当 dotNetRDF 与 Fuseki 对话时,它使用 RDF/XML writer。所以你的 pastebin 的内容不是发送给 Fuseki 的内容。

我在发送这样的 RDF/XML 文件时从 Fuseki 收到了同样的错误:

<!DOCTYPE RDF [
  <!ENTITY ns5 'http://dbpedia.org/resource/Buyer's_Remorse:' >
]>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>

此文件不包含任何数据,它只是设置了一个 XML 实体,这在 RDF/XML 中很常见。该文件无效,因为实体声明中间的撇号未转义。 (这是XML,所以需要转义为&apos;。)

您可以通过使用 RDF/XML 编写器将 IGraph 写入文件来验证问题。

我已就此事提交 a bug report for dotNetRDF