gpt4 book ai didi

org.eclipse.rdf4j.rio.RDFWriter, how to set base namespace when writing document(org.eclipse.rdf4j.rio.RDFWriter,写文档时如何设置基命名空间)

转载 作者:bug小助手 更新时间:2023-10-26 20:23:31 26 4
gpt4 key购买 nike



I am using the rdf4j library to export xml files.
I use org.eclipse.rdf4j.rio.RDFWriter to save it in a file and I need to define the base namespace. So I expect to have something like :

我正在使用rdf4j库来导出XML文件。我使用org.eclipse.rdf4j.rio.RDFWriter将其保存到文件中,并且我需要定义基本名称空间。因此,我希望拥有这样的东西:


<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xml:base="http://exmaple.org/">
<rdf:Description rdf:about=foo>
<rdf:type rdf:resource= FOO>
...

I understood that I can set a namespace using handleNamespace(). But I cant figure out how to use it to set the base namespace : I tried :

我知道我可以使用handleNamespace()设置名称空间。但我想不出如何使用它来设置基本名称空间:我试过了:


ModelBuilder builder = new ModelBuilder();
ValueFactory vf = SimpleValueFactory.getInstance();
builder.add(vf.createIRI("base:foo"), RDFS.ISDEFINEDBY, vf.createIRI("base:FOO");
Model model = builder.build();
RDFWriter writer = Rio.createWriter(RDFFormat.RDFXML, someOutputStream);
try {
writer.startRDF();
writer.handleNamespace("base", "http://exmaple.org/");
for (Statement st: model) {
writer.handleStatement(st);
}
writer.endRDF();
...

but this does not give me what I expect :

但这并没有给我带来我所期望的:


<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:base="http://exmaple.org/">
<rdf:Description rdf:about=base:foo>
<rdf:type rdf:resource= base:FOO>

Any ideas?

有什么主意吗?


更多回答
优秀答案推荐

A namespace is not the same thing as a base URI, and there is no such thing as a "base namespace". Namespaces are an abbreviation mechanism for URIs where you define a prefix that replaces the first part of a full URI with a short string, so that in your RDF data you can represent the URI with a "prefixed name" (depending on the specific RDF syntax what that looks like, exactly).

命名空间与基本URI不是一回事,也没有“基本命名空间”这回事。名称空间是URI的缩写机制,您可以在其中定义一个前缀,将完整URI的第一部分替换为一个短字符串,以便在RDF数据中可以用“前缀名称”来表示URI(具体取决于具体的RDF语法,确切地说是什么样子)。


Base URIs, on the other hand, are a generic parser/writer mechanism that allows resolving relative URIs in the data. Some syntax formats (like XML) allow defining the base URI in the document itself, others don't, but they serve a different purpose than namespaces.

另一方面,基本URI是一种通用的解析器/写入器机制,允许解析数据中的相对URI。一些语法格式(如XML)允许在文档本身中定义基本URI,而其他格式则不允许,但它们的用途与名称空间不同。


As an example, this XML snippet:

作为示例,下面的XML片段如下:


<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:base="http://example.org/">
<rdf:Description rdf:about="foo">
....

is legal RDF, despite the fact that the RDF/XML syntax spec requires that the value of the rdf:about attribute (the subject) is a full URI. The reason it's legal is that a base URI is defined against which the string foo can be resolved as a relative URI. The resolved full URI will be http://example.org/foo. Note that this has nothing to do with namespaces.

是合法的RDF,尽管RDF/XML语法规范要求rdf:About属性(主题)的值是一个完整的URI。之所以合法,是因为定义了一个基URI,可以根据该基URI将字符串foo解析为相对URI。解析的完整URI将为http://example.org/foo.请注意,这与名称空间无关。


What is happening in your code is that you are not using base URIs at all. In the first part you are defining an RDF model in memory, using the ModelBuilder. You are adding the string base:foo as a subject URI. The ModelBuilder (actually, the ValueFactory, but it comes tot the same thing) interprets this as a full URI, because there is no namespace defined in the ModelBuilder that has base as its prefix.

在您的代码中发生的情况是,您根本没有使用基本URI。在第一部分中,您将使用ModelBuilder在内存中定义RDF模型。您将添加字符串base:foo作为主题URI。模型构建器(实际上是ValueFactory,但结果是相同的)将其解释为完整的URI,因为在模型构建器中没有定义以base为前缀的命名空间。


Writing that model to XML file does not change that, regardless of what you define as base URI or namespaces while writing: you are exporting an RDF model that has base:foo as a full URI, so that's what you'll get in your output.

将该模型写入XML文件并不会改变这一点,无论您在编写时定义为基本URI还是名称空间:您正在将一个具有base:foo的RDF模型导出为一个完整的URI,因此这就是您在输出中得到的结果。


To get what you want, you need to make sure that your Model object has the full URIs, and that, instead of trying add a namespace to the writer, you set its base URI when creating the writer.

要获得您想要的内容,您需要确保Model对象具有完整的URI,并且在创建编写器时设置其基本URI,而不是尝试向编写器添加名称空间。


        var model = new ModelBuilder().add("http://example.org/foo", RDFS.ISDEFINEDBY, "http://example.org/FOO").build();
var writer = Rio.createWriter(RDFFormat.RDFXML, System.out, "http://example.org/");

writer.startRDF();
for (Statement st : model) {
writer.handleStatement(st);
}
writer.endRDF();

...or more succinctly:

...或者更简洁地说:


        var model = new ModelBuilder().add("http://example.org/foo", RDFS.ISDEFINEDBY, "http://example.org/FOO").build();

Rio.write(model, System.out, "http://example.org/", RDFFormat.RDFXML);

If you don't like having to write the full URI every time when creating your model, that's where namespaces can come in handy, for example:

如果您不喜欢每次创建模型时都要编写完整的URI,那么名称空间就可以派上用场了,例如:


        var model = new ModelBuilder().setNamespace("ex", "http://example.org/")
.add("ex:foo", RDFS.ISDEFINEDBY, "ex:FOO").build();

更多回答

Thanks a lot @Jeen Broekstra. Your answer made things very clear :)

非常感谢@Jeen Broekstra。你的回答很清楚:)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com