开发者

openrdf-sesame SPARQLResultsXMLWriter and XSL

开发者 https://www.devze.com 2023-04-11 23:46 出处:网络
how can i add xsl stylesheet in XML out stream provided by SPARQLResultsXMLWriter? RepositoryConnection con = repository.getConnection();

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 Framewor开发者_如何学JAVAk... the similar for Sesame

Thanks, Enzo


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消