使用 Jena 在 RDF 文件中创建嵌套标签

发布于 2024-10-27 13:41:26 字数 402 浏览 0 评论 0原文

我是耶拿的新手,现在遇到以下问题。 基本上,我需要创建一个具有嵌套元素的 RDF,例如:

<rdf:description about="http://www.acme.com">
    <dc:country>
      <dc:capital>London</dc:capital>
      <dc:language>English</dc:language>  
    <dc:country>
</rdf:description>

也许这很简单,但我无法做到这一点。我已经阅读了一些教程,但没有提供此类元素的示例。 有人可以为我提供一个能够产生这样的输出的 java 工作示例吗?

非常感谢!

I am new to Jena and I am now stuck with the following problem.
Basically I need to create a RDF having nested elements, for example:

<rdf:description about="http://www.acme.com">
    <dc:country>
      <dc:capital>London</dc:capital>
      <dc:language>English</dc:language>  
    <dc:country>
</rdf:description>

Maybe this is trivial, however I wasn't able to do that. I have read several tutorial but no example for this kind of element was present.
Could some one provide me a working example in java capable to produce such output?

Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2024-11-03 13:41:26

您没有说明如何生成输出,但最可能的解释是您正在使用默认输出格式 RDF/XML。相反,如果您调用,

yourModel.write( outStream, "RDF/XML-ABBREV" );

您将获得一个更紧凑的结构,该结构很可能包含每个示例的嵌套元素。

然而,有一个“然而”。

您没有说明为什么“需要创建具有嵌套元素的 RDF”。 Jena 生成的所有输出在 RDF 三元组级别上在语义上都是等效的。任何兼容的 RDF 处理器都会通过解析序列化为 RDF/XML(无论是否缩写、Turtle、n-三元组等)的模型来生成相同的模型。因此,如果您将输出交给另一个 RDF 进程,那么无论是否缩写都无关紧要。您的输出是否有嵌套结构。

但是,如果您希望将输出提供给 XML 工具链,那么这可能会更加困难。 RDF XML 编码非常复杂。此外,Jena XML 编写器不保证按特定顺序序列化元素。根据您正在编写的 Model 的内容,XML 结构可能看起来完全不同。例如,这可能会使编写 XSLT 样式表变得棘手。

如果您计划将 RDF XML 通过管道传输到您的 XML 工具链中,我建议您创建一个自定义编写器,以您的 XML 工具可以依赖的格式生成可靠的输出,或者可能使用 SPARQL 查询您的模型并使用 SPARQL 的 XML 编码结果集作为 XML 管道的输入。 SPARQL 的 XML 编码 比完整的 RDF/XML 更规则,并且基础更好用于后续 XML 处理。

You don't say how you are generating your output, but the most likely explanation is that you are using the default output format RDF/XML. If, instead, you call

yourModel.write( outStream, "RDF/XML-ABBREV" );

you will get a more compact structure that will most likely contain nested elements per your example.

There is a 'however', however.

You don't say why you "need to create RDF having nested elements". All of the output generated by Jena is semantically equivalent at the RDF triples level. Any compliant RDF processor will produce the same model by parsing a model serialised as RDF/XML, abbreviated or not, Turtle, n-triples, etc. So if you are handing the output off to another RDF process, it won't matter whether your output has nested structures or not.

If, however, you are hoping to feed the output into an XML toolchain then this can be more difficult. The RDF XML encoding is notoriously complex. Moreover, the Jena XML writer does not guarantee to serialise elements in a particular order. Depending on the contents of the Model you are writing, the XML structure may come out looking quite different. This can make, for example, writing XSLT stylesheets tricky.

If you are planning to pipe RDF XML into your XML toolchain, I would recommend either creating a custom writer which produces reliable output in the format your XML tools can rely on, or perhaps querying your model using SPARQL and using the XML encoding of the SPARQL resultset as the input to your XML pipeline. SPARQL's XML encoding is more regular than full RDF/XML, and a better basis for subsequent XML processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文