openrdf-sesame SPARQLResultsXMLWriter 和 XSL

发布于 2024-12-09 18:05:18 字数 584 浏览 0 评论 0原文

如何在 SPARQLResultsXMLWriter 提供的 XML 输出流中添加 xsl 样式表?

RepositoryConnection con = repository.getConnection();
SPARQLResultsXMLWriter sparqlWriter = new SPARQLResultsXMLWriter(out);
   try {
       TupleQuery query = con.prepareTupleQuery(org.openrdf.query.QueryLanguage.SPARQL, qs);
       query.evaluate(sparqlWriter);
   } 
   finally {
       con.close();
   }

我正在寻找类似的东西,

com.hp.hpl.jena.query.ResultSetFormatter.outputAsXML(outStream,resulSet,**xslFilePath**);

由 Jena Framework 提供...类似于 Sesame

谢谢, 恩佐

how can i add xsl stylesheet in XML out stream provided by SPARQLResultsXMLWriter?

RepositoryConnection con = repository.getConnection();
SPARQLResultsXMLWriter sparqlWriter = new SPARQLResultsXMLWriter(out);
   try {
       TupleQuery query = con.prepareTupleQuery(org.openrdf.query.QueryLanguage.SPARQL, qs);
       query.evaluate(sparqlWriter);
   } 
   finally {
       con.close();
   }

I'm looking for something like this,

com.hp.hpl.jena.query.ResultSetFormatter.outputAsXML(outStream,resulSet,**xslFilePath**);

provided by Jena Framework... the similar for Sesame

Thanks,
Enzo

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

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

发布评论

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

评论(1

隔岸观火 2024-12-16 18:05:18

默认的 Sesame SPARQLResultsXMLWriter 没有此选项。然而,定制它应该相当容易。您需要做的是创建 info.aduna.xml.XMLWriter 类的子类(该类是 SPARQLResultsXMLWriter 内部使用的用于正确格式化 XML 的实用程序类)。在此子类中,添加 getter/setter 来指定样式表链接,然后重写 startDocument 方法,如下所示:

public class MyXMLWriter extends info.aduna.xml.XMLWriter {

     @Override
     public void startDocument() throws IOException {
         super.startDocument();
         _write("<?xml-stylesheet type=\"text/xsl\"");
         _write(" href=\"" + getStylesheetRef() + "\"");
         _writeLn("?>");
     }
}

然后只需将这个自定义的 XMLWriter 提供给您的 SPARQLResultsXMLWriter 并将样式表引用设置为正确的值:

MyXMLWriter writer = new MyXMLWriter(out);
writer.setStylesheetRef("example.xsl");

SPARQLResultsXMLWriter sparqlWriter = new SPARQLResultsXMLWriter(writer);

未经测试,但相当有信心这应该工作。

或者,向 Sesame 项目提出功能请求和/或向他们提供补丁。

The default Sesame SPARQLResultsXMLWriter does not have an option for this. However, it should be fairly easy to customize it. What you need to do is create a subclass of the class info.aduna.xml.XMLWriter (which is a utility class for correctly formatted XML that is used by the SPARQLResultsXMLWriter internally). In this subclass, add a getter/setter to specify the stylesheet link, and then override the startDocument method like so:

public class MyXMLWriter extends info.aduna.xml.XMLWriter {

     @Override
     public void startDocument() throws IOException {
         super.startDocument();
         _write("<?xml-stylesheet type=\"text/xsl\"");
         _write(" href=\"" + getStylesheetRef() + "\"");
         _writeLn("?>");
     }
}

Then just supply this customized XMLWriter to your SPARQLResultsXMLWriter and set the stylesheet ref to the correct value:

MyXMLWriter writer = new MyXMLWriter(out);
writer.setStylesheetRef("example.xsl");

SPARQLResultsXMLWriter sparqlWriter = new SPARQLResultsXMLWriter(writer);

Untested, but fairly confident this should work.

Alternatively, raise a feature request with the Sesame project and/or supply them with a patch.

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